diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index aa847e07..b27f7335 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -181,7 +181,7 @@ type Uri2LocalRes = { isLocal: boolean } -export async function uri2local(uri: string, fileName: string | null = null): Promise { +export async function uri2local(UriOrPath: string, fileName: string | null = null): Promise { const res = { success: false, errMsg: '', @@ -190,26 +190,29 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr path: '', isLocal: false }; - if (!fileName) { - fileName = randomUUID(); - } - let filePath = path.join(getTempDir(), fileName); + if (!fileName) fileName = randomUUID(); + let filePath = path.join(getTempDir(), fileName);//临时目录 let url = null; + //区分path和uri try { - url = new URL(uri); - } catch (e: any) { - res.errMsg = `uri ${uri} 解析失败,` + e.toString() + ` 可能${uri}不存在`; + if (fs.existsSync(UriOrPath)) url = new URL("file://" + UriOrPath); + } catch (error: any) { } + try { + url = new URL(UriOrPath); + } catch (error: any) { } + + //验证url + if (!url) { + res.errMsg = `UriOrPath ${UriOrPath} 解析失败,可能${UriOrPath}不存在`; return res; } - // log("uri protocol", url.protocol, uri); if (url.protocol == 'base64:') { // base64转成文件 - const base64Data = uri.split('base64://')[1]; + const base64Data = UriOrPath.split('base64://')[1]; try { const buffer = Buffer.from(base64Data, 'base64'); fs.writeFileSync(filePath, buffer); - } catch (e: any) { res.errMsg = 'base64文件下载失败,' + e.toString(); return res; @@ -218,7 +221,7 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr // 下载文件 let buffer: Buffer | null = null; try { - buffer = await httpDownload(uri); + buffer = await httpDownload(UriOrPath); } catch (e: any) { res.errMsg = `${url}下载失败,` + e.toString(); return res; @@ -252,6 +255,7 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr } } else { + // 26702执行forword file文件操作 不应该在这里乱来 // const cache = await dbUtil.getFileCacheByName(uri); // if (cache) { // filePath = cache.path; @@ -259,7 +263,6 @@ export async function uri2local(uri: string, fileName: string | null = null): Pr // filePath = uri; // } } - res.isLocal = true; } // else{ diff --git a/src/onebot11/action/extends/OCRImage.ts b/src/onebot11/action/extends/OCRImage.ts index d0cc6d05..8128fe4e 100644 --- a/src/onebot11/action/extends/OCRImage.ts +++ b/src/onebot11/action/extends/OCRImage.ts @@ -20,8 +20,8 @@ export class OCRImage extends BaseAction { actionName = ActionName.OCRImage; PayloadSchema = SchemaData; protected async _handle(payload: Payload) { - const { path, isLocal, errMsg } = (await uri2local(payload.image)); - if (errMsg) { + const { path, isLocal, errMsg,success } = (await uri2local(payload.image)); + if (!success) { throw `OCR ${payload.image}失败,image字段可能格式不正确`; } if (path) { diff --git a/src/onebot11/action/extends/SetGroupHeader.ts b/src/onebot11/action/extends/SetGroupHeader.ts index 3a5dd8d2..1998301b 100644 --- a/src/onebot11/action/extends/SetGroupHeader.ts +++ b/src/onebot11/action/extends/SetGroupHeader.ts @@ -26,8 +26,8 @@ export default class SetGroupHeader extends BaseAction { }; } protected async _handle(payload: Payload): Promise { - const { path, isLocal, errMsg } = (await uri2local(payload.file)); - if (errMsg) { + const { path, isLocal, errMsg,success } = (await uri2local(payload.file)); + if (!success) { throw `头像${payload.file}设置失败,file字段可能格式不正确`; } if (path) { diff --git a/src/onebot11/action/extends/SetQQAvatar.ts b/src/onebot11/action/extends/SetQQAvatar.ts index ad0553fd..f8006344 100644 --- a/src/onebot11/action/extends/SetQQAvatar.ts +++ b/src/onebot11/action/extends/SetQQAvatar.ts @@ -24,8 +24,8 @@ export default class SetAvatar extends BaseAction { }; } protected async _handle(payload: Payload): Promise { - const { path, isLocal, errMsg } = (await uri2local(payload.file)); - if (errMsg) { + const { path, isLocal, errMsg,success } = (await uri2local(payload.file)); + if (!success) { throw `头像${payload.file}设置失败,file字段可能格式不正确`; } if (path) { diff --git a/src/onebot11/action/go-cqhttp/SendGroupNotice.ts b/src/onebot11/action/go-cqhttp/SendGroupNotice.ts index 28e0d613..4be85a83 100644 --- a/src/onebot11/action/go-cqhttp/SendGroupNotice.ts +++ b/src/onebot11/action/go-cqhttp/SendGroupNotice.ts @@ -7,7 +7,7 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; const SchemaData = { type: 'object', properties: { - group_id: { type: [ 'number' , 'string' ] }, + group_id: { type: ['number', 'string'] }, content: { type: 'string' }, image: { type: 'string' }, pinned: { type: 'number' }, @@ -24,8 +24,8 @@ export class SendGroupNotice extends BaseAction { let UploadImage: { id: string, width: number, height: number } | undefined = undefined; if (payload.image) { //公告图逻辑 - const { errMsg, path, isLocal } = (await uri2local(payload.image)); - if (errMsg) { + const { errMsg, path, isLocal, success } = (await uri2local(payload.image)); + if (!success) { throw `群公告${payload.image}设置失败,image字段可能格式不正确`; } if (!path) { diff --git a/src/onebot11/action/go-cqhttp/UploadGroupFile.ts b/src/onebot11/action/go-cqhttp/UploadGroupFile.ts index a3330b72..d00895db 100644 --- a/src/onebot11/action/go-cqhttp/UploadGroupFile.ts +++ b/src/onebot11/action/go-cqhttp/UploadGroupFile.ts @@ -34,7 +34,7 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction { file = `file://${file}`; } const downloadResult = await uri2local(file); - if (downloadResult.errMsg) { + if (!downloadResult.success) { throw new Error(downloadResult.errMsg); } const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name, payload.folder_id); diff --git a/src/onebot11/action/go-cqhttp/UploadPrivareFile.ts b/src/onebot11/action/go-cqhttp/UploadPrivareFile.ts index 456ee1dd..853ca721 100644 --- a/src/onebot11/action/go-cqhttp/UploadPrivareFile.ts +++ b/src/onebot11/action/go-cqhttp/UploadPrivareFile.ts @@ -41,7 +41,7 @@ export default class GoCQHTTPUploadPrivateFile extends BaseAction file = `file://${file}`; } const downloadResult = await uri2local(file); - if (downloadResult.errMsg) { + if (!downloadResult.success) { throw new Error(downloadResult.errMsg); } const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name); diff --git a/src/onebot11/action/msg/SendMsg/create-send-elements.ts b/src/onebot11/action/msg/SendMsg/create-send-elements.ts index 6bb22514..92358791 100644 --- a/src/onebot11/action/msg/SendMsg/create-send-elements.ts +++ b/src/onebot11/action/msg/SendMsg/create-send-elements.ts @@ -27,9 +27,9 @@ async function handleOb11FileLikeMessage( { deleteAfterSentFiles }: MessageContext ) { //有的奇怪的框架将url作为参数 而不是file 此时优先url - const { path, isLocal, fileName, errMsg } = (await uri2local(inputdata?.url || inputdata.file)); + const { path, isLocal, fileName, errMsg,success } = (await uri2local(inputdata?.url || inputdata.file)); - if (errMsg) { + if (!success) { logError('文件下载失败', errMsg); throw Error('文件下载失败' + errMsg); }