|
| 1 | +import fsp from "node:fs/promises" |
1 | 2 | import path from "node:path" |
2 | 3 | import { fileURLToPath } from "node:url" |
3 | 4 |
|
4 | 5 | import { callWindowExpose } from "@follow/shared/bridge" |
5 | 6 | import { DEV } from "@follow/shared/constants" |
6 | | -import { app, BrowserWindow, clipboard, dialog } from "electron" |
| 7 | +import { app, BrowserWindow, clipboard, dialog, shell } from "electron" |
7 | 8 |
|
| 9 | +import { getCacheSize } from "~/lib/cleaner" |
8 | 10 | import { i18n } from "~/lib/i18n" |
| 11 | +import { store, StoreKey } from "~/lib/store" |
9 | 12 | import { registerAppTray } from "~/lib/tray" |
10 | | -import { logger } from "~/logger" |
| 13 | +import { logger, revealLogFile } from "~/logger" |
11 | 14 | import { AppManager } from "~/manager/app" |
12 | 15 | import { WindowManager } from "~/manager/window" |
13 | 16 | import { cleanupOldRender, loadDynamicRenderEntry } from "~/updater/hot-updater" |
@@ -179,4 +182,73 @@ export class AppService extends IpcService { |
179 | 182 |
|
180 | 183 | return path.join(app.getAppPath(), input) |
181 | 184 | } |
| 185 | + |
| 186 | + @IpcMethod() |
| 187 | + openCacheFolder(_context: IpcContext): void { |
| 188 | + const dir = path.join(app.getPath("userData"), "cache") |
| 189 | + shell.openPath(dir) |
| 190 | + } |
| 191 | + |
| 192 | + @IpcMethod() |
| 193 | + getCacheLimit(_context: IpcContext): number { |
| 194 | + return store.get(StoreKey.CacheSizeLimit) || 0 |
| 195 | + } |
| 196 | + |
| 197 | + @IpcMethod() |
| 198 | + async clearCache(_context: IpcContext): Promise<void> { |
| 199 | + const cachePath = path.join(app.getPath("userData"), "cache", "Cache_Data") |
| 200 | + if (process.platform === "win32") { |
| 201 | + // Request elevation on Windows |
| 202 | + |
| 203 | + try { |
| 204 | + // Create a bat file to delete cache with elevated privileges |
| 205 | + const batPath = path.join(app.getPath("temp"), "clear_cache.bat") |
| 206 | + await fsp.writeFile(batPath, `@echo off\nrd /s /q "${cachePath}"\ndel "%~f0"`, "utf-8") |
| 207 | + |
| 208 | + // Execute the bat file with admin privileges |
| 209 | + await shell.openPath(batPath) |
| 210 | + return |
| 211 | + } catch (err) { |
| 212 | + logger.error("Failed to clear cache with elevation", { error: err }) |
| 213 | + } |
| 214 | + } |
| 215 | + await fsp.rm(cachePath, { recursive: true, force: true }).catch(() => { |
| 216 | + logger.error("Failed to clear cache") |
| 217 | + }) |
| 218 | + } |
| 219 | + |
| 220 | + // getCacheLimit: t.procedure.action(async () => { |
| 221 | + // return store.get(StoreKey.CacheSizeLimit) |
| 222 | + // }), |
| 223 | + |
| 224 | + // clearCache: t.procedure.action(async () => { |
| 225 | + |
| 226 | + // }), |
| 227 | + |
| 228 | + // limitCacheSize: t.procedure.input<number>().action(async ({ input }) => { |
| 229 | + // logger.info("set limitCacheSize", input) |
| 230 | + // if (input === 0) { |
| 231 | + // store.delete(StoreKey.CacheSizeLimit) |
| 232 | + // } else { |
| 233 | + // store.set(StoreKey.CacheSizeLimit, input) |
| 234 | + // } |
| 235 | + // }), |
| 236 | + @IpcMethod() |
| 237 | + limitCacheSize(_context: IpcContext, input: number): void { |
| 238 | + if (input === 0) { |
| 239 | + store.delete(StoreKey.CacheSizeLimit) |
| 240 | + } else { |
| 241 | + store.set(StoreKey.CacheSizeLimit, input) |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + @IpcMethod() |
| 246 | + revealLogFile(_context: IpcContext) { |
| 247 | + return revealLogFile() |
| 248 | + } |
| 249 | + |
| 250 | + @IpcMethod() |
| 251 | + getCacheSize(_context: IpcContext) { |
| 252 | + return getCacheSize() |
| 253 | + } |
182 | 254 | } |
0 commit comments