Use element.fileSize fields to calc totalSize

Remove filesystem stat calls and the node:fs/promises import; replace the async size calculation with a synchronous reduce that sums parseInt(fileSize) from each send element (falls back to 0 on parse failure). This avoids runtime I/O, simplifies the logic, and still provides a totalSize used to compute message timeout.
This commit is contained in:
手瓜一十雪 committed 2026-03-12 15:07:44 +08:00
1 parent e9b74138d1
commit 7a974d38d4
1 file changed
+14 -24
+14 -24
View File
@@ -34,7 +34,6 @@ import { EventType } from '@/napcat-onebot/event/OneBotEvent';
import { encodeCQCode } from '@/napcat-onebot/helper/cqcode'; import { encodeCQCode } from '@/napcat-onebot/helper/cqcode';
import { uriToLocalFile } from 'napcat-common/src/file'; import { uriToLocalFile } from 'napcat-common/src/file';
import { RequestUtil } from 'napcat-common/src/request'; import { RequestUtil } from 'napcat-common/src/request';
import fsPromise from 'node:fs/promises';
import { OB11FriendAddNoticeEvent } from '@/napcat-onebot/event/notice/OB11FriendAddNoticeEvent'; import { OB11FriendAddNoticeEvent } from '@/napcat-onebot/event/notice/OB11FriendAddNoticeEvent';
import { ForwardMsgBuilder } from '@/napcat-core/helper/forward-msg-builder'; import { ForwardMsgBuilder } from '@/napcat-core/helper/forward-msg-builder';
import { calculateTimeout, capTimeout } from '@/napcat-onebot/config/config'; import { calculateTimeout, capTimeout } from '@/napcat-onebot/config/config';
@@ -1253,29 +1252,20 @@ export class OneBotMsgApi {
if (timeoutOverride !== undefined && timeoutOverride > 0) { if (timeoutOverride !== undefined && timeoutOverride > 0) {
timeout = capTimeout(timeoutConfig, timeoutOverride); timeout = capTimeout(timeoutConfig, timeoutOverride);
} else { } else {
const calculateTotalSize = async (elements: SendMessageElement[]): Promise<number> => { const totalSize = sendElements.reduce((total, element) => {
const sizePromises = elements.map(async element => { switch (element.elementType) {
switch (element.elementType) { case ElementType.PTT:
case ElementType.PTT: return total + (parseInt(element.pttElement.fileSize) || 0);
return (await fsPromise.stat(element.pttElement.filePath)).size; case ElementType.FILE:
case ElementType.FILE: return total + (parseInt(element.fileElement.fileSize) || 0);
return (await fsPromise.stat(element.fileElement.filePath)).size; case ElementType.VIDEO:
case ElementType.VIDEO: return total + (parseInt(element.videoElement.fileSize) || 0);
return (await fsPromise.stat(element.videoElement.filePath)).size; case ElementType.PIC:
case ElementType.PIC: return total + (parseInt(element.picElement.fileSize) || 0);
return (await fsPromise.stat(element.picElement.sourcePath)).size; default:
default: return total;
return 0; }
} }, 0);
});
const sizes = await Promise.all(sizePromises);
return sizes.reduce((total, size) => total + size, 0);
};
const totalSize = await calculateTotalSize(sendElements).catch(e => {
this.core.context.logger.logError('发送消息计算预计时间异常', e);
return 0;
});
timeout = calculateTimeout(timeoutConfig, totalSize, timeoutConfig.uploadSpeedKBps); timeout = calculateTimeout(timeoutConfig, totalSize, timeoutConfig.uploadSpeedKBps);
} }