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

@@ -40,6 +40,7 @@ export class SendGroupNotice extends OneBotAction<SendGroupNoticePayload, void>
const {
path,
success,
isLocal,
} = (await uriToLocalFile(this.core.NapCatTempPath, payload.image));
if (!success) {
throw new Error(`群公告${payload.image}设置失败,image字段可能格式不正确`);
@@ -53,7 +54,7 @@ export class SendGroupNotice extends OneBotAction<SendGroupNoticePayload, void>
throw new Error(`群公告${payload.image}设置失败,图片上传失败 , 错误信息:${ImageUploadResult.errMsg}`);
}
unlink(path).catch(() => { });
if (!isLocal) unlink(path).catch(() => { });
UploadImage = ImageUploadResult.picInfo;
}
@@ -30,14 +30,14 @@ export default class SetGroupPortrait extends OneBotAction<SetGroupPortraitPaylo
override returnExample = GoCQHTTPActionsExamples.SetGroupPortrait.response;
async _handle (payload: SetGroupPortraitPayload): Promise<ReturnType> {
const { path, success } = (await uriToLocalFile(this.core.NapCatTempPath, payload.file));
const { path, success, isLocal } = (await uriToLocalFile(this.core.NapCatTempPath, payload.file));
if (!success) {
throw new Error(`头像${payload.file}设置失败,file字段可能格式不正确`);
}
if (path) {
await checkFileExistV2(path, 5000); // 文件不存在QQ会崩溃,需要提前判断
const ret = await this.core.apis.GroupApi.setGroupAvatar(payload.group_id.toString(), path);
fs.unlink(path).catch(() => { });
if (!isLocal) fs.unlink(path).catch(() => { });
if (!ret) {
throw new Error(`头像${payload.file}设置失败,api无返回`);
}
@@ -51,7 +51,6 @@ export default class SetGroupPortrait extends OneBotAction<SetGroupPortraitPaylo
errMsg: ret.errMsg,
};
} else {
fs.unlink(path).catch(() => { });
throw new Error(`头像${payload.file}设置失败,无法获取头像,文件可能不存在`);
}
}
@@ -51,8 +51,10 @@ export default class GoCQHTTPUploadGroupFile extends OneBotAction<GoCQHTTPUpload
peer,
deleteAfterSentFiles: [],
};
const sendFileEle = await this.obContext.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name, payload.folder ?? payload.folder_id, payload.upload_file);
msgContext.deleteAfterSentFiles.push(downloadResult.path);
const sendFileEle = await this.obContext.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name, payload.folder ?? payload.folder_id, payload.upload_file, downloadResult.isLocal);
if (!downloadResult.isLocal) {
msgContext.deleteAfterSentFiles.push(downloadResult.path);
}
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, [sendFileEle], msgContext.deleteAfterSentFiles);
const fileElement = returnMsg.elements.find(ele => ele.elementType === ElementType.FILE);
@@ -61,8 +61,10 @@ export default class GoCQHTTPUploadPrivateFile extends OneBotAction<GoCQHTTPUplo
}, ContextMode.Private),
deleteAfterSentFiles: [],
};
const sendFileEle: SendFileElement = await this.obContext.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name, '', payload.upload_file);
msgContext.deleteAfterSentFiles.push(downloadResult.path);
const sendFileEle: SendFileElement = await this.obContext.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name, '', payload.upload_file, downloadResult.isLocal);
if (!downloadResult.isLocal) {
msgContext.deleteAfterSentFiles.push(downloadResult.path);
}
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], msgContext.deleteAfterSentFiles);
const fileElement = returnMsg.elements.find(ele => ele.elementType === ElementType.FILE);