From 897e5a6f61419055e0270361c28a9cabd865219a Mon Sep 17 00:00:00 2001 From: "Wesley F. Young" Date: Sat, 7 Sep 2024 10:15:35 +0800 Subject: [PATCH] feat: destroy file cache from raw or url --- src/laana/action/message.ts | 23 +++++++++++++++------- src/laana/utils/file.ts | 13 +++++++++++++ src/laana/utils/message.ts | 39 ++++++++++++++++++++++++++++--------- 3 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/laana/action/message.ts b/src/laana/action/message.ts index 9e20a744..a5eaf3f1 100644 --- a/src/laana/action/message.ts +++ b/src/laana/action/message.ts @@ -11,30 +11,39 @@ export class LaanaMessageActionHandler { impl: LaanaActionHandler = { sendMessage: async (params) => { - const { elements, fileCacheIds } = await this.laana.utils.msg.laanaMessageToRaw(params.message!, params); + const { elements, fileCacheRecords } = await this.laana.utils.msg.laanaMessageToRaw(params.message!, params); + let cacheSize = 0; try { - for (const cacheId of fileCacheIds) { - cacheSize += fs.statSync(await this.laana.utils.file.toLocalPath(cacheId)).size; + for (const cacheRecord of fileCacheRecords) { + cacheSize += fs.statSync(await this.laana.utils.file.toLocalPath(cacheRecord.cacheId)).size; } } catch (e) { this.core.context.logger.logWarn('文件缓存大小计算失败', e); } const estimatedSendMsgTimeout = cacheSize / 1024 / 256 * 1000 + // file upload time - 1000 * fileCacheIds.length + // request timeout + 1000 * fileCacheRecords.length + // request timeout 10000; // fallback timeout - const sentMsg = await this.core.apis.MsgApi.sendMsg( + + const sentMsgOrEmpty = await this.core.apis.MsgApi.sendMsg( await this.laana.utils.msg.laanaPeerToRaw(params.targetPeer!), elements, true, // TODO: add 'wait complete' (bool) field estimatedSendMsgTimeout, ); - if (!sentMsg) { + + fileCacheRecords.forEach(record => { + if (record.originalType !== 'cacheId') { + this.laana.utils.file.destroyCache(record.cacheId); + } + }); + + if (!sentMsgOrEmpty) { throw Error('消息发送失败'); } return { - msgId: sentMsg.msgId + msgId: sentMsgOrEmpty.msgId }; } }; diff --git a/src/laana/utils/file.ts b/src/laana/utils/file.ts index 8a8ac697..168dc492 100644 --- a/src/laana/utils/file.ts +++ b/src/laana/utils/file.ts @@ -83,4 +83,17 @@ export class LaanaFileUtils { fileElementId }; } + + async destroyCache(cacheId: string) { + const cachePath = path.join(this.cacheDir, cacheId); + const stat = await fsPromises.stat(cachePath); + if (stat.isFile()) { + await fsPromises.unlink(cachePath); + } else if (stat.isSymbolicLink()) { + await fsPromises.unlink(cachePath); + await fsPromises.unlink(await fsPromises.readlink(cachePath)); + } else { + throw Error('不支持的缓存类型'); + } + } } diff --git a/src/laana/utils/message.ts b/src/laana/utils/message.ts index 872ecaa1..3cf09e86 100644 --- a/src/laana/utils/message.ts +++ b/src/laana/utils/message.ts @@ -12,8 +12,14 @@ import { import { NapCatLaanaAdapter } from '..'; import { OutgoingMessage, SendMessagePing } from '../types/action/message'; import { Bubble, Message as LaanaMessage, Peer as LaanaPeer, Peer_Type } from '../types/entity/message'; +import { File } from '../types/entity/file'; import faceConfig from '@/core/external/face_config.json'; +export type SentMessageFileCacheRecord = { + originalType: File['uri']['oneofKind'], + cacheId: string, +}; + type Laana2RawConverters = { [key in Exclude]: ( @@ -23,7 +29,7 @@ type Laana2RawConverters = { params: SendMessagePing, ) => PromiseLike<{ elements: SendMessageElement[], - fileCacheIds: string[], + fileCacheRecords: SentMessageFileCacheRecord[], }> } @@ -51,7 +57,7 @@ export class LaanaMessageUtils { } const elements: SendMessageElement[] = []; - const fileCacheIds: string[] = []; + const fileCacheRecords: SentMessageFileCacheRecord[] = []; if (msgContent.repliedMsgId) { const replyMsg = ( @@ -155,13 +161,16 @@ export class LaanaMessageUtils { elements.push(await this.core.apis.FileApi.createValidSendPicElement( await this.laana.utils.file.toLocalPath(cacheId) )); - fileCacheIds.push(cacheId); + fileCacheRecords.push({ + originalType: content.image.uri.oneofKind, + cacheId, + }); } else { throw Error('未知的消息内容类型'); } } - return { elements, fileCacheIds }; + return { elements, fileCacheRecords }; }, file: async msgContent => { @@ -173,7 +182,10 @@ export class LaanaMessageUtils { msgContent.name, ), ], - fileCacheIds: [cacheId], + fileCacheRecords: [{ + originalType: msgContent.file!.uri.oneofKind, + cacheId, + }], }; }, @@ -187,7 +199,10 @@ export class LaanaMessageUtils { // TODO: add 'sub type' field ) ], - fileCacheIds: [cacheId], + fileCacheRecords: [{ + originalType: msgContent.image!.uri.oneofKind, + cacheId, + }], }; }, @@ -201,7 +216,7 @@ export class LaanaMessageUtils { faceName: msgContent.displayText ?? '[商城表情]', }, }], - fileCacheIds: [], + fileCacheRecords: [], }), video: async msgContent => { @@ -213,7 +228,10 @@ export class LaanaMessageUtils { // TODO: add file name and thumb path ), ], - fileCacheIds: [cacheId], + fileCacheRecords: [{ + originalType: msgContent.uri.oneofKind, + cacheId, + }], }; }, @@ -225,7 +243,10 @@ export class LaanaMessageUtils { await this.laana.utils.file.toLocalPath(cacheId), ) ], - fileCacheIds: [cacheId], + fileCacheRecords: [{ + originalType: msgContent.uri.oneofKind, + cacheId, + }], }; },