From 9a791e3a21dceb1d5b68cd810ef42925e77a4b76 Mon Sep 17 00:00:00 2001 From: idranme Date: Tue, 17 Sep 2024 02:17:16 +0800 Subject: [PATCH] fix --- src/common/utils/file.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index 5af41fd..8c995e1 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -91,17 +91,26 @@ interface FetchFileRes { } export async function fetchFile(url: string, headersInit?: Record): Promise { - const headers: Record = { + const headers = new Headers({ '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', 'Host': new URL(url).hostname, ...headersInit - } - const raw = await fetch(url, { headers }).catch((err) => { + }) + let raw = await fetch(url, { headers }).catch((err) => { if (err.cause) { throw err.cause } throw err }) + if (raw.status === 403 && !headers.has('Referer')) { + headers.set('Referer', url) + raw = await fetch(url, { headers }).catch((err) => { + if (err.cause) { + throw err.cause + } + throw err + }) + } if (!raw.ok) throw new Error(`statusText: ${raw.statusText}`) return { data: Buffer.from(await raw.arrayBuffer()), @@ -133,7 +142,7 @@ export async function uri2local(uri: string, filename?: string, needExt?: boolea if (type === FileUriType.RemoteURL) { try { - const res = await fetchFile(uri, { 'Referer': uri }) + const res = await fetchFile(uri) const match = res.url.match(/.+\/([^/?]*)(?=\?)?/) if (match?.[1]) { filename ??= match[1].replace(/[/\\:*?"<>|]/g, '_')