From 640e3516d434da02fbc54c3036ac2a329c5016f1 Mon Sep 17 00:00:00 2001 From: Alen Date: Sun, 15 Sep 2024 17:23:10 +0800 Subject: [PATCH 1/2] style --- src/onebot/api/msg.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index c8090c0b..919a51ae 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -629,7 +629,7 @@ export class OneBotMsgApi { if (element.grayTipElement.subElementType == NTGrayTipElementSubTypeV2.GRAYTIP_ELEMENT_SUBTYPE_XMLMSG) { //好友添加成功事件 if (element.grayTipElement.xmlElement.templId === '10229' && msg.peerUin !== '') { - return new OB11FriendAddNoticeEvent(this.core, parseInt(msg.peerUin) || Number(await this.core.apis.UserApi.getUinByUidV2(msg.peerUid))); + return new OB11FriendAddNoticeEvent(this.core, parseInt(msg.peerUin || await this.core.apis.UserApi.getUinByUidV2(msg.peerUid))); } } } From 3093bdbc685f810fef6c8e26e44f56bdc0d4eef7 Mon Sep 17 00:00:00 2001 From: Alen Date: Tue, 17 Sep 2024 03:39:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E4=B8=8B=E8=BD=BD=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化下载逻辑 --- src/common/file.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/common/file.ts b/src/common/file.ts index 60477fce..6ece861c 100644 --- a/src/common/file.ts +++ b/src/common/file.ts @@ -123,12 +123,11 @@ export interface HttpDownloadOptions { headers?: Record | string; } -export async function httpDownload(options: string | HttpDownloadOptions): Promise { +async function tryDownload(options: string | HttpDownloadOptions, useReferer: boolean = false): Promise { // const chunks: Buffer[] = []; let url: string; let headers: Record = { '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') { 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) => { if (err.cause) { throw err.cause; } 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 { + 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(); return Buffer.from(buffer); }