From 01d5663bc82f7e41fe6909a15ac442e09a4954ba 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: Mon, 12 May 2025 19:18:33 +0800 Subject: [PATCH] fix: image size --- src/common/ffmpeg.ts | 4 ++-- src/core/apis/file.ts | 18 ++---------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/common/ffmpeg.ts b/src/common/ffmpeg.ts index 40385d23..e6100f1d 100644 --- a/src/common/ffmpeg.ts +++ b/src/common/ffmpeg.ts @@ -4,10 +4,10 @@ import { execFile } from 'child_process'; import { promisify } from 'util'; import type { VideoInfo } from './video'; import { fileTypeFromFile } from 'file-type'; -import imageSize from 'image-size'; import { fileURLToPath } from 'node:url'; import { platform } from 'node:os'; import { LogWrapper } from './log'; +import { imageSizeFallBack } from '@/image-size'; const currentPath = dirname(fileURLToPath(import.meta.url)); const execFileAsync = promisify(execFile); const getFFmpegPath = (tool: string): string => { @@ -157,7 +157,7 @@ export class FFmpegService { try { await this.extractThumbnail(videoPath, thumbnailPath); // 获取图片尺寸 - const dimensions = imageSize(thumbnailPath); + const dimensions = await imageSizeFallBack(thumbnailPath); return { format: fileType?.ext ?? 'mp4', diff --git a/src/core/apis/file.ts b/src/core/apis/file.ts index 7b124b47..b92eee23 100644 --- a/src/core/apis/file.ts +++ b/src/core/apis/file.ts @@ -17,8 +17,6 @@ import fs from 'fs'; import fsPromises from 'fs/promises'; import { InstanceContext, NapCatCore, SearchResultItem } from '@/core'; import { fileTypeFromFile } from 'file-type'; -import imageSize from 'image-size'; -import { ISizeCalculationResult } from 'image-size/dist/types/interface'; import { RkeyManager } from '@/core/helper/rkey'; import { calculateFileMD5 } from '@/common/file'; import pathLib from 'node:path'; @@ -30,6 +28,7 @@ import { FFmpegService } from '@/common/ffmpeg'; import { rkeyDataType } from '../types/file'; import { NapProtoMsg } from '@napneko/nap-proto-core'; import { FileId } from '../packet/transformer/proto/misc/fileid'; +import { imageSizeFallBack } from '@/image-size'; export class NTQQFileApi { context: InstanceContext; @@ -209,7 +208,7 @@ export class NTQQFileApi { if (fileSize === 0) { throw new Error('文件异常,大小为0'); } - const imageSize = await this.core.apis.FileApi.getImageSize(picPath); + const imageSize = await imageSizeFallBack(picPath); context.deleteAfterSentFiles.push(path); return { elementType: ElementType.PIC, @@ -437,19 +436,6 @@ export class NTQQFileApi { return completeRetData.filePath; } - async getImageSize(filePath: string): Promise { - return new Promise((resolve, reject) => { - imageSize(filePath, (err: Error | null, dimensions) => { - if (err) { - reject(new Error(err.message)); - } else if (!dimensions) { - reject(new Error('获取图片尺寸失败')); - } else { - resolve(dimensions); - } - }); - }); - } async searchForFile(keys: string[]): Promise { const randomResultId = 100000 + Math.floor(Math.random() * 10000);