mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
refactor: log path
This commit is contained in:
parent
465b7eaf6e
commit
19dfc06822
@ -17,7 +17,7 @@
|
||||
"tag": "latest",
|
||||
"name": "LLOneBot.zip"
|
||||
}
|
||||
},
|
||||
},
|
||||
"platform": [
|
||||
"win32",
|
||||
"linux",
|
||||
|
2
src/global.d.ts
vendored
2
src/global.d.ts
vendored
@ -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>;
|
||||
};
|
||||
|
@ -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));
|
||||
});
|
||||
|
@ -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) => {
|
||||
|
||||
})
|
||||
}
|
||||
@ -14,3 +23,4 @@ export function isGIF(path: string) {
|
||||
fs.closeSync(fd);
|
||||
return buffer.toString() === 'GIF8'
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user