From 6d33fb8b14cad536d7395deb2e65fb81ba04adcb Mon Sep 17 00:00:00 2001 From: linyuchen Date: Sat, 20 Jan 2024 08:38:14 +0800 Subject: [PATCH] check gif --- src/main/main.ts | 7 +++++-- src/main/utils.ts | 8 ++++++++ src/renderer.ts | 3 +-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/main.ts b/src/main/main.ts index 039426d..d828bd0 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -17,7 +17,7 @@ import { } from "../common/IPCChannel"; import {ConfigUtil} from "./config"; import {startExpress} from "./HttpServer"; -import {log} from "./utils"; +import {isGIF, log} from "./utils"; import {friends, groups, selfInfo} from "./data"; const fs = require('fs'); @@ -55,7 +55,10 @@ function onLoad(plugin: any) { let buffer = await blob.arrayBuffer(); fs.writeFileSync(arg.localFilePath, Buffer.from(buffer)); } - // todo: 需要识别gif格式 + if (isGIF(arg.localFilePath)) { + fs.renameSync(arg.localFilePath, arg.localFilePath + ".gif"); + arg.localFilePath += ".gif"; + } return arg.localFilePath; }) ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => { diff --git a/src/main/utils.ts b/src/main/utils.ts index 50b4845..727e07f 100644 --- a/src/main/utils.ts +++ b/src/main/utils.ts @@ -5,4 +5,12 @@ export function log(msg: any) { fs.appendFile("./llonebot.log", currentDateTime + ":" + msg + "\n", (err: any) => { }) +} + +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' } \ No newline at end of file diff --git a/src/renderer.ts b/src/renderer.ts index f329ce2..6a62807 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -233,7 +233,6 @@ async function listenSendMessage(postData: PostDataSendMsg) { let uri = new URL(url); let ext: string; if (message.type == "image") { - // todo: 需要识别gif格式 ext = ".png" } if (message.type == "voice") { @@ -243,7 +242,7 @@ async function listenSendMessage(postData: PostDataSendMsg) { if (uri.protocol == "file:") { localFilePath = url.split("file://")[1] } else { - await window.llonebot.downloadFile({uri: url, localFilePath: localFilePath}) + localFilePath = await window.llonebot.downloadFile({uri: url, localFilePath: localFilePath}) } message.file = localFilePath sendFiles.push(localFilePath);