From 0ce6546d7f563c44de7d06a4f2dff8ed8f318c1a Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 11 Nov 2024 23:10:32 +0800 Subject: [PATCH] fix: clear cache permission in windows Signed-off-by: Innei --- apps/main/src/lib/cleaner.ts | 4 +- apps/main/src/tipc/app.ts | 27 ++++++++++-- apps/renderer/index.html | 2 +- .../renderer/src/modules/settings/control.tsx | 2 +- .../modules/settings/tabs/data-control.tsx | 43 ++++++++++++++----- icons/mgc/folder_open_cute_re.svg | 1 + 6 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 icons/mgc/folder_open_cute_re.svg diff --git a/apps/main/src/lib/cleaner.ts b/apps/main/src/lib/cleaner.ts index c761aa658..325e52d8b 100644 --- a/apps/main/src/lib/cleaner.ts +++ b/apps/main/src/lib/cleaner.ts @@ -69,7 +69,9 @@ export const getCacheSize = async () => { const cachePath = path.join(app.getPath("userData"), "cache") // Size is in bytes - const sizeInBytes = await fastFolderSizeAsync(cachePath) + const sizeInBytes = await fastFolderSizeAsync(cachePath).catch((error) => { + logger.error(error) + }) return sizeInBytes || 0 } diff --git a/apps/main/src/tipc/app.ts b/apps/main/src/tipc/app.ts index 3415a59ca..e064653a8 100644 --- a/apps/main/src/tipc/app.ts +++ b/apps/main/src/tipc/app.ts @@ -5,7 +5,7 @@ import path from "node:path" import { getRendererHandlers } from "@egoist/tipc/main" import { callWindowExpose } from "@follow/shared/bridge" import type { BrowserWindow } from "electron" -import { app, clipboard, dialog, screen } from "electron" +import { app, clipboard, dialog, screen, shell } from "electron" import { registerMenuAndContextMenu } from "~/init" import { clearAllData, getCacheSize } from "~/lib/cleaner" @@ -275,13 +275,34 @@ ${content} getCacheSize: t.procedure.action(async () => { return getCacheSize() }), + openCacheFolder: t.procedure.action(async () => { + const dir = path.join(app.getPath("userData"), "cache") + shell.openPath(dir) + }), getCacheLimit: t.procedure.action(async () => { return store.get(StoreKey.CacheSizeLimit) }), clearCache: t.procedure.action(async () => { - const cachePath = path.join(app.getPath("userData"), "cache") - await fsp.rm(cachePath, { recursive: true, force: true }) + const cachePath = path.join(app.getPath("userData"), "cache", "Cache_Data") + if (process.platform === "win32") { + // Request elevation on Windows + + try { + // Create a bat file to delete cache with elevated privileges + const batPath = path.join(app.getPath("temp"), "clear_cache.bat") + await fsp.writeFile(batPath, `@echo off\nrd /s /q "${cachePath}"\ndel "%~f0"`, "utf-8") + + // Execute the bat file with admin privileges + await shell.openPath(batPath) + return + } catch (err) { + logger.error("Failed to clear cache with elevation", { error: err }) + } + } + await fsp.rm(cachePath, { recursive: true, force: true }).catch(() => { + logger.error("Failed to clear cache") + }) }), limitCacheSize: t.procedure.input().action(async ({ input }) => { diff --git a/apps/renderer/index.html b/apps/renderer/index.html index 97cb70803..188bd8d37 100644 --- a/apps/renderer/index.html +++ b/apps/renderer/index.html @@ -63,7 +63,7 @@
-
+