From 91e0839ed553fca10d45a185a91055a1e0f79a93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 3 Jan 2026 16:25:38 +0800 Subject: [PATCH] Add upload_file option for file upload actions Introduces an 'upload_file' boolean option to group and private file upload actions, allowing control over whether files are uploaded to group storage or sent directly. Updates the NTQQFileApi and OneBotFileApi to support this option and adjusts file handling logic accordingly. --- packages/napcat-core/apis/file.ts | 41 +++++++++++-------- .../action/go-cqhttp/UploadGroupFile.ts | 3 +- .../action/go-cqhttp/UploadPrivateFile.ts | 3 +- packages/napcat-onebot/api/file.ts | 8 ++-- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/packages/napcat-core/apis/file.ts b/packages/napcat-core/apis/file.ts index e45ebf70..a7acd3bc 100644 --- a/packages/napcat-core/apis/file.ts +++ b/packages/napcat-core/apis/file.ts @@ -33,7 +33,7 @@ export class NTQQFileApi { 'http://ss.xingzhige.com/music_card/rkey', 'https://secret-service.bietiaop.com/rkeys', ], - this.context.logger + this.context.logger ); } @@ -138,7 +138,7 @@ export class NTQQFileApi { })).urlResult.domainUrl; } - async uploadFile (filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0) { + async uploadFile (filePath: string, elementType: ElementType = ElementType.PIC, elementSubType: number = 0, uploadGroupFile = true) { const fileMd5 = await calculateFileMD5(filePath); const extOrEmpty = await fileTypeFromFile(filePath).then(e => e?.ext ?? '').catch(() => ''); const ext = extOrEmpty ? `.${extOrEmpty}` : ''; @@ -146,24 +146,33 @@ export class NTQQFileApi { if (fileName.indexOf('.') === -1) { fileName += ext; } - - const mediaPath = this.context.session.getMsgService().getRichMediaFilePathForGuild({ - md5HexStr: fileMd5, - fileName, - elementType, - elementSubType, - thumbSize: 0, - needCreate: true, - downloadType: 1, - file_uuid: '', - }); - - await this.copyFile(filePath, mediaPath); const fileSize = await this.getFileSize(filePath); + if (uploadGroupFile) { + const mediaPath = this.context.session.getMsgService().getRichMediaFilePathForGuild({ + md5HexStr: fileMd5, + fileName, + elementType, + elementSubType, + thumbSize: 0, + needCreate: true, + downloadType: 1, + file_uuid: '', + }); + + await this.copyFile(filePath, mediaPath); + + return { + md5: fileMd5, + fileName, + path: mediaPath, + fileSize, + ext, + }; + } return { md5: fileMd5, fileName, - path: mediaPath, + path: filePath, fileSize, ext, }; diff --git a/packages/napcat-onebot/action/go-cqhttp/UploadGroupFile.ts b/packages/napcat-onebot/action/go-cqhttp/UploadGroupFile.ts index d9d09d5c..11bfe4b9 100644 --- a/packages/napcat-onebot/action/go-cqhttp/UploadGroupFile.ts +++ b/packages/napcat-onebot/action/go-cqhttp/UploadGroupFile.ts @@ -12,6 +12,7 @@ const SchemaData = Type.Object({ name: Type.String(), folder: Type.Optional(Type.String()), folder_id: Type.Optional(Type.String()), // 临时扩展 + upload_file: Type.Boolean({ default: true }), }); type Payload = Static; @@ -41,7 +42,7 @@ export default class GoCQHTTPUploadGroupFile extends OneBotAction; @@ -51,7 +52,7 @@ export default class GoCQHTTPUploadPrivateFile extends OneBotAction { + async createValidSendFileElement (context: SendMessageContext, filePath: string, fileName: string = '', folderId: string = '', uploadGroupFile: boolean = false): Promise { const { fileName: _fileName, path, fileSize, - } = await this.core.apis.FileApi.uploadFile(filePath, ElementType.FILE); + } = await this.core.apis.FileApi.uploadFile(filePath, ElementType.FILE, 0, uploadGroupFile); if (fileSize === 0) { throw new Error('文件异常,大小为0'); } - context.deleteAfterSentFiles.push(path); + if (uploadGroupFile) { + context.deleteAfterSentFiles.push(path); + } return { elementType: ElementType.FILE, elementId: '',