mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
feat: destroy file cache from raw or url
This commit is contained in:
parent
7a5d7dd49e
commit
897e5a6f61
@ -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
|
||||
};
|
||||
}
|
||||
};
|
||||
|
@ -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('不支持的缓存类型');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<OutgoingMessage['content']['oneofKind'], undefined>]:
|
||||
(
|
||||
@ -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,
|
||||
}],
|
||||
};
|
||||
},
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user