From 82608dd5ff47153cf5a7e23ae3203d646e86ca67 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: Thu, 12 Sep 2024 00:17:35 +0800 Subject: [PATCH] fix: build --- src/common/file.ts | 9 ++++++--- src/core/apis/file.ts | 21 +++++++++++++-------- src/onebot/api/msg.ts | 2 -- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/common/file.ts b/src/common/file.ts index 4e239acc..e93b1932 100644 --- a/src/common/file.ts +++ b/src/common/file.ts @@ -250,16 +250,19 @@ export async function uri2local(dir: string, uri: string, filename: string | und if (UriType == FileUriType.Remote) { const pathInfo = path.parse(decodeURIComponent(new URL(HandledUri).pathname)); if (pathInfo.name) { - filename = pathInfo.name.substring(0, 50);//过长截断 + let pathlen = 200 - dir.length - pathInfo.name.length; + filename = pathlen > 0 ? pathInfo.name.substring(0, pathlen) : pathInfo.name.substring(pathInfo.name.length, pathInfo.name.length - 10);//过长截断 if (pathInfo.ext) { filename += pathInfo.ext; } } filename = filename.replace(/[/\\:*?"<>|]/g, '_'); - const fileExt = path.extname(HandledUri); + const fileExt = path.extname(HandledUri).replace(/[/\\:*?"<>|]/g, '_').substring(0, 10); const filePath = path.join(dir, tempName + fileExt); const buffer = await httpDownload(HandledUri); - fs.writeFileSync(filePath, buffer); + //fs.writeFileSync(filePath, buffer); + //没有文件就创建 + fs.writeFileSync(filePath, buffer, { flag: 'wx' }); return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath }; } //解析Base64 diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index 4436bfe8..a5a4f167 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -135,24 +135,29 @@ export class NTQQFileApi { 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) { - throw new Error('文件异常,大小为0'); - } - let thumb = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`); - thumb = pathLib.dirname(thumb); let videoInfo = { width: 1920, height: 1080, time: 15, format: 'mp4', - size: fileSize, + size: 0, filePath, }; try { - videoInfo = await getVideoInfo(path, logger); + videoInfo = await getVideoInfo(filePath, logger); } catch (e) { logger.logError('获取视频信息失败,将使用默认值', e); } + let newFilePath = filePath + '.mp4'; + fs.renameSync(filePath, newFilePath); + filePath = newFilePath; + const { fileName: _fileName, path, fileSize, md5 } = await this.core.apis.FileApi.uploadFile(filePath, ElementType.VIDEO); + if (fileSize === 0) { + throw new Error('文件异常,大小为0'); + } + videoInfo.size = fileSize; + let thumb = path.replace(`${pathLib.sep}Ori${pathLib.sep}`, `${pathLib.sep}Thumb${pathLib.sep}`); + thumb = pathLib.dirname(thumb); + const thumbPath = new Map(); const _thumbPath = await new Promise((resolve, reject) => { const thumbFileName = `${md5}_0.png`; diff --git a/src/onebot/api/msg.ts b/src/onebot/api/msg.ts index ce4b8e9b..a2033539 100644 --- a/src/onebot/api/msg.ts +++ b/src/onebot/api/msg.ts @@ -483,8 +483,6 @@ export class OneBotMsgApi { if (uri2LocalRes.success) thumb = uri2LocalRes.path; } const videoEle = await this.core.apis.FileApi.createValidSendVideoElement(context, path, fileName, thumb); - - context.deleteAfterSentFiles.push(videoEle.videoElement.filePath); return videoEle; },