refactor: 重构图片代理控制器,优化代码结构

This commit is contained in:
jiangrui
2025-02-24 16:10:46 +08:00
parent f5106e782a
commit f860cc1f57
2 changed files with 44 additions and 21 deletions

View File

@@ -3,6 +3,7 @@ import { sendSuccess, sendError } from "../utils/response";
import Searcher from "../services/Searcher"; import Searcher from "../services/Searcher";
import UserSetting from "../models/UserSetting"; import UserSetting from "../models/UserSetting";
import GlobalSetting from "../models/GlobalSetting"; import GlobalSetting from "../models/GlobalSetting";
import { iamgesInstance } from "./teleImages";
export const settingController = { export const settingController = {
async get(req: Request, res: Response): Promise<void> { async get(req: Request, res: Response): Promise<void> {
@@ -45,6 +46,7 @@ export const settingController = {
await UserSetting.update(userSettings, { where: { userId } }); await UserSetting.update(userSettings, { where: { userId } });
if (role === 1 && globalSetting) await GlobalSetting.update(globalSetting, { where: {} }); if (role === 1 && globalSetting) await GlobalSetting.update(globalSetting, { where: {} });
Searcher.updateAxiosInstance(); Searcher.updateAxiosInstance();
iamgesInstance.updateProxyConfig();
sendSuccess(res, { sendSuccess(res, {
message: "保存成功", message: "保存成功",
}); });

View File

@@ -1,43 +1,64 @@
import axios, { AxiosInstance } from "axios"; import axios, { AxiosInstance } from "axios";
import { Request, Response } from "express"; import e, { Request, Response } from "express";
import tunnel from "tunnel"; import tunnel from "tunnel";
import GlobalSetting from "../models/GlobalSetting"; import GlobalSetting from "../models/GlobalSetting";
import { GlobalSettingAttributes } from "../models/GlobalSetting"; import { GlobalSettingAttributes } from "../models/GlobalSetting";
export class ImageControll { export class ImageControll {
private axiosInstance: AxiosInstance | null = null; private axiosInstance: AxiosInstance | null = null;
private isUpdate = false; private settings: GlobalSetting | null = null;
constructor() { constructor() {
this.initializeAxiosInstance(); this.initializeAxiosInstance();
} }
private async initializeAxiosInstance(isUpdate = false): Promise<void> { private async initializeAxiosInstance(): Promise<void> {
let settings = null; try {
if (isUpdate) { this.settings = await GlobalSetting.findOne();
settings = await GlobalSetting.findOne(); } catch (error) {
this.isUpdate = isUpdate; console.error("Error fetching global settings:", error);
} else {
return;
} }
const globalSetting = settings?.dataValues || ({} as GlobalSettingAttributes); const globalSetting = this.settings?.dataValues || ({} as GlobalSettingAttributes);
this.axiosInstance = axios.create({ this.axiosInstance = axios.create({
timeout: 3000, timeout: 3000,
httpsAgent: tunnel.httpsOverHttp({ httpsAgent: globalSetting.isProxyEnabled
proxy: { ? tunnel.httpsOverHttp({
host: globalSetting.httpProxyHost, proxy: {
port: globalSetting.httpProxyPort, host: globalSetting.httpProxyHost,
headers: { port: globalSetting.httpProxyPort,
"Proxy-Authorization": "", headers: {
}, "Proxy-Authorization": "",
}, },
}), },
})
: undefined,
withCredentials: true, withCredentials: true,
}); });
} }
public async updateProxyConfig(): Promise<void> {
try {
this.settings = await GlobalSetting.findOne();
const globalSetting = this.settings?.dataValues || ({} as GlobalSettingAttributes);
if (this.axiosInstance) {
this.axiosInstance.defaults.httpsAgent = globalSetting.isProxyEnabled
? tunnel.httpsOverHttp({
proxy: {
host: globalSetting.httpProxyHost,
port: globalSetting.httpProxyPort,
headers: {
"Proxy-Authorization": "",
},
},
})
: undefined;
}
} catch (error) {
console.error("Error updating proxy config:", error);
}
}
async getImages(req: Request, res: Response, url: string): Promise<void> { async getImages(req: Request, res: Response, url: string): Promise<void> {
try { try {
if (!this.isUpdate) await this.initializeAxiosInstance(true);
const response = await this.axiosInstance?.get(url, { responseType: "stream" }); const response = await this.axiosInstance?.get(url, { responseType: "stream" });
res.set("Content-Type", response?.headers["content-type"]); res.set("Content-Type", response?.headers["content-type"]);
response?.data.pipe(res); response?.data.pipe(res);
@@ -47,7 +68,7 @@ export class ImageControll {
} }
} }
const iamgesInstance = new ImageControll(); export const iamgesInstance = new ImageControll();
export const imageControll = { export const imageControll = {
getImages: async (req: Request, res: Response): Promise<void> => { getImages: async (req: Request, res: Response): Promise<void> => {