mirror of
https://github.com/jiangrui1994/CloudSaver.git
synced 2026-01-11 23:58:46 +08:00
feat:版本迭代
This commit is contained in:
57
backend/src/controllers/teleImages.ts
Normal file
57
backend/src/controllers/teleImages.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import axios, { AxiosInstance } from "axios";
|
||||
import { 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;
|
||||
|
||||
constructor() {
|
||||
this.initializeAxiosInstance();
|
||||
}
|
||||
|
||||
private async initializeAxiosInstance(isUpdate = false) {
|
||||
let settings = null;
|
||||
if (isUpdate) {
|
||||
settings = await GlobalSetting.findOne();
|
||||
this.isUpdate = isUpdate;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
const globalSetting = settings?.dataValues || ({} as GlobalSettingAttributes);
|
||||
this.axiosInstance = axios.create({
|
||||
timeout: 3000,
|
||||
httpsAgent: tunnel.httpsOverHttp({
|
||||
proxy: {
|
||||
host: globalSetting.httpProxyHost,
|
||||
port: globalSetting.httpProxyPort,
|
||||
headers: {
|
||||
"Proxy-Authorization": "",
|
||||
},
|
||||
},
|
||||
}),
|
||||
withCredentials: true,
|
||||
});
|
||||
}
|
||||
async getImages(req: Request, res: Response, url: string) {
|
||||
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);
|
||||
} catch (error) {
|
||||
res.status(500).send("Image fetch error");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const iamgesInstance = new ImageControll();
|
||||
|
||||
export const imageControll = {
|
||||
getImages: async (req: Request, res: Response) => {
|
||||
const url = req.query.url as string;
|
||||
iamgesInstance.getImages(req, res, url);
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user