Add isLocal flag to uriToLocalFile and use it

Expose an isLocal boolean from uriToLocalFile to indicate user-owned files vs temporary downloads/decodes, and update callers to avoid deleting or cleaning up user local files. Update OneBot file APIs to accept the isLocal hint (including createValidSendFileElement/createValidSendPicElement/createValidSendVideoElement) and adjust temp-file handling for videos: local files are copied into the NapCat temp dir with a random UUID filename to avoid polluting user directories, while temporary files get an added extension in-place. Also adjust deleteAfterSentFiles usage, image/video thumb handling, and numeric parsing fallbacks for element fileSize to be more robust. Minor imports (randomUUID) and related callsites were updated accordingly.
This commit is contained in:
手瓜一十雪 committed 2026-03-12 15:36:44 +08:00
1 parent 7a974d38d4
commit 07d4d80ab7
11 files changed
+76 -44

No files matched your search

+22 -7
View File
@@ -10,6 +10,7 @@ import { fileTypeFromFile } from 'file-type';
import pathLib from 'node:path';
import fsPromises from 'fs/promises';
import fs from 'fs';
import { randomUUID } from 'crypto';
import { defaultVideoThumbB64 } from '@/napcat-core/helper/ffmpeg/video';
export class OneBotFileApi {
obContext: NapCatOneBot11Adapter;
@@ -19,7 +20,7 @@ export class OneBotFileApi {
this.core = core;
}
async createValidSendFileElement (context: SendMessageContext, filePath: string, fileName: string = '', folderId: string = '', uploadGroupFile: boolean = false): Promise<SendFileElement> {
async createValidSendFileElement (context: SendMessageContext, filePath: string, fileName: string = '', folderId: string = '', uploadGroupFile: boolean = false, _isLocal: boolean = false): Promise<SendFileElement> {
const {
fileName: _fileName,
path,
@@ -43,7 +44,7 @@ export class OneBotFileApi {
};
}
async createValidSendPicElement (context: SendMessageContext, picPath: string, summary: string = '', subType: PicSubType = 0): Promise<SendPicElement> {
async createValidSendPicElement (context: SendMessageContext, picPath: string, summary: string = '', subType: PicSubType = 0, _isLocal: boolean = false): Promise<SendPicElement> {
const { md5, fileName, path, fileSize } = await this.core.apis.FileApi.uploadFile(picPath, ElementType.PIC, subType);
if (fileSize === 0) {
throw new Error('文件异常,大小为0');
@@ -71,7 +72,7 @@ export class OneBotFileApi {
};
}
async createValidSendVideoElement (context: SendMessageContext, filePath: string, fileName: string = '', _diyThumbPath: string = ''): Promise<SendVideoElement> {
async createValidSendVideoElement (context: SendMessageContext, filePath: string, fileName: string = '', _diyThumbPath: string = '', isLocal: boolean = false): Promise<SendVideoElement> {
let videoInfo = {
width: 1920,
height: 1080,
@@ -87,10 +88,24 @@ export class OneBotFileApi {
} catch (e) {
this.core.context.logger.logError('获取文件类型失败', e);
}
const newFilePath = `${filePath}.${fileExt}`;
fs.copyFileSync(filePath, newFilePath);
context.deleteAfterSentFiles.push(newFilePath);
filePath = newFilePath;
const currentExt = pathLib.extname(filePath).slice(1).toLowerCase();
const needsExtCopy = currentExt !== fileExt.toLowerCase();
if (needsExtCopy) {
if (isLocal) {
// 本地文件缺少正确扩展名:拷贝到临时目录,避免污染用户目录
const tempFilePath = pathLib.join(this.core.NapCatTempPath, `${randomUUID()}.${fileExt}`);
fs.copyFileSync(filePath, tempFilePath);
context.deleteAfterSentFiles.push(tempFilePath);
filePath = tempFilePath;
} else {
// 临时文件缺少正确扩展名:在原位置加扩展名
const newFilePath = `${filePath}.${fileExt}`;
fs.copyFileSync(filePath, newFilePath);
context.deleteAfterSentFiles.push(newFilePath);
filePath = newFilePath;
}
}
// 扩展名已匹配时:本地文件直接用原始路径(零拷贝),临时文件也无需额外操作
const { fileName: _fileName, path, fileSize, md5 } = await this.core.apis.FileApi.uploadFile(filePath, ElementType.VIDEO);
context.deleteAfterSentFiles.push(path);
+20 -14
View File
@@ -736,32 +736,36 @@ export class OneBotMsgApi {
// File service
[OB11MessageDataType.image]: async (sendMsg, context) => {
const result = await this.handleOb11FileLikeMessage(sendMsg, context);
return await this.obContext.apis.FileApi.createValidSendPicElement(
context,
(await this.handleOb11FileLikeMessage(sendMsg, context)).path,
result.path,
sendMsg.data.summary,
sendMsg.data.sub_type
sendMsg.data.sub_type,
result.isLocal
);
},
[OB11MessageDataType.file]: async (sendMsg, context) => {
const { path, fileName } = await this.handleOb11FileLikeMessage(sendMsg, context);
return await this.obContext.apis.FileApi.createValidSendFileElement(context, path, fileName);
const { path, fileName, isLocal } = await this.handleOb11FileLikeMessage(sendMsg, context);
return await this.obContext.apis.FileApi.createValidSendFileElement(context, path, fileName, '', false, isLocal);
},
[OB11MessageDataType.video]: async (sendMsg, context) => {
const { path, fileName } = await this.handleOb11FileLikeMessage(sendMsg, context);
const { path, fileName, isLocal } = await this.handleOb11FileLikeMessage(sendMsg, context);
let thumb = sendMsg.data.thumb;
if (thumb) {
const uri2LocalRes = await uriToLocalFile(this.core.NapCatTempPath, thumb);
if (uri2LocalRes.success) {
thumb = uri2LocalRes.path;
context.deleteAfterSentFiles.push(thumb);
if (!uri2LocalRes.isLocal) {
context.deleteAfterSentFiles.push(thumb);
}
}
}
return await this.obContext.apis.FileApi.createValidSendVideoElement(context, path, fileName, thumb);
return await this.obContext.apis.FileApi.createValidSendVideoElement(context, path, fileName, thumb, isLocal);
},
[OB11MessageDataType.voice]: async (sendMsg, context) =>
@@ -1255,13 +1259,13 @@ export class OneBotMsgApi {
const totalSize = sendElements.reduce((total, element) => {
switch (element.elementType) {
case ElementType.PTT:
return total + (parseInt(element.pttElement.fileSize) || 0);
return total + (+(element.pttElement.fileSize || '3000'));
case ElementType.FILE:
return total + (parseInt(element.fileElement.fileSize) || 0);
return total + (+(element.fileElement.fileSize || '3000'));
case ElementType.VIDEO:
return total + (parseInt(element.videoElement.fileSize) || 0);
return total + (+(element.videoElement.fileSize || '3000'));
case ElementType.PIC:
return total + (parseInt(element.picElement.fileSize) || 0);
return total + (+(element.picElement.fileSize || '3000'));
default:
return total;
}
@@ -1307,13 +1311,15 @@ export class OneBotMsgApi {
realUri = await this.handleObfuckName(realUri) ?? realUri;
try {
const proxy = this.obContext.configLoader.configData.imageDownloadProxy || undefined;
const { path, fileName, errMsg, success } = await uriToLocalFile(this.core.NapCatTempPath, realUri, undefined, undefined, proxy);
const { path, fileName, errMsg, success, isLocal } = await uriToLocalFile(this.core.NapCatTempPath, realUri, undefined, undefined, proxy);
if (!success) {
this.core.context.logger.logError('文件处理失败', errMsg);
throw new Error('文件处理失败: ' + errMsg);
}
deleteAfterSentFiles.push(path);
return { path, fileName: inputdata.name ?? fileName };
if (!isLocal) {
deleteAfterSentFiles.push(path);
}
return { path, fileName: inputdata.name ?? fileName, isLocal };
} catch (e: unknown) {
throw new Error((e as Error).message);
}