diff --git a/changelog.md b/changelog.md index c02ad15..b7908ca 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,7 @@ +### New Features + +- 自动删除过期日志与更新缓存 + ### Bug Fixes - 修复接管dns后自定义hosts无法删除的问题 diff --git a/src/main/utils/init.ts b/src/main/utils/init.ts index 99cbb9c..0a23ffa 100644 --- a/src/main/utils/init.ts +++ b/src/main/utils/init.ts @@ -20,7 +20,7 @@ import { defaultProfileConfig } from './template' import yaml from 'yaml' -import { mkdir, writeFile, copyFile } from 'fs/promises' +import { mkdir, writeFile, copyFile, rm, readdir } from 'fs/promises' import { existsSync } from 'fs' import path from 'path' import { startPacServer } from '../resolve/server' @@ -90,6 +90,26 @@ async function initFiles(): Promise { ]) } +async function cleanup(): Promise { + // update cache + const files = await readdir(dataDir()) + for (const file of files) { + if (file.endsWith('.exe') || file.endsWith('.dmg')) { + await rm(path.join(dataDir(), file)) + } + } + // logs + const { maxLogDays = 7 } = await getAppConfig() + const logs = await readdir(logDir()) + for (const log of logs) { + const date = new Date(log.split('.')[0]) + const diff = Date.now() - date.getTime() + if (diff > maxLogDays * 24 * 60 * 60 * 1000) { + await rm(path.join(logDir(), log)) + } + } +} + function initDeeplink(): void { if (process.defaultApp) { if (process.argv.length >= 2) { @@ -106,6 +126,7 @@ export async function init(): Promise { await initDirs() await initConfig() await initFiles() + await cleanup() await startPacServer() const { sysProxy } = await getAppConfig() await triggerSysProxy(sysProxy.enable) diff --git a/src/main/utils/template.ts b/src/main/utils/template.ts index 75a8797..1a9b1ef 100644 --- a/src/main/utils/template.ts +++ b/src/main/utils/template.ts @@ -3,6 +3,7 @@ export const defaultConfig: IAppConfig = { silentStart: false, appTheme: 'system', proxyInTray: true, + maxLogDays: 7, proxyDisplayMode: 'simple', proxyDisplayOrder: 'default', autoCheckUpdate: true, diff --git a/src/renderer/src/pages/mihomo.tsx b/src/renderer/src/pages/mihomo.tsx index c39f4eb..5945451 100644 --- a/src/renderer/src/pages/mihomo.tsx +++ b/src/renderer/src/pages/mihomo.tsx @@ -17,7 +17,7 @@ const CoreMap = { const Mihomo: React.FC = () => { const { appConfig, patchAppConfig } = useAppConfig() - const { core = 'mihomo' } = appConfig || {} + const { core = 'mihomo', maxLogDays = 7 } = appConfig || {} const { controledMihomoConfig, patchControledMihomoConfig } = useControledMihomoConfig() const { ipv6, @@ -340,6 +340,17 @@ const Mihomo: React.FC = () => { }} /> + + { + patchAppConfig({ maxLogDays: parseInt(v) }) + }} + /> +