refactor: log path

This commit is contained in:
linyuchen 2024-01-30 17:26:00 +08:00
parent 465b7eaf6e
commit 19dfc06822
5 changed files with 34 additions and 24 deletions

View File

@ -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",

2
src/global.d.ts vendored
View File

@ -43,7 +43,7 @@ declare var llonebot: {
setConfig(config: Config):void;
getConfig():Promise<Config>;
setSelfInfo(selfInfo: SelfInfo):void;
downloadFile(arg: {uri: string, localFilePath: string}):Promise<string>;
downloadFile(arg: {uri: string, fileName: string}):Promise<string>;
deleteFile(path: string[]):Promise<void>;
getRunningStatus(): Promise<boolean>;
};

View File

@ -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));
});

View File

@ -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'
}
}

View File

@ -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);