mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: GetFile
This commit is contained in:
@@ -23,31 +23,28 @@ export async function solveAsyncProblem<T extends (...args: any[]) => Promise<an
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
//下面这个类是用于将uid+msgid合并的类
|
|
||||||
export class UUIDConverter {
|
|
||||||
static encode(highStr: string, lowStr: string): string {
|
|
||||||
const high = BigInt(highStr);
|
|
||||||
const low = BigInt(lowStr);
|
|
||||||
const highHex = high.toString(16).padStart(16, '0');
|
|
||||||
const lowHex = low.toString(16).padStart(16, '0');
|
|
||||||
const combinedHex = highHex + lowHex;
|
|
||||||
return `${combinedHex.substring(0, 8)}-${combinedHex.substring(8, 12)}-${combinedHex.substring(
|
|
||||||
12,
|
|
||||||
16,
|
|
||||||
)}-${combinedHex.substring(16, 20)}-${combinedHex.substring(20)}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
static decode(uuid: string): { high: string; low: string } {
|
|
||||||
const hex = uuid.replace(/-/g, '');
|
|
||||||
const high = BigInt('0x' + hex.substring(0, 16));
|
|
||||||
const low = BigInt('0x' + hex.substring(16));
|
|
||||||
return { high: high.toString(), low: low.toString() };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export class FileNapCatOneBotUUID {
|
export class FileNapCatOneBotUUID {
|
||||||
|
static encodeModelId(peer: Peer, modelId: string): string {
|
||||||
|
return `NapCatOneBot-ModeldFile-${peer.chatType}-${peer.peerUid}-${modelId}`;
|
||||||
|
}
|
||||||
|
static decodeModelId(uuid: string): undefined | {
|
||||||
|
peer: Peer,
|
||||||
|
modelId: string
|
||||||
|
} {
|
||||||
|
if (!uuid.startsWith('NapCatOneBot-ModeldFile-')) return undefined;
|
||||||
|
const data = uuid.split('-');
|
||||||
|
if (data.length !== 5) return undefined;
|
||||||
|
const [, , chatType, peerUid, modelId] = data;
|
||||||
|
return {
|
||||||
|
peer: {
|
||||||
|
chatType: chatType as any,
|
||||||
|
peerUid: peerUid
|
||||||
|
},
|
||||||
|
modelId,
|
||||||
|
};
|
||||||
|
}
|
||||||
static encode(peer: Peer, msgId: string, elementId: string): string {
|
static encode(peer: Peer, msgId: string, elementId: string): string {
|
||||||
return `NapCatOneBot-File-${peer.chatType}-${peer.peerUid}-${msgId}-${elementId}`;
|
return `NapCatOneBot-MsgFile-${peer.chatType}-${peer.peerUid}-${msgId}-${elementId}`;
|
||||||
}
|
}
|
||||||
static decode(uuid: string): undefined | {
|
static decode(uuid: string): undefined | {
|
||||||
peer: Peer,
|
peer: Peer,
|
||||||
|
@@ -302,7 +302,18 @@ export class NTQQFileApi {
|
|||||||
async downloadMediaByUuid() {
|
async downloadMediaByUuid() {
|
||||||
//napCatCore.session.getRichMediaService().downloadFileForFileUuid();
|
//napCatCore.session.getRichMediaService().downloadFileForFileUuid();
|
||||||
}
|
}
|
||||||
|
async downloadFileForModelId(peer: Peer, modelId: string, timeout = 1000 * 60 * 2) {
|
||||||
|
const [, fileTransNotifyInfo] = await this.core.eventWrapper.callNormalEventV2(
|
||||||
|
'NodeIKernelRichMediaService/downloadFileForModelId',
|
||||||
|
'NodeIKernelMsgListener/onRichMediaDownloadComplete',
|
||||||
|
[peer, [modelId]],
|
||||||
|
() => true,
|
||||||
|
(arg) => arg?.commonFileInfo?.fileModelId === modelId,
|
||||||
|
1,
|
||||||
|
timeout,
|
||||||
|
);
|
||||||
|
return fileTransNotifyInfo.filePath;
|
||||||
|
}
|
||||||
async downloadMedia(msgId: string, chatType: ChatType, peerUid: string, elementId: string, thumbPath: string, sourcePath: string, timeout = 1000 * 60 * 2, force: boolean = false) {
|
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);
|
//logDebug('receive downloadMedia task', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force);
|
||||||
// 用于下载收到的消息中的图片等
|
// 用于下载收到的消息中的图片等
|
||||||
|
@@ -1,5 +1,25 @@
|
|||||||
import { ChatType, RawMessage } from '@/core/entities';
|
import { ChatType, RawMessage } from '@/core/entities';
|
||||||
|
export interface CommonFileInfo {
|
||||||
|
bizType: null;
|
||||||
|
chatType: number;
|
||||||
|
elemId: string;
|
||||||
|
favId: null;
|
||||||
|
fileModelId: string;
|
||||||
|
fileName: string;
|
||||||
|
fileSize: string;
|
||||||
|
md5: string;
|
||||||
|
md510m: string;
|
||||||
|
msgId: string;
|
||||||
|
msgTime: string;
|
||||||
|
parent: null;
|
||||||
|
peerUid: string;
|
||||||
|
picThumbPath: null;
|
||||||
|
sha: string;
|
||||||
|
sha3: string;
|
||||||
|
subId: string;
|
||||||
|
uuid: string;
|
||||||
|
[property: string]: any;
|
||||||
|
}
|
||||||
export interface OnRichMediaDownloadCompleteParams {
|
export interface OnRichMediaDownloadCompleteParams {
|
||||||
fileModelId: string,
|
fileModelId: string,
|
||||||
msgElementId: string,
|
msgElementId: string,
|
||||||
@@ -15,7 +35,7 @@ export interface OnRichMediaDownloadCompleteParams {
|
|||||||
totalSize: string,
|
totalSize: string,
|
||||||
trasferStatus: number,
|
trasferStatus: number,
|
||||||
step: number,
|
step: number,
|
||||||
commonFileInfo: unknown | null,
|
commonFileInfo?: CommonFileInfo,
|
||||||
fileSrvErrCode: string,
|
fileSrvErrCode: string,
|
||||||
clientMsg: string,
|
clientMsg: string,
|
||||||
businessId: number,
|
businessId: number,
|
||||||
|
@@ -155,7 +155,7 @@ export interface NodeIKernelRichMediaService {
|
|||||||
}): unknown;
|
}): unknown;
|
||||||
|
|
||||||
//arg3为“”
|
//arg3为“”
|
||||||
downloadFileForModelId(peer: Peer, ModelId: string[], arg3: string): unknown;
|
downloadFileForModelId(peer: Peer, ModelId: string[]): Promise<unknown>;
|
||||||
|
|
||||||
//第三个参数 Array<Type>
|
//第三个参数 Array<Type>
|
||||||
// this.fileId = "";
|
// this.fileId = "";
|
||||||
|
@@ -32,11 +32,11 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
|
|||||||
const NTQQMsgApi = this.core.apis.MsgApi;
|
const NTQQMsgApi = this.core.apis.MsgApi;
|
||||||
const NTQQFileApi = this.core.apis.FileApi;
|
const NTQQFileApi = this.core.apis.FileApi;
|
||||||
|
|
||||||
const contextFile = FileNapCatOneBotUUID.decode(payload.file);
|
const contextMsgFile = FileNapCatOneBotUUID.decode(payload.file);
|
||||||
|
|
||||||
//接收消息标记模式
|
//接收消息标记模式
|
||||||
if (contextFile) {
|
if (contextMsgFile) {
|
||||||
const { peer, msgId, elementId } = contextFile;
|
const { peer, msgId, elementId } = contextMsgFile;
|
||||||
|
|
||||||
const downloadPath = await NTQQFileApi.downloadMedia(msgId, peer.chatType, peer.peerUid, elementId, '', '');
|
const downloadPath = await NTQQFileApi.downloadMedia(msgId, peer.chatType, peer.peerUid, elementId, '', '');
|
||||||
|
|
||||||
@@ -65,6 +65,26 @@ export class GetFileBase extends BaseAction<GetFilePayload, GetFileResponse> {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
//群文件模式
|
//群文件模式
|
||||||
|
const contextModelIdFile = FileNapCatOneBotUUID.decodeModelId(payload.file);
|
||||||
|
if (contextModelIdFile) {
|
||||||
|
const { peer, modelId } = contextModelIdFile;
|
||||||
|
const downloadPath = await NTQQFileApi.downloadFileForModelId(peer, modelId);
|
||||||
|
const res: GetFileResponse = {
|
||||||
|
file: downloadPath,
|
||||||
|
url: downloadPath,
|
||||||
|
file_size: '',
|
||||||
|
file_name: '',
|
||||||
|
};
|
||||||
|
|
||||||
|
if (this.obContext.configLoader.configData.enableLocalFile2Url && downloadPath) {
|
||||||
|
try {
|
||||||
|
res.base64 = await fs.readFile(downloadPath, 'base64');
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error('文件下载失败. ' + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
//搜索名字模式
|
//搜索名字模式
|
||||||
const NTSearchNameResult = (await NTQQFileApi.searchfile([payload.file])).resultItems;
|
const NTSearchNameResult = (await NTQQFileApi.searchfile([payload.file])).resultItems;
|
||||||
|
Reference in New Issue
Block a user