mirror of
https://github.com/jiangrui1994/CloudSaver.git
synced 2026-01-10 15:18:46 +08:00
refactor: 重构图片代理控制器,优化代码结构
This commit is contained in:
@@ -3,6 +3,7 @@ import { sendSuccess, sendError } from "../utils/response";
|
||||
import Searcher from "../services/Searcher";
|
||||
import UserSetting from "../models/UserSetting";
|
||||
import GlobalSetting from "../models/GlobalSetting";
|
||||
import { iamgesInstance } from "./teleImages";
|
||||
|
||||
export const settingController = {
|
||||
async get(req: Request, res: Response): Promise<void> {
|
||||
@@ -45,6 +46,7 @@ export const settingController = {
|
||||
await UserSetting.update(userSettings, { where: { userId } });
|
||||
if (role === 1 && globalSetting) await GlobalSetting.update(globalSetting, { where: {} });
|
||||
Searcher.updateAxiosInstance();
|
||||
iamgesInstance.updateProxyConfig();
|
||||
sendSuccess(res, {
|
||||
message: "保存成功",
|
||||
});
|
||||
|
||||
@@ -1,43 +1,64 @@
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import { Request, Response } from "express";
|
||||
import e, { Request, Response } from "express";
|
||||
import tunnel from "tunnel";
|
||||
import GlobalSetting from "../models/GlobalSetting";
|
||||
import { GlobalSettingAttributes } from "../models/GlobalSetting";
|
||||
|
||||
export class ImageControll {
|
||||
private axiosInstance: AxiosInstance | null = null;
|
||||
private isUpdate = false;
|
||||
private settings: GlobalSetting | null = null;
|
||||
|
||||
constructor() {
|
||||
this.initializeAxiosInstance();
|
||||
}
|
||||
|
||||
private async initializeAxiosInstance(isUpdate = false): Promise<void> {
|
||||
let settings = null;
|
||||
if (isUpdate) {
|
||||
settings = await GlobalSetting.findOne();
|
||||
this.isUpdate = isUpdate;
|
||||
} else {
|
||||
return;
|
||||
private async initializeAxiosInstance(): Promise<void> {
|
||||
try {
|
||||
this.settings = await GlobalSetting.findOne();
|
||||
} catch (error) {
|
||||
console.error("Error fetching global settings:", error);
|
||||
}
|
||||
const globalSetting = settings?.dataValues || ({} as GlobalSettingAttributes);
|
||||
const globalSetting = this.settings?.dataValues || ({} as GlobalSettingAttributes);
|
||||
this.axiosInstance = axios.create({
|
||||
timeout: 3000,
|
||||
httpsAgent: tunnel.httpsOverHttp({
|
||||
proxy: {
|
||||
host: globalSetting.httpProxyHost,
|
||||
port: globalSetting.httpProxyPort,
|
||||
headers: {
|
||||
"Proxy-Authorization": "",
|
||||
},
|
||||
},
|
||||
}),
|
||||
httpsAgent: globalSetting.isProxyEnabled
|
||||
? tunnel.httpsOverHttp({
|
||||
proxy: {
|
||||
host: globalSetting.httpProxyHost,
|
||||
port: globalSetting.httpProxyPort,
|
||||
headers: {
|
||||
"Proxy-Authorization": "",
|
||||
},
|
||||
},
|
||||
})
|
||||
: undefined,
|
||||
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> {
|
||||
try {
|
||||
if (!this.isUpdate) await this.initializeAxiosInstance(true);
|
||||
const response = await this.axiosInstance?.get(url, { responseType: "stream" });
|
||||
res.set("Content-Type", response?.headers["content-type"]);
|
||||
response?.data.pipe(res);
|
||||
@@ -47,7 +68,7 @@ export class ImageControll {
|
||||
}
|
||||
}
|
||||
|
||||
const iamgesInstance = new ImageControll();
|
||||
export const iamgesInstance = new ImageControll();
|
||||
|
||||
export const imageControll = {
|
||||
getImages: async (req: Request, res: Response): Promise<void> => {
|
||||
|
||||
Reference in New Issue
Block a user