Spaces:
Sleeping
Sleeping
File size: 1,991 Bytes
f0953a4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import { injectable } from "inversify";
import axios, { AxiosInstance } from "axios";
import tunnel from "tunnel";
import GlobalSetting from "../models/GlobalSetting";
import { GlobalSettingAttributes } from "../models/GlobalSetting";
@injectable()
export class ImageService {
private axiosInstance: AxiosInstance | null = null;
constructor() {
// 移除构造函数中的初始化,改为懒加载
}
private async ensureAxiosInstance(): Promise<AxiosInstance> {
if (!this.axiosInstance) {
const settings = await GlobalSetting.findOne();
const globalSetting = settings?.dataValues || ({} as GlobalSettingAttributes);
this.axiosInstance = axios.create({
timeout: 30000,
headers: {
Accept: "image/*, */*",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
},
withCredentials: false,
maxRedirects: 5,
httpsAgent: globalSetting.isProxyEnabled
? tunnel.httpsOverHttp({
proxy: {
host: globalSetting.httpProxyHost,
port: globalSetting.httpProxyPort,
headers: {
"Proxy-Authorization": "",
},
},
})
: undefined,
});
this.axiosInstance.interceptors.response.use(
(response) => response,
(error) => {
throw error;
}
);
}
return this.axiosInstance;
}
async updateAxiosInstance(): Promise<void> {
this.axiosInstance = null;
await this.ensureAxiosInstance();
}
async getImages(url: string): Promise<any> {
const axiosInstance = await this.ensureAxiosInstance();
return await axiosInstance.get(url, {
responseType: "stream",
validateStatus: (status) => status >= 200 && status < 300,
headers: {
Referer: new URL(url).origin,
},
});
}
}
|