Skip to content

Commit

Permalink
perf: 图片后缀识别
Browse files Browse the repository at this point in the history
  • Loading branch information
msojocs committed May 7, 2024
1 parent 8ab6ba6 commit f896a25
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/common/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import http from 'http'
import https from 'https'
import path from 'path'
import { useLogger } from './log'
import { getFileType } from '../ntqq/common/fs-api'

const log = useLogger('File')

/**
* 获取图像信息
*
* TODO: 使用NTQQ自带方法获取
* @param path 图片路径
* @returns 取图像信息
*/
Expand All @@ -27,11 +27,13 @@ export const getImageInfo = async (path: string): Promise<ImageInfo | undefined>
const md5 = await getFileMd5(path)
log.info('get image info...')
const stat = fs.statSync(path)
const type = getFileType(path)
return {
width: ret.width || 0,
height: ret.height || 0,
md5: md5,
size: stat.size
size: stat.size,
ext: type?.ext ?? 'jpg'
}
}

Expand Down
1 change: 1 addition & 0 deletions src/common/interface/file.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface ImageInfo {
height: number
size: number
md5: string
ext: string
}
5 changes: 3 additions & 2 deletions src/transfer/message/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export const convertBotMessage2NTMessageSingle = async (msg: BotMessage.SendElem
height: src.height,
md5: src.md5,
size: src.size,
ext: 'jpg',
} : undefined
if (src.path == null || !fs.existsSync(src.path)) {
// 文件路径有问题,检查是否有网络地址
Expand All @@ -220,7 +221,7 @@ export const convertBotMessage2NTMessageSingle = async (msg: BotMessage.SendElem
// const fileType = await getFileType(src.path)
// log.info('file type:', fileType)
// get real storage path
const realPath = getRichMediaFilePathForGuild(info.md5, `${info.md5}.jpg`)
const realPath = getRichMediaFilePathForGuild(info.md5, `${info.md5}.${info.ext}`)
log.info('real path:', realPath)
// copy
const ret = copyFile(src.path, realPath)
Expand All @@ -239,7 +240,7 @@ export const convertBotMessage2NTMessageSingle = async (msg: BotMessage.SendElem
md5HexStr: info.md5,
picWidth: info.width,
picHeight: info.height,
fileName: `${info.md5}.jpg`,
fileName: `${info.md5}.${info.ext}`,
fileSize: `${info.size}`,
original: true,
picSubType: 0,
Expand Down

0 comments on commit f896a25

Please sign in to comment.