From 19dfc068227a0b0c6f5e580161f3cf734c155a23 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Tue, 30 Jan 2024 17:26:00 +0800 Subject: [PATCH] refactor: log path --- manifest.json | 14 +++++++------- src/global.d.ts | 2 +- src/main/main.ts | 26 +++++++++++++------------- src/main/utils.ts | 14 ++++++++++++-- src/renderer.ts | 2 +- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/manifest.json b/manifest.json index 5ff5984..d75169a 100644 --- a/manifest.json +++ b/manifest.json @@ -11,13 +11,13 @@ "link": "https://github.com/linyuchen" }], "repository": { - "repo": "linyuchen/LiteLoaderQQNT-OneBotApi", - "branch": "main", - "release": { - "tag": "latest", - "name": "LLOneBot.zip" - } - }, + "repo": "linyuchen/LiteLoaderQQNT-OneBotApi", + "branch": "main", + "release": { + "tag": "latest", + "name": "LLOneBot.zip" + } +}, "platform": [ "win32", "linux", diff --git a/src/global.d.ts b/src/global.d.ts index 2ecbbb1..d2b22ff 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -43,7 +43,7 @@ declare var llonebot: { setConfig(config: Config):void; getConfig():Promise; setSelfInfo(selfInfo: SelfInfo):void; - downloadFile(arg: {uri: string, localFilePath: string}):Promise; + downloadFile(arg: {uri: string, fileName: string}):Promise; deleteFile(path: string[]):Promise; getRunningStatus(): Promise; }; diff --git a/src/main/main.ts b/src/main/main.ts index c6653ff..1a44736 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 {isGIF, log} from "./utils"; +import {CONFIG_DIR, isGIF, log} from "./utils"; import {friends, groups, selfInfo} from "./data"; import {} from "../global"; @@ -30,39 +30,39 @@ let running = false; function onLoad() { log("main onLoaded"); // const config_dir = browserWindow.LiteLoader.plugins["LLOneBot"].path.data; - const config_dir = global.LiteLoader.plugins["LLOneBot"].path.data; function getConfigUtil() { - const configFilePath = path.join(config_dir, `config_${selfInfo.user_id}.json`) + const configFilePath = path.join(CONFIG_DIR, `config_${selfInfo.user_id}.json`) return new ConfigUtil(configFilePath) } - if (!fs.existsSync(config_dir)) { - fs.mkdirSync(config_dir, {recursive: true}); + if (!fs.existsSync(CONFIG_DIR)) { + fs.mkdirSync(CONFIG_DIR, {recursive: true}); } ipcMain.handle(CHANNEL_GET_CONFIG, (event: any, arg: any) => { return getConfigUtil().getConfig() }) - ipcMain.handle(CHANNEL_DOWNLOAD_FILE, async (event: any, arg: {uri: string, localFilePath: string}) => { + ipcMain.handle(CHANNEL_DOWNLOAD_FILE, async (event: any, arg: {uri: string, fileName: string}) => { + let filePath = path.join(CONFIG_DIR, arg.fileName) let url = new URL(arg.uri); if (url.protocol == "base64:"){ // base64转成文件 let base64Data = arg.uri.split("base64://")[1] const buffer = Buffer.from(base64Data, 'base64'); - fs.writeFileSync(arg.localFilePath, buffer); + fs.writeFileSync(filePath, buffer); } else if (url.protocol == "http:" || url.protocol == "https:") { // 下载文件 let res = await fetch(url) let blob = await res.blob(); let buffer = await blob.arrayBuffer(); - fs.writeFileSync(arg.localFilePath, Buffer.from(buffer)); + fs.writeFileSync(filePath, Buffer.from(buffer)); } - if (isGIF(arg.localFilePath)) { - fs.renameSync(arg.localFilePath, arg.localFilePath + ".gif"); - arg.localFilePath += ".gif"; + if (isGIF(filePath)) { + fs.renameSync(filePath, filePath + ".gif"); + filePath += ".gif"; } - return arg.localFilePath; + return filePath; }) ipcMain.on(CHANNEL_SET_CONFIG, (event: any, arg: Config) => { getConfigUtil().setConfig(arg) @@ -113,7 +113,7 @@ function onLoad() { }, body: JSON.stringify(arg) }).then((res: any) => { - log("新消息事件上传"); + log("新消息事件上传成功:" + JSON.stringify(arg)); }, (err: any) => { log("新消息事件上传失败:" + err + JSON.stringify(arg)); }); diff --git a/src/main/utils.ts b/src/main/utils.ts index 727e07f..4a51b1b 100644 --- a/src/main/utils.ts +++ b/src/main/utils.ts @@ -1,8 +1,17 @@ +import * as path from "path"; +import {json} from "express"; + const fs = require('fs'); +export const CONFIG_DIR = global.LiteLoader.plugins["LLOneBot"].path.data; export function log(msg: any) { let currentDateTime = new Date().toLocaleString(); - fs.appendFile("./llonebot.log", currentDateTime + ":" + msg + "\n", (err: any) => { + const date = new Date(); + const year = date.getFullYear(); + const month = date.getMonth() + 1; + const day = date.getDate(); + const currentDate = `${year}-${month}-${day}`; + fs.appendFile(path.join(CONFIG_DIR , `llonebot-${currentDate}.log`), currentDateTime + ":" + JSON.stringify(msg) + "\n", (err: any) => { }) } @@ -13,4 +22,5 @@ export function isGIF(path: string) { 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 e0b0e98..949607a 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -245,7 +245,7 @@ async function listenSendMessage(postData: PostDataSendMsg) { if (uri.protocol == "file:") { localFilePath = url.split("file://")[1] } else { - localFilePath = await window.llonebot.downloadFile({uri: url, localFilePath: localFilePath}) + localFilePath = await window.llonebot.downloadFile({uri: url, fileName: localFilePath}) } message.file = localFilePath sendFiles.push(localFilePath);