From b016268fdb63ac47b20abceabad9e53cc13a7b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 9 Aug 2024 17:12:57 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=9D=E6=AD=A5fileApi=E5=B0=B1?= =?UTF-8?q?=E7=BB=AA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/apis/file.ts | 128 +++++++++--------- src/core/core.ts | 7 +- src/core/wrapper/context.ts | 3 +- src/onebot/action/extends/CreateCollection.ts | 1 - src/onebot/action/file/DelGroupFileFolder.ts | 1 + src/onebot/action/file/GetFile.ts | 17 +-- src/onebot/action/file/GetGroupFileCount.ts | 1 + src/onebot/action/file/GetGroupFileList.ts | 3 +- src/onebot/action/file/SetGroupFileFolder.ts | 3 +- 9 files changed, 84 insertions(+), 80 deletions(-) diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index b54ae53b..2154af95 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -75,69 +75,71 @@ export class NTQQFileApi { async downloadMediaByUuid() { //napCatCore.session.getRichMediaService().downloadFileForFileUuid(); } - // async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) { - // //logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force); - // // 用于下载收到的消息中的图片等 - // if (sourcePath && fs.existsSync(sourcePath)) { - // if (force) { - // try { - // await fsPromises.unlink(sourcePath); - // } catch (e) { - // // - // } - // } else { - // return sourcePath; - // } - // } - // const data = await this.core.eventWrapper.CallNormalEvent< - // ( - // params: { - // fileModelId: string, - // downloadSourceType: number, - // triggerType: number, - // msgId: string, - // chatType: ChatType, - // peerUid: string, - // elementId: string, - // thumbSize: number, - // downloadType: number, - // filePath: string - // }) => Promise, - // (fileTransNotifyInfo: OnRichMediaDownloadCompleteParams) => void - // >( - // 'NodeIKernelMsgService/downloadRichMedia', - // 'NodeIKernelMsgListener/onRichMediaDownloadComplete', - // 1, - // timeout, - // (arg: OnRichMediaDownloadCompleteParams) => { - // if (arg.msgId === msgId) { - // return true; - // } - // return false; - // }, - // { - // fileModelId: '0', - // downloadSourceType: 0, - // triggerType: 1, - // msgId: msgId, - // chatType: chatType, - // peerUid: peerUid, - // elementId: elementId, - // thumbSize: 0, - // downloadType: 1, - // filePath: thumbPath - // } - // ); - // let filePath = data[1].filePath; - // if (filePath.startsWith('\\')) { - // // log('filePath start with \\'); - // const downloadPath = sessionConfig.defaultFileDownloadPath; - // //logDebug('downloadPath', downloadPath); - // filePath = path.join(downloadPath, filePath); - // // 下载路径是下载文件夹的相对路径 - // } - // return filePath; - // } + async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) { + //logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force); + // 用于下载收到的消息中的图片等 + if (sourcePath && fs.existsSync(sourcePath)) { + if (force) { + try { + await fsPromises.unlink(sourcePath); + } catch (e) { + // + } + } else { + return sourcePath; + } + } + const data = await this.core.eventWrapper.CallNormalEvent< + ( + params: { + fileModelId: string, + downloadSourceType: number, + triggerType: number, + msgId: string, + chatType: ChatType, + peerUid: string, + elementId: string, + thumbSize: number, + downloadType: number, + filePath: string + }) => Promise, + (fileTransNotifyInfo: OnRichMediaDownloadCompleteParams) => void + >( + 'NodeIKernelMsgService/downloadRichMedia', + 'NodeIKernelMsgListener/onRichMediaDownloadComplete', + 1, + timeout, + (arg: OnRichMediaDownloadCompleteParams) => { + if (arg.msgId === msgId) { + return true; + } + return false; + }, + { + fileModelId: '0', + downloadSourceType: 0, + triggerType: 1, + msgId: msgId, + chatType: chatType, + peerUid: peerUid, + elementId: elementId, + thumbSize: 0, + downloadType: 1, + filePath: thumbPath + } + ); + let filePath = data[1].filePath; + if (filePath.startsWith('\\')) { + // log('filePath start with \\'); + // Mlikiowa V2.0.0 Refactor Todo + //const downloadPath = sessionConfig.defaultFileDownloadPath; + //logDebug('downloadPath', downloadPath); + //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + filePath = path.join("", filePath); + // 下载路径是下载文件夹的相对路径 + } + return filePath; + } async getImageSize(filePath: string): Promise { return new Promise((resolve, reject) => { diff --git a/src/core/core.ts b/src/core/core.ts index b0ea6bd6..7a6f801b 100644 --- a/src/core/core.ts +++ b/src/core/core.ts @@ -8,7 +8,7 @@ import { MsgListener, ProfileListener } from "./listeners"; import { sleep } from "@/common/utils/helper"; import { SelfInfo, LineDevice, SelfStatusInfo } from "./entities"; import { LegacyNTEventWrapper } from "@/common/framework/event-legacy"; -import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "./apis"; +import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "./apis"; import os from "node:os"; import { NTQQCollectionApi } from "./apis/collection"; export enum NapCatCoreWorkingEnv { @@ -43,8 +43,9 @@ export class NapCatCore { this.eventWrapper = new LegacyNTEventWrapper(context.wrapper, context.session); this.initNapCatCoreListeners().then().catch(console.error); this.ApiContext = { - SystemApi:new NTQQSystemApi(this.context, this), - CollectionApi:new NTQQCollectionApi(this.context, this), + FileApi: new NTQQFileApi(this.context, this), + SystemApi: new NTQQSystemApi(this.context, this), + CollectionApi: new NTQQCollectionApi(this.context, this), WebApi: new NTQQWebApi(this.context, this), FriendApi: new NTQQFriendApi(this.context, this), MsgApi: new NTQQMsgApi(this.context, this), diff --git a/src/core/wrapper/context.ts b/src/core/wrapper/context.ts index 4f27ab31..16cb0d53 100644 --- a/src/core/wrapper/context.ts +++ b/src/core/wrapper/context.ts @@ -4,7 +4,7 @@ import { NapCatCoreWorkingEnv } from "@/core"; import { SelfInfo } from "../entities"; import { NodeIKernelLoginService } from "../services"; import { WrapperNodeApi, NodeIQQNTWrapperSession } from "@/core"; -import { NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "../apis"; +import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from "../apis"; import { NTQQCollectionApi } from "../apis/collection"; export interface InstanceContext { @@ -16,6 +16,7 @@ export interface InstanceContext { readonly basicInfoWrapper: QQBasicInfoWrapper; } export interface NTApiContext { + FileApi: NTQQFileApi, SystemApi: NTQQSystemApi, CollectionApi: NTQQCollectionApi, WebApi: NTQQWebApi, diff --git a/src/onebot/action/extends/CreateCollection.ts b/src/onebot/action/extends/CreateCollection.ts index 257a16fc..e72178d0 100644 --- a/src/onebot/action/extends/CreateCollection.ts +++ b/src/onebot/action/extends/CreateCollection.ts @@ -17,7 +17,6 @@ export class CreateCollection extends BaseAction { PayloadSchema = SchemaData; protected async _handle(payload: Payload) { return await this.CoreContext.getApiContext().CollectionApi.createCollection( - this.CoreContext.selfInfo.uin, this.CoreContext.selfInfo.uid, this.CoreContext.selfInfo.nick, diff --git a/src/onebot/action/file/DelGroupFileFolder.ts b/src/onebot/action/file/DelGroupFileFolder.ts index 31907558..ad9cdc47 100644 --- a/src/onebot/action/file/DelGroupFileFolder.ts +++ b/src/onebot/action/file/DelGroupFileFolder.ts @@ -18,6 +18,7 @@ export class DelGroupFileFolder extends BaseAction { actionName = ActionName.DelGroupFileFolder; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; return (await NTQQGroupApi.DelGroupFileFolder(payload.group_id.toString(), payload.folder_id)).groupFileCommonResult; } } diff --git a/src/onebot/action/file/GetFile.ts b/src/onebot/action/file/GetFile.ts index 85672682..00f992b6 100644 --- a/src/onebot/action/file/GetFile.ts +++ b/src/onebot/action/file/GetFile.ts @@ -1,12 +1,9 @@ import BaseAction from '../BaseAction'; import fs from 'fs/promises'; -import { ob11Config } from '@/onebot11/config'; import { UUIDConverter } from '@/common/utils/helper'; -import { ActionName, BaseCheckResult } from '../types'; +import { ActionName } from '../types'; import { ChatType, ElementType, FileElement, Peer, RawMessage, VideoElement } from '@/core/entities'; -import { NTQQFileApi, NTQQFriendApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; -import { getGroup } from '@/core/data'; export interface GetFilePayload { file: string; // 文件名或者fileUuid @@ -42,7 +39,11 @@ export class GetFileBase extends BaseAction { return { id: element.elementId, element: element.fileElement }; } protected async _handle(payload: GetFilePayload): Promise { - const { enableLocalFile2Url } = ob11Config; + const NTQQFriendApi = this.CoreContext.getApiContext().FriendApi; + const NTQQUserApi = this.CoreContext.getApiContext().UserApi; + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; + const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; + const NTQQFileApi = this.CoreContext.getApiContext().FileApi; let UuidData: { high: string; low: string; @@ -52,7 +53,7 @@ export class GetFileBase extends BaseAction { if (UuidData) { const peerUin = UuidData.high; const msgId = UuidData.low; - const isGroup = await getGroup(peerUin); + const isGroup: boolean = !!(await NTQQGroupApi.getGroups(false)).find(e => e.groupCode == peerUin); let peer: Peer | undefined; //识别Peer if (isGroup) { @@ -88,7 +89,7 @@ export class GetFileBase extends BaseAction { file_size: fileSize, file_name: fileName }; - if (enableLocalFile2Url) { + if (true/*enableLocalFile2Url*/) { try { res.base64 = await fs.readFile(downloadPath, 'base64'); } catch (e) { @@ -128,7 +129,7 @@ export class GetFileBase extends BaseAction { file_size: NTSearchNameResult[0].fileSize.toString(), file_name: NTSearchNameResult[0].fileName }; - if (enableLocalFile2Url) { + if (true/*enableLocalFile2Url*/) { try { res.base64 = await fs.readFile(downloadPath, 'base64'); } catch (e) { diff --git a/src/onebot/action/file/GetGroupFileCount.ts b/src/onebot/action/file/GetGroupFileCount.ts index 2b84a2b6..dbf17a2d 100644 --- a/src/onebot/action/file/GetGroupFileCount.ts +++ b/src/onebot/action/file/GetGroupFileCount.ts @@ -17,6 +17,7 @@ export class GetGroupFileCount extends BaseAction { actionName = ActionName.GetGroupFileCount; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; const ret = await NTQQGroupApi.GetGroupFileCount([payload.group_id?.toString()]); return { count: ret.groupFileCounts[0] }; } diff --git a/src/onebot/action/file/GetGroupFileList.ts b/src/onebot/action/file/GetGroupFileList.ts index 0d5a48e7..0a4f1d6c 100644 --- a/src/onebot/action/file/GetGroupFileList.ts +++ b/src/onebot/action/file/GetGroupFileList.ts @@ -1,8 +1,6 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NTQQGroupApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis'; - const SchemaData = { type: 'object', properties: { @@ -19,6 +17,7 @@ export class GetGroupFileList extends BaseAction actionName = ActionName.GetGroupFileList; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQMsgApi = this.CoreContext.getApiContext().MsgApi; const ret = await NTQQMsgApi.getGroupFileList(payload.group_id.toString(), { sortType: 1, fileCount: payload.file_count, diff --git a/src/onebot/action/file/SetGroupFileFolder.ts b/src/onebot/action/file/SetGroupFileFolder.ts index 856a2ba5..0ff52a43 100644 --- a/src/onebot/action/file/SetGroupFileFolder.ts +++ b/src/onebot/action/file/SetGroupFileFolder.ts @@ -1,8 +1,6 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { NTQQGroupApi, NTQQMsgApi, NTQQUserApi } from '@/core/apis'; - const SchemaData = { type: 'object', properties: { @@ -18,6 +16,7 @@ export class SetGroupFileFolder extends BaseAction { actionName = ActionName.SetGroupFileFolder; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { + const NTQQGroupApi = this.CoreContext.getApiContext().GroupApi; return (await NTQQGroupApi.CreatGroupFileFolder(payload.group_id.toString(), payload.folder_name)).resultWithGroupItem; } }