diff --git a/src/common/file.ts b/src/common/file.ts index 3166aa64..5fa19f05 100644 --- a/src/common/file.ts +++ b/src/common/file.ts @@ -19,14 +19,6 @@ type Uri2LocalRes = { path: string } -export function isGIF(path: string) { - const buffer = Buffer.alloc(4); - const fd = fs.openSync(path, 'r'); - fs.readSync(fd, buffer, 0, 4, 0); - fs.closeSync(fd); - return buffer.toString() === 'GIF8'; -} - // 定义一个异步函数来检查文件是否存在 export function checkFileExist(path: string, timeout: number = 3000): Promise { return new Promise((resolve, reject) => { diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index 5040ebc7..4b54f8e5 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -21,12 +21,13 @@ import * as fileType from 'file-type'; import imageSize from 'image-size'; import { ISizeCalculationResult } from 'image-size/dist/types/interface'; import { RkeyManager } from '@/core/helper/rkey'; -import { calculateFileMD5, isGIF } from '@/common/file'; +import { calculateFileMD5 } from '@/common/file'; 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'; +import { getFileTypeForSendType } from '../helper/msg'; export class NTQQFileApi { context: InstanceContext; @@ -130,7 +131,7 @@ export class NTQQFileApi { fileName: fileName, sourcePath: path, original: true, - picType: isGIF(picPath) ? PicType.NEWPIC_GIF : PicType.NEWPIC_JPEG, + picType: await getFileTypeForSendType(picPath), picSubType: subType, fileUuid: '', fileSubId: '', diff --git a/src/core/helper/msg.ts b/src/core/helper/msg.ts new file mode 100644 index 00000000..dd910bef --- /dev/null +++ b/src/core/helper/msg.ts @@ -0,0 +1,14 @@ +import * as fileType from 'file-type'; +import { PicType } from '../types'; +export async function getFileTypeForSendType(picPath: string): Promise { + const fileTypeResult = (await fileType.fileTypeFromFile(picPath))?.ext ?? 'jpg'; + const picTypeMap: { [key: string]: PicType } = { + 'webp': PicType.NEWPIC_WEBP, + 'gif': PicType.NEWPIC_GIF, + 'png': PicType.NEWPIC_APNG, + 'jpg': PicType.NEWPIC_JPEG, + 'jpeg': PicType.NEWPIC_JPEG, + 'bmp': PicType.NEWPIC_BMP, + }; + return picTypeMap[fileTypeResult] ?? PicType.NEWPIC_JPEG; +} \ No newline at end of file