From 39477aa6a0702975dac9c50e347f6d262c5d055e Mon Sep 17 00:00:00 2001 From: "Wesley F. Young" Date: Thu, 29 Aug 2024 22:27:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20try=20to=20fix=20'=E6=90=9C=E7=B4=A2?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E6=A8=A1=E5=BC=8F'=20of=20GetFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/apis/file.ts | 41 +++++++--- .../NodeIKernelFileAssistantListener.ts | 80 ++++++++++++++++++- .../NodeIKernelFileAssistantService.ts | 10 ++- src/onebot/action/file/GetFile.ts | 25 ++---- 4 files changed, 119 insertions(+), 37 deletions(-) diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index 03118965..5d3a25ce 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -17,7 +17,7 @@ import { import path from 'path'; import fs from 'fs'; import fsPromises from 'fs/promises'; -import { InstanceContext, NapCatCore } from '@/core'; +import { InstanceContext, NapCatCore, SearchResultItem } from '@/core'; import * as fileType from 'file-type'; import imageSize from 'image-size'; import { ISizeCalculationResult } from 'image-size/dist/types/interface'; @@ -438,17 +438,36 @@ export class NTQQFileApi { }); } - async searchfile(keys: string[]) { - const Event = this.core.eventWrapper.createEventFunction('NodeIKernelSearchService/searchFileWithKeywords'); - const id = await Event!(keys, 12); - const Listener = this.core.eventWrapper.registerListen( - 'NodeIKernelSearchListener/onSearchFileKeywordsResult', - 1, - 20000, - (params) => id !== '' && params.searchId == id, + async searchForFile(keys: string[]): Promise { + const [, searchResult] = await this.core.eventWrapper.callNormalEventV2( + 'NodeIKernelFileAssistantService/searchFile', + 'NodeIKernelFileAssistantListener/onFileSearch', + [ + keys, + { + resultType: 2, + pageLimit: 1, + } + ] ); - const [ret] = (await Listener); - return ret; + return searchResult.resultItems[0]; + } + + async downloadFileById( + fileId: string, + fileSize: number = 1024576, + estimatedTime: number = (fileSize * 1000 / 1024576) + 5000, + ) { + const [, ret] = await this.core.eventWrapper.callNormalEventV2( + 'NodeIKernelFileAssistantService/downloadFile', + 'NodeIKernelFileAssistantListener/onFileStatusChanged', + [[fileId]], + ret => ret.result === 0, + status => status.fileStatus === 2 && status.fileProgress === '0', + 1, + estimatedTime, // estimate 1MB/s + ); + return ret.filePath!; } async getImageUrl(element: PicElement) { diff --git a/src/core/listeners/NodeIKernelFileAssistantListener.ts b/src/core/listeners/NodeIKernelFileAssistantListener.ts index 0765ed11..b3524249 100644 --- a/src/core/listeners/NodeIKernelFileAssistantListener.ts +++ b/src/core/listeners/NodeIKernelFileAssistantListener.ts @@ -1,5 +1,13 @@ export class NodeIKernelFileAssistantListener { - onFileStatusChanged(...args: unknown[]) { + onFileStatusChanged(fileStatus: { + id: string, + fileStatus: number, + fileProgress: `${number}`, + fileSize: `${number}`, + fileSpeed: number, + thumbPath: string | null, + filePath: string | null, + }) { } onSessionListChanged(...args: unknown[]) { @@ -11,6 +19,74 @@ export class NodeIKernelFileAssistantListener { onFileListChanged(...args: unknown[]) { } - onFileSearch(...args: unknown[]) { + onFileSearch(searchResult: { + searchId: number, + resultType: number, + hasMore: boolean, + resultItems: { + id: string, + fileName: string, + fileNameHits: string[], + fileStatus: number, + fileSize: string, + isSend: boolean, + source: number, + fileTime: string, + expTime: string, + session: { + context: null, + uid: string, + nick: string, + remark: string, + memberCard: string, + groupCode: string, + groupName: string, + groupRemark: string, + count: number, + }, + thumbPath: string, + filePath: string, + msgId: string, + chatType: number, + peerUid: string, + fileType: number, + }[], + }) { } } + +export type SearchResultWrapper = { + searchId: number, + resultType: number, + hasMore: boolean, + resultItems: SearchResultItem[], +}; + +export type SearchResultItem = { + id: string, + fileName: string, + fileNameHits: string[], + fileStatus: number, + fileSize: string, + isSend: boolean, + source: number, + fileTime: string, + expTime: string, + session: { + context: null, + uid: string, + nick: string, + remark: string, + memberCard: string, + groupCode: string, + groupName: string, + groupRemark: string, + count: number, + }, + thumbPath: string, + filePath: string, + msgId: string, + chatType: number, + peerUid: string, + fileType: number, +}; diff --git a/src/core/services/NodeIKernelFileAssistantService.ts b/src/core/services/NodeIKernelFileAssistantService.ts index 66037223..83936ffc 100644 --- a/src/core/services/NodeIKernelFileAssistantService.ts +++ b/src/core/services/NodeIKernelFileAssistantService.ts @@ -1,5 +1,7 @@ +import { NodeIKernelFileAssistantListener } from '@/core'; + export interface NodeIKernelFileAssistantService { - addKernelFileAssistantListener(arg1: unknown[]): unknown; + addKernelFileAssistantListener(listener: NodeIKernelFileAssistantListener): unknown; removeKernelFileAssistantListener(arg1: unknown[]): unknown; @@ -9,7 +11,7 @@ export interface NodeIKernelFileAssistantService { getFileSessionList(): unknown; - searchFile(arg1: unknown, arg2: unknown, arg3: unknown): unknown; + searchFile(keywords: string[], params: { resultType: number, pageLimit: number }): unknown; resetSearchFileSortType(arg1: unknown, arg2: unknown, arg3: unknown): unknown; @@ -17,7 +19,7 @@ export interface NodeIKernelFileAssistantService { cancelSearchFile(arg1: unknown, arg2: unknown, arg3: unknown): unknown; - downloadFile(arg1: unknown[]): unknown; + downloadFile(fileIds: string[]): { result: number, errMsg: string }; forwardFile(arg1: unknown, arg2: unknown, arg3: unknown): unknown; @@ -32,4 +34,4 @@ export interface NodeIKernelFileAssistantService { saveAsWithRename(arg1: unknown, arg2: unknown, arg3: unknown): unknown; isNull(): boolean; -} \ No newline at end of file +} diff --git a/src/onebot/action/file/GetFile.ts b/src/onebot/action/file/GetFile.ts index 57a3b60f..b28464a8 100644 --- a/src/onebot/action/file/GetFile.ts +++ b/src/onebot/action/file/GetFile.ts @@ -84,29 +84,14 @@ export class GetFileBase extends BaseAction { } //搜索名字模式 - const NTSearchNameResult = (await NTQQFileApi.searchfile([payload.file])).resultItems; - if (NTSearchNameResult.length !== 0) { - const MsgId = NTSearchNameResult[0].msgId; - let peer: Peer | undefined = undefined; - if (NTSearchNameResult[0].chatType == ChatType.KCHATTYPEGROUP) { - peer = { chatType: ChatType.KCHATTYPEGROUP, peerUid: NTSearchNameResult[0].groupChatInfo[0].groupCode }; - } - if (!peer) throw new Error('chattype not support'); - const msgList: RawMessage[] = (await NTQQMsgApi.getMsgsByMsgId(peer, [MsgId]))?.msgList; - if (!msgList || msgList.length == 0) { - throw new Error('msg not found'); - } - const msg = msgList[0]; - const file = msg.elements.filter(e => e.elementType == NTSearchNameResult[0].elemType); - if (file.length == 0) { - throw new Error('file not found'); - } - const downloadPath = await NTQQFileApi.downloadMedia(msg.msgId, msg.chatType, msg.peerUid, file[0].elementId, '', ''); + const searchResult = (await NTQQFileApi.searchForFile([payload.file])); + if (searchResult) { + const downloadPath = await NTQQFileApi.downloadFileById(searchResult.id, parseInt(searchResult.fileSize)); const res: GetFileResponse = { file: downloadPath, url: downloadPath, - file_size: NTSearchNameResult[0].fileSize.toString(), - file_name: NTSearchNameResult[0].fileName, + file_size: searchResult.fileSize.toString(), + file_name: searchResult.fileName, }; if (this.obContext.configLoader.configData.enableLocalFile2Url && downloadPath) { try {