From 374fc64427187bf44f018ac84438bd8e4c639dab 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: Wed, 11 Sep 2024 23:29:26 +0800 Subject: [PATCH] feat: delFile --- src/core/apis/file.ts | 10 +++++++--- src/onebot/action/go-cqhttp/UploadGroupFile.ts | 18 ++++++++++++------ src/onebot/api/msg.ts | 5 +++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index 44016ace..4436bfe8 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -24,6 +24,7 @@ import pathLib from 'node:path'; import { defaultVideoThumbB64, getVideoInfo } from '@/common/video'; import ffmpeg from 'fluent-ffmpeg'; import { encodeSilk } from '@/common/audio'; +import { MessageContext } from '@/onebot/api'; export class NTQQFileApi { context: InstanceContext; @@ -82,7 +83,7 @@ export class NTQQFileApi { }; } - async createValidSendFileElement(filePath: string, fileName: string = '', folderId: string = '',): Promise { + async createValidSendFileElement(context: MessageContext, filePath: string, fileName: string = '', folderId: string = '',): Promise { const { fileName: _fileName, path, @@ -91,6 +92,7 @@ export class NTQQFileApi { if (fileSize === 0) { throw new Error('文件异常,大小为0'); } + context.deleteAfterSentFiles.push(path); return { elementType: ElementType.FILE, elementId: '', @@ -103,12 +105,13 @@ export class NTQQFileApi { }; } - async createValidSendPicElement(picPath: string, summary: string = '', subType: 0 | 1 = 0,): Promise { + async createValidSendPicElement(context: MessageContext, picPath: string, summary: string = '', subType: 0 | 1 = 0,): Promise { const { md5, fileName, path, fileSize } = await this.core.apis.FileApi.uploadFile(picPath, ElementType.PIC, subType); if (fileSize === 0) { throw new Error('文件异常,大小为0'); } const imageSize = await this.core.apis.FileApi.getImageSize(picPath); + context.deleteAfterSentFiles.push(path); return { elementType: ElementType.PIC, elementId: '', @@ -130,7 +133,7 @@ export class NTQQFileApi { }; } - async createValidSendVideoElement(filePath: string, fileName: string = '', diyThumbPath: string = ''): Promise { + async createValidSendVideoElement(context: MessageContext, filePath: string, fileName: string = '', diyThumbPath: string = ''): Promise { const logger = this.core.context.logger; const { fileName: _fileName, path, fileSize, md5 } = await this.core.apis.FileApi.uploadFile(filePath, ElementType.VIDEO); if (fileSize === 0) { @@ -179,6 +182,7 @@ export class NTQQFileApi { const thumbSize = _thumbPath ? (await fsPromises.stat(_thumbPath)).size : 0; thumbPath.set(0, _thumbPath); const thumbMd5 = _thumbPath ? await calculateFileMD5(_thumbPath) : ''; + context.deleteAfterSentFiles.push(path); return { elementType: ElementType.VIDEO, elementId: '', diff --git a/src/onebot/action/go-cqhttp/UploadGroupFile.ts b/src/onebot/action/go-cqhttp/UploadGroupFile.ts index 4f0f9111..755ca2d0 100644 --- a/src/onebot/action/go-cqhttp/UploadGroupFile.ts +++ b/src/onebot/action/go-cqhttp/UploadGroupFile.ts @@ -1,9 +1,10 @@ import BaseAction from '../BaseAction'; import { ActionName } from '../types'; -import { ChatType } from '@/core/entities'; +import { ChatType, Peer } from '@/core/entities'; import fs from 'fs'; import { uri2local } from '@/common/file'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; +import { MessageContext } from '@/onebot/api'; const SchemaData = { type: 'object', @@ -29,14 +30,19 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction { file = `file://${file}`; } const downloadResult = await uri2local(this.core.NapCatTempPath, file); + const peer: Peer = { + chatType: ChatType.KCHATTYPEGROUP, + peerUid: payload.group_id.toString(), + }; if (!downloadResult.success) { throw new Error(downloadResult.errMsg); } - const sendFileEle = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name, payload.folder_id); - await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId({ - chatType: ChatType.KCHATTYPEGROUP, - peerUid: payload.group_id.toString(), - }, [sendFileEle], [], true); + let msgContext: MessageContext = { + peer: peer, + deleteAfterSentFiles: [] + } + const sendFileEle = await this.core.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name, payload.folder_id); + await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, [sendFileEle], [], true); return null; } } diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 40338c85..ce4b8e9b 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -460,6 +460,7 @@ export class OneBotMsgApi { // File service [OB11MessageDataType.image]: async (sendMsg, context) => { const sendPicElement = await this.core.apis.FileApi.createValidSendPicElement( + context, (await this.handleOb11FileLikeMessage(sendMsg, context)).path, sendMsg.data.summary, sendMsg.data.sub_type, @@ -470,7 +471,7 @@ export class OneBotMsgApi { [OB11MessageDataType.file]: async (sendMsg, context) => { const { path, fileName } = await this.handleOb11FileLikeMessage(sendMsg, context); - return await this.core.apis.FileApi.createValidSendFileElement(path, fileName); + return await this.core.apis.FileApi.createValidSendFileElement(context, path, fileName); }, [OB11MessageDataType.video]: async (sendMsg, context) => { @@ -481,7 +482,7 @@ export class OneBotMsgApi { const uri2LocalRes = await uri2local(this.core.NapCatTempPath, thumb); if (uri2LocalRes.success) thumb = uri2LocalRes.path; } - const videoEle = await this.core.apis.FileApi.createValidSendVideoElement(path, fileName, thumb); + const videoEle = await this.core.apis.FileApi.createValidSendVideoElement(context, path, fileName, thumb); context.deleteAfterSentFiles.push(videoEle.videoElement.filePath); return videoEle;