From b863896249857a2c10144f84ca39f8a30a0c91e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 19 Jun 2024 23:26:05 +0800 Subject: [PATCH 1/2] refactor: log file limit --- src/common/utils/helper.ts | 26 ++++++++++++++++++++++++-- src/common/utils/log.ts | 2 +- src/index.ts | 7 ++++--- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/common/utils/helper.ts b/src/common/utils/helper.ts index a2bfb0a1..ff1fe789 100644 --- a/src/common/utils/helper.ts +++ b/src/common/utils/helper.ts @@ -4,7 +4,7 @@ import fs from 'fs/promises'; import { log, logDebug } from './log'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; - +import * as fsPromise from 'node:fs/promises'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); @@ -261,4 +261,26 @@ export function isEqual(obj1: any, obj2: any) { if (!isEqual(obj1[key], obj2[key])) return false; } return true; -} \ No newline at end of file +} + +export async function deleteOldFiles(directoryPath: string, daysThreshold: number) { + try { + const files = await fsPromise.readdir(directoryPath); + + for (const file of files) { + const filePath = path.join(directoryPath, file); + const stats = await fsPromise.stat(filePath); + const lastModifiedTime = stats.mtimeMs; + const currentTime = Date.now(); + const timeDifference = currentTime - lastModifiedTime; + const daysDifference = timeDifference / (1000 * 60 * 60 * 24); + + if (daysDifference > daysThreshold) { + await fsPromise.unlink(filePath); // Delete the file + //console.log(`Deleted: ${filePath}`); + } + } + } catch (error) { + //console.error('Error deleting files:', error); + } +} diff --git a/src/common/utils/log.ts b/src/common/utils/log.ts index fb43c68c..cd6edcc8 100644 --- a/src/common/utils/log.ts +++ b/src/common/utils/log.ts @@ -39,7 +39,7 @@ const logConfig: Configuration = { FileAppender: { // 输出到文件的appender type: 'file', filename: logPath, // 指定日志文件的位置和文件名 - maxLoogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB + maxLogSize: 10485760, // 日志文件的最大大小(单位:字节),这里设置为10MB layout: { type: 'pattern', pattern: '%d{yyyy-MM-dd hh:mm:ss} [%p] %X{userInfo} | %m' diff --git a/src/index.ts b/src/index.ts index a8d5b2a6..de6bc80f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,7 +9,7 @@ import { log, logDebug, logError, LogLevel, logWarn, setLogLevel } from '@/commo import { NapCatOnebot11 } from '@/onebot11/main'; import { InitWebUi } from './webui/index'; import { WebUiDataRuntime } from './webui/src/helper/Data'; -import { UpdateConfig } from './common/utils/helper'; +import { deleteOldFiles, UpdateConfig } from './common/utils/helper'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import chalk from 'chalk'; @@ -22,6 +22,7 @@ program .option('-q, --qq ', 'QQ号') .parse(process.argv); +//deleteOldFiles(path.join(__dirname, 'logs'), 3).then().catch(); // UpdateConfig().catch(logError); 移除支持 // 启动WebUi InitWebUi(); @@ -40,10 +41,10 @@ checkVersion().then(async (remoteVersion: string) => { break; } } - logDebug(tagColor('[NapCat]'),'当前已是最新版本'); + logDebug(tagColor('[NapCat]'), '当前已是最新版本'); return; }).catch((e) => { - logError(tagColor('[NapCat]'),'检测更新失败', e); + logError(tagColor('[NapCat]'), '检测更新失败', e); }); // 不是很好待优化 const NapCat_OneBot11 = new NapCatOnebot11(); From ae0277f33c78d6dffa17eeec47eb5a171b680e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Wed, 19 Jun 2024 23:34:09 +0800 Subject: [PATCH 2/2] release: 1.5.8 --- docs/changelogs/CHANGELOG.v1.5.8.md | 7 ++++--- package.json | 2 +- src/onebot11/action/msg/SendMsg/create-send-elements.ts | 6 ++++-- src/onebot11/version.ts | 2 +- src/webui/ui/NapCat.ts | 2 +- static/assets/renderer.js | 2 +- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/docs/changelogs/CHANGELOG.v1.5.8.md b/docs/changelogs/CHANGELOG.v1.5.8.md index ca40304a..1ceab5ff 100644 --- a/docs/changelogs/CHANGELOG.v1.5.8.md +++ b/docs/changelogs/CHANGELOG.v1.5.8.md @@ -3,11 +3,12 @@ QQ Version: Windows 9.9.11-24568 / Linux 3.2.9-24568 ## 修复与优化 -* 修复文件残留问题 +* 修复视频文件残留问题 * 重构 getcookies接口 支持大部分常见域 + ## 新增与调整 -* 日志清理 -* 支持QQ音乐 图文卡片 +* 日志大小限制 +* 支持 QQ音乐 卡片 无签名支持时 启用内置方法(缺点没有封面 限速1min/条) * 支持Window X86-32机器 新增的 API 详细见[API文档](https://napneko.github.io/zh-CN/develop/extends_api) diff --git a/package.json b/package.json index 141237c6..298afcf9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "napcat", "private": true, "type": "module", - "version": "1.5.7", + "version": "1.5.8", "scripts": { "watch:dev": "vite --mode development", "watch:prod": "vite --mode production", diff --git a/src/onebot11/action/msg/SendMsg/create-send-elements.ts b/src/onebot11/action/msg/SendMsg/create-send-elements.ts index a0a36b82..bfca7171 100644 --- a/src/onebot11/action/msg/SendMsg/create-send-elements.ts +++ b/src/onebot11/action/msg/SendMsg/create-send-elements.ts @@ -134,8 +134,10 @@ const _handlers: { const uri2LocalRes = await uri2local(thumb); if (uri2LocalRes.success) thumb = uri2LocalRes.path; } - - return SendMsgElementConstructor.video(path, fileName, thumb); + let videoEle = await SendMsgElementConstructor.video(path, fileName, thumb); + //未测试 + context.deleteAfterSentFiles.push(videoEle.videoElement.filePath); + return videoEle; }, [OB11MessageDataType.miniapp]: async ({ data: any }) => SendMsgElementConstructor.miniapp(), diff --git a/src/onebot11/version.ts b/src/onebot11/version.ts index 4f7b3e8c..32eadef0 100644 --- a/src/onebot11/version.ts +++ b/src/onebot11/version.ts @@ -1 +1 @@ -export const version = '1.5.7'; +export const version = '1.5.8'; diff --git a/src/webui/ui/NapCat.ts b/src/webui/ui/NapCat.ts index a4ce9e15..7cf8fcda 100644 --- a/src/webui/ui/NapCat.ts +++ b/src/webui/ui/NapCat.ts @@ -29,7 +29,7 @@ async function onSettingWindowCreated(view: Element) { SettingItem( 'Napcat', undefined, - SettingButton('V1.5.7', 'napcat-update-button', 'secondary') + SettingButton('V1.5.8', 'napcat-update-button', 'secondary') ), ]), SettingList([ diff --git a/static/assets/renderer.js b/static/assets/renderer.js index 52ba36bd..d5f32807 100644 --- a/static/assets/renderer.js +++ b/static/assets/renderer.js @@ -167,7 +167,7 @@ async function onSettingWindowCreated(view) { SettingItem( 'Napcat', void 0, - SettingButton("V1.5.7", "napcat-update-button", "secondary") + SettingButton("V1.5.8", "napcat-update-button", "secondary") ) ]), SettingList([