mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: filetype 识别
This commit is contained in:
@@ -19,14 +19,6 @@ type Uri2LocalRes = {
|
|||||||
path: string
|
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<void> {
|
export function checkFileExist(path: string, timeout: number = 3000): Promise<void> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
@@ -21,12 +21,13 @@ import * as fileType from 'file-type';
|
|||||||
import imageSize from 'image-size';
|
import imageSize from 'image-size';
|
||||||
import { ISizeCalculationResult } from 'image-size/dist/types/interface';
|
import { ISizeCalculationResult } from 'image-size/dist/types/interface';
|
||||||
import { RkeyManager } from '@/core/helper/rkey';
|
import { RkeyManager } from '@/core/helper/rkey';
|
||||||
import { calculateFileMD5, isGIF } from '@/common/file';
|
import { calculateFileMD5 } from '@/common/file';
|
||||||
import pathLib from 'node:path';
|
import pathLib from 'node:path';
|
||||||
import { defaultVideoThumbB64, getVideoInfo } from '@/common/video';
|
import { defaultVideoThumbB64, getVideoInfo } from '@/common/video';
|
||||||
import ffmpeg from 'fluent-ffmpeg';
|
import ffmpeg from 'fluent-ffmpeg';
|
||||||
import { encodeSilk } from '@/common/audio';
|
import { encodeSilk } from '@/common/audio';
|
||||||
import { MessageContext } from '@/onebot/api';
|
import { MessageContext } from '@/onebot/api';
|
||||||
|
import { getFileTypeForSendType } from '../helper/msg';
|
||||||
|
|
||||||
export class NTQQFileApi {
|
export class NTQQFileApi {
|
||||||
context: InstanceContext;
|
context: InstanceContext;
|
||||||
@@ -130,7 +131,7 @@ export class NTQQFileApi {
|
|||||||
fileName: fileName,
|
fileName: fileName,
|
||||||
sourcePath: path,
|
sourcePath: path,
|
||||||
original: true,
|
original: true,
|
||||||
picType: isGIF(picPath) ? PicType.NEWPIC_GIF : PicType.NEWPIC_JPEG,
|
picType: await getFileTypeForSendType(picPath),
|
||||||
picSubType: subType,
|
picSubType: subType,
|
||||||
fileUuid: '',
|
fileUuid: '',
|
||||||
fileSubId: '',
|
fileSubId: '',
|
||||||
|
14
src/core/helper/msg.ts
Normal file
14
src/core/helper/msg.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import * as fileType from 'file-type';
|
||||||
|
import { PicType } from '../types';
|
||||||
|
export async function getFileTypeForSendType(picPath: string): Promise<PicType> {
|
||||||
|
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;
|
||||||
|
}
|
Reference in New Issue
Block a user