From 95bd74bb0d2511185baeccaadc57e76cdac57863 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:18:38 +0800 Subject: [PATCH] BUILD: TEST --- src/common/file.ts | 28 +++++++++++-------- src/core/apis/file.ts | 2 +- src/core/entities/msg.ts | 2 +- src/onebot/action/extends/OCRImage.ts | 13 +++------ src/onebot/action/extends/SetQQAvatar.ts | 15 ++++------ .../action/go-cqhttp/SendGroupNotice.ts | 9 +++--- .../action/go-cqhttp/SetGroupPortrait.ts | 12 ++------ src/onebot/api/msg.ts | 5 +--- 8 files changed, 36 insertions(+), 50 deletions(-) diff --git a/src/common/file.ts b/src/common/file.ts index b918c4d6..33276de3 100644 --- a/src/common/file.ts +++ b/src/common/file.ts @@ -160,8 +160,7 @@ type Uri2LocalRes = { errMsg: string, fileName: string, ext: string, - path: string, - isLocal: boolean + path: string } export async function checkFileV2(filePath: string) { @@ -194,7 +193,6 @@ export async function checkUriType(Uri: string) { return undefined; }, Uri); if (LocalFileRet) return LocalFileRet; - const OtherFileRet = await solveProblem((uri: string) => { //再判断是否是Http if (uri.startsWith('http://') || uri.startsWith('https://')) { @@ -206,13 +204,13 @@ export async function checkUriType(Uri: string) { } if (uri.startsWith('file://')) { let filePath: string; - // await fs.copyFile(url.pathname, filePath); const pathname = decodeURIComponent(new URL(uri).pathname); if (process.platform === 'win32') { filePath = pathname.slice(1); } else { filePath = pathname; } + return { Uri: filePath, Type: FileUriType.Local }; } if (uri.startsWith('data:')) { @@ -228,20 +226,26 @@ export async function checkUriType(Uri: string) { export async function uri2local(dir: string, uri: string, filename: string | undefined = undefined): Promise { const { Uri: HandledUri, Type: UriType } = await checkUriType(uri); //解析失败 + const tempName = randomUUID(); + if (!filename) filename = randomUUID(); + //解析Http和Https协议 if (UriType == FileUriType.Unknown) { - return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '', isLocal: false }; + return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '' }; } //解析File协议和本地文件 if (UriType == FileUriType.Local) { const fileExt = path.extname(HandledUri); let filename = path.basename(HandledUri, fileExt); filename += fileExt; - return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: HandledUri, isLocal: true }; + //复制文件到临时文件并保持后缀 + const filenameTemp = tempName + fileExt; + const filePath = path.join(dir, filenameTemp); + fs.copyFileSync(HandledUri, filePath); + console.log('复制文件到临时文件', HandledUri, filePath); + return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath }; } //接下来都要有文件名 - if (!filename) filename = randomUUID(); - //解析Http和Https协议 if (UriType == FileUriType.Remote) { const pathInfo = path.parse(decodeURIComponent(new URL(HandledUri).pathname)); @@ -253,10 +257,10 @@ export async function uri2local(dir: string, uri: string, filename: string | und } filename = filename.replace(/[/\\:*?"<>|]/g, '_'); const fileExt = path.extname(HandledUri); - const filePath = path.join(dir, filename); + const filePath = path.join(dir, tempName + fileExt); const buffer = await httpDownload(HandledUri); fs.writeFileSync(filePath, buffer); - return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath, isLocal: false }; + return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath }; } //解析Base64 if (UriType == FileUriType.Base64) { @@ -271,7 +275,7 @@ export async function uri2local(dir: string, uri: string, filename: string | und fileExt = ext; filename = filename + '.' + ext; } - return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath, isLocal: false }; + return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath }; } - return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '', isLocal: false }; + return { success: false, errMsg: '未知文件类型', fileName: '', ext: '', path: '' }; } diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index de01aaec..44016ace 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -71,7 +71,7 @@ export class NTQQFileApi { file_uuid: '', }); - await this.copyFile(filePath, mediaPath!); + await this.copyFile(filePath, mediaPath); const fileSize = await this.getFileSize(filePath); return { md5: fileMd5, diff --git a/src/core/entities/msg.ts b/src/core/entities/msg.ts index 0f05abca..f136cc0b 100644 --- a/src/core/entities/msg.ts +++ b/src/core/entities/msg.ts @@ -1,4 +1,4 @@ -import { GroupMemberRole, Peer } from '@/core'; +import { GroupMemberRole } from '@/core'; export interface Peer { chatType: ChatType; diff --git a/src/onebot/action/extends/OCRImage.ts b/src/onebot/action/extends/OCRImage.ts index 4a9a033b..ecd26435 100644 --- a/src/onebot/action/extends/OCRImage.ts +++ b/src/onebot/action/extends/OCRImage.ts @@ -19,26 +19,21 @@ export class OCRImage extends BaseAction { payloadSchema = SchemaData; async _handle(payload: Payload) { - const { path, isLocal, success } = (await uri2local(this.core.NapCatTempPath, payload.image)); + const { path, success } = (await uri2local(this.core.NapCatTempPath, payload.image)); if (!success) { throw `OCR ${payload.image}失败,image字段可能格式不正确`; } if (path) { await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃,需要提前判断 const ret = await this.core.apis.SystemApi.ocrImage(path); - if (!isLocal) { - fs.unlink(path, () => { - }); - } + fs.unlink(path, () => { }); + if (!ret) { throw `OCR ${payload.file}失败`; } return ret.result; } - if (!isLocal) { - fs.unlink(path, () => { - }); - } + fs.unlink(path, () => { }); throw `OCR ${payload.file}失败,文件可能不存在`; } } diff --git a/src/onebot/action/extends/SetQQAvatar.ts b/src/onebot/action/extends/SetQQAvatar.ts index 735cfd3a..c75d82f6 100644 --- a/src/onebot/action/extends/SetQQAvatar.ts +++ b/src/onebot/action/extends/SetQQAvatar.ts @@ -24,17 +24,16 @@ export default class SetAvatar extends BaseAction { } async _handle(payload: Payload): Promise { - const { path, isLocal, success } = (await uri2local(this.core.NapCatTempPath, payload.file)); + const { path, success } = (await uri2local(this.core.NapCatTempPath, payload.file)); if (!success) { throw `头像${payload.file}设置失败,file字段可能格式不正确`; } if (path) { await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃,需要提前判断 const ret = await this.core.apis.UserApi.setQQAvatar(path); - if (!isLocal) { - fs.unlink(path, () => { - }); - } + fs.unlink(path, () => { + }); + if (!ret) { throw `头像${payload.file}设置失败,api无返回`; } @@ -45,10 +44,8 @@ export default class SetAvatar extends BaseAction { throw `头像${payload.file}设置失败,未知的错误,${ret['result']}:${ret['errMsg']}`; } } else { - if (!isLocal) { - fs.unlink(path, () => { - }); - } + fs.unlink(path, () => { }); + throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`; } return null; diff --git a/src/onebot/action/go-cqhttp/SendGroupNotice.ts b/src/onebot/action/go-cqhttp/SendGroupNotice.ts index 74833007..a4b5f7ee 100644 --- a/src/onebot/action/go-cqhttp/SendGroupNotice.ts +++ b/src/onebot/action/go-cqhttp/SendGroupNotice.ts @@ -31,7 +31,6 @@ export class SendGroupNotice extends BaseAction { //公告图逻辑 const { path, - isLocal, success, } = (await uri2local(this.core.NapCatTempPath, payload.image)); if (!success) { @@ -45,10 +44,10 @@ export class SendGroupNotice extends BaseAction { if (ImageUploadResult.errCode != 0) { throw `群公告${payload.image}设置失败,图片上传失败`; } - if (!isLocal) { - unlink(path, () => { - }); - } + + unlink(path, () => { + }); + UploadImage = ImageUploadResult.picInfo; } diff --git a/src/onebot/action/go-cqhttp/SetGroupPortrait.ts b/src/onebot/action/go-cqhttp/SetGroupPortrait.ts index 9243bfd3..c7b363d1 100644 --- a/src/onebot/action/go-cqhttp/SetGroupPortrait.ts +++ b/src/onebot/action/go-cqhttp/SetGroupPortrait.ts @@ -25,17 +25,14 @@ export default class SetGroupPortrait extends BaseAction { } async _handle(payload: Payload): Promise { - const { path, isLocal, success } = (await uri2local(this.core.NapCatTempPath, payload.file)); + const { path, success } = (await uri2local(this.core.NapCatTempPath, payload.file)); if (!success) { throw `头像${payload.file}设置失败,file字段可能格式不正确`; } if (path) { await checkFileReceived(path, 5000); // 文件不存在QQ会崩溃,需要提前判断 const ret = await this.core.apis.GroupApi.setGroupAvatar(payload.group_id.toString(), path) as any; - if (!isLocal) { - fs.unlink(path, () => { - }); - } + fs.unlink(path, () => { }); if (!ret) { throw `头像${payload.file}设置失败,api无返回`; } @@ -46,10 +43,7 @@ export default class SetGroupPortrait extends BaseAction { } return ret; } else { - if (!isLocal) { - fs.unlink(path, () => { - }); - } + fs.unlink(path, () => {}); throw `头像${payload.file}设置失败,无法获取头像,文件可能不存在`; } } diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index 2a55cf4d..40338c85 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -805,7 +805,6 @@ export class OneBotMsgApi { } const { path, - isLocal, fileName, errMsg, success, @@ -816,9 +815,7 @@ export class OneBotMsgApi { throw Error('文件下载失败' + errMsg); } - if (!isLocal) { // 只删除http和base64转过来的文件 - deleteAfterSentFiles.push(path); - } + deleteAfterSentFiles.push(path); return { path, fileName: inputdata.name ?? fileName }; }