This commit is contained in:
手瓜一十雪 2024-09-17 08:56:53 +08:00
commit 3843795d8f

View File

@ -123,12 +123,11 @@ export interface HttpDownloadOptions {
headers?: Record<string, string> | string; headers?: Record<string, string> | string;
} }
export async function httpDownload(options: string | HttpDownloadOptions): Promise<Buffer> { async function tryDownload(options: string | HttpDownloadOptions, useReferer: boolean = false): Promise<Response> {
// const chunks: Buffer[] = []; // const chunks: Buffer[] = [];
let url: string; let url: string;
let headers: Record<string, string> = { let headers: Record<string, string> = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36',
'referer': typeof options === 'string' ? options : options.url,
}; };
if (typeof options === 'string') { if (typeof options === 'string') {
url = options; url = options;
@ -143,15 +142,26 @@ export async function httpDownload(options: string | HttpDownloadOptions): Promi
} }
} }
} }
if (useReferer && !headers['Referer']) {
headers['Referer'] = url;
};
const fetchRes = await fetch(url, { headers }).catch((err) => { const fetchRes = await fetch(url, { headers }).catch((err) => {
if (err.cause) { if (err.cause) {
throw err.cause; throw err.cause;
} }
throw err; throw err;
}); });
if (!fetchRes.ok) throw new Error(`下载文件失败: ${fetchRes.statusText}`); return fetchRes;
}
const blob = await fetchRes.blob(); export async function httpDownload(options: string | HttpDownloadOptions): Promise<Buffer> {
const useReferer = typeof options === 'string';
let resp = await tryDownload(options);
if (resp.status === 403 && useReferer) {
resp = await tryDownload(options, true);
}
if (!resp.ok) throw new Error(`下载文件失败: ${resp.statusText}`);
const blob = await resp.blob();
const buffer = await blob.arrayBuffer(); const buffer = await blob.arrayBuffer();
return Buffer.from(buffer); return Buffer.from(buffer);
} }