mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: downloadMedia
This commit is contained in:
@@ -1,9 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
CacheFileList,
|
|
||||||
CacheFileListItem,
|
CacheFileListItem,
|
||||||
CacheFileType,
|
CacheFileType,
|
||||||
CacheScanResult,
|
|
||||||
ChatCacheList,
|
|
||||||
ChatCacheListItemBasic,
|
ChatCacheListItemBasic,
|
||||||
ChatType,
|
ChatType,
|
||||||
ElementType, IMAGE_HTTP_HOST, IMAGE_HTTP_HOST_NT, RawMessage
|
ElementType, IMAGE_HTTP_HOST, IMAGE_HTTP_HOST_NT, RawMessage
|
||||||
@@ -21,22 +18,8 @@ import { ISizeCalculationResult } from 'image-size/dist/types/interface';
|
|||||||
import { sessionConfig } from '@/core/sessionConfig';
|
import { sessionConfig } from '@/core/sessionConfig';
|
||||||
import { randomUUID } from 'crypto';
|
import { randomUUID } from 'crypto';
|
||||||
import { rkeyManager } from '../utils/rkey';
|
import { rkeyManager } from '../utils/rkey';
|
||||||
|
import { NTEventDispatch } from '@/common/utils/EventTask';
|
||||||
|
|
||||||
|
|
||||||
const downloadMediaTasks: Map<string, (arg: OnRichMediaDownloadCompleteParams) => void> = new Map<string, (arg: OnRichMediaDownloadCompleteParams) => void>();
|
|
||||||
|
|
||||||
const downloadMediaListener = new MsgListener();
|
|
||||||
downloadMediaListener.onRichMediaDownloadComplete = arg => {
|
|
||||||
for (const [uuid, cb] of downloadMediaTasks) {
|
|
||||||
cb(arg);
|
|
||||||
downloadMediaTasks.delete(uuid);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
setTimeout(() => {
|
|
||||||
napCatCore.onLoginSuccess(() => {
|
|
||||||
napCatCore.addListener(downloadMediaListener);
|
|
||||||
});
|
|
||||||
}, 100);
|
|
||||||
export class NTQQFileApi {
|
export class NTQQFileApi {
|
||||||
static async getFileType(filePath: string) {
|
static async getFileType(filePath: string) {
|
||||||
return fileType.fileTypeFromFile(filePath);
|
return fileType.fileTypeFromFile(filePath);
|
||||||
@@ -103,31 +86,33 @@ export class NTQQFileApi {
|
|||||||
return sourcePath;
|
return sourcePath;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//logDebug('start downloadMedia', msgId, chatType, peerUid, elementId, thumbPath, sourcePath, timeout, force);
|
let data = await NTEventDispatch.CallNormalEvent<
|
||||||
return new Promise<string>((resolve, reject) => {
|
(
|
||||||
let completed = false;
|
params: {
|
||||||
const cb = (arg: OnRichMediaDownloadCompleteParams) => {
|
fileModelId: string,
|
||||||
//logDebug('downloadMedia complete', arg, msgId);
|
downloadSourceType: number,
|
||||||
|
triggerType: number,
|
||||||
|
msgId: string,
|
||||||
|
chatType: ChatType,
|
||||||
|
peerUid: string,
|
||||||
|
elementId: string,
|
||||||
|
thumbSize: number,
|
||||||
|
downloadType: number,
|
||||||
|
filePath: string
|
||||||
|
}) => Promise<unknown>,
|
||||||
|
(fileTransNotifyInfo: OnRichMediaDownloadCompleteParams) => void
|
||||||
|
>(
|
||||||
|
'NodeIKernelMsgService/downloadRichMedia',
|
||||||
|
'NodeIKernelMsgListener/onRichMediaDownloadComplete',
|
||||||
|
1,
|
||||||
|
timeout,
|
||||||
|
(arg: OnRichMediaDownloadCompleteParams) => {
|
||||||
if (arg.msgId === msgId) {
|
if (arg.msgId === msgId) {
|
||||||
completed = true;
|
return true;
|
||||||
let filePath = arg.filePath;
|
|
||||||
if (filePath.startsWith('\\')) {
|
|
||||||
// log('filePath start with \\');
|
|
||||||
const downloadPath = sessionConfig.defaultFileDownloadPath;
|
|
||||||
//logDebug('downloadPath', downloadPath);
|
|
||||||
filePath = path.join(downloadPath, filePath);
|
|
||||||
// 下载路径是下载文件夹的相对路径
|
|
||||||
}
|
|
||||||
resolve(filePath);
|
|
||||||
}
|
}
|
||||||
};
|
return false;
|
||||||
downloadMediaTasks.set(randomUUID(), cb);
|
},
|
||||||
setTimeout(() => {
|
{
|
||||||
if (!completed) {
|
|
||||||
reject('下载超时');
|
|
||||||
}
|
|
||||||
}, timeout);
|
|
||||||
napCatCore.session.getMsgService().downloadRichMedia({
|
|
||||||
fileModelId: '0',
|
fileModelId: '0',
|
||||||
downloadSourceType: 0,
|
downloadSourceType: 0,
|
||||||
triggerType: 1,
|
triggerType: 1,
|
||||||
@@ -137,9 +122,18 @@ export class NTQQFileApi {
|
|||||||
elementId: elementId,
|
elementId: elementId,
|
||||||
thumbSize: 0,
|
thumbSize: 0,
|
||||||
downloadType: 1,
|
downloadType: 1,
|
||||||
filePath: thumbPath,
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> {
|
static async getImageSize(filePath: string): Promise<ISizeCalculationResult | undefined> {
|
||||||
|
Reference in New Issue
Block a user