Skip to content

Commit

Permalink
fix: clear cache permission in windows
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Nov 11, 2024
1 parent 6eed2c4 commit 0ce6546
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
4 changes: 3 additions & 1 deletion apps/main/src/lib/cleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
27 changes: 24 additions & 3 deletions apps/main/src/tipc/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<number>().action(async ({ input }) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/renderer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<body>
<div id="root"></div>

<div id="app-skeleton">
<div id="app-skeleton" class="drag-region">
<!-- Skeleton -->
<style>
html,
Expand Down
2 changes: 1 addition & 1 deletion apps/renderer/src/modules/settings/control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const SettingActionItem = ({
action,
buttonText,
}: {
label: string
label: ReactNode
action: () => void
buttonText: string
}) => (
Expand Down
43 changes: 32 additions & 11 deletions apps/renderer/src/modules/settings/tabs/data-control.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CarbonInfinitySymbol } from "@follow/components/icons/infinify.jsx"
import { Button } from "@follow/components/ui/button/index.js"
import { Button, MotionButtonBase } from "@follow/components/ui/button/index.js"
import { Label } from "@follow/components/ui/label/index.jsx"
import { Slider } from "@follow/components/ui/slider/index.js"
import { env } from "@follow/shared/env"
Expand All @@ -15,7 +15,7 @@ import { tipcClient } from "~/lib/client"
import { queryClient } from "~/lib/query-client"
import { clearLocalPersistStoreData } from "~/store/utils/clear"

import { SettingDescription } from "../control"
import { SettingActionItem, SettingDescription } from "../control"
import { createSetting } from "../helper/builder"
import { SettingItemGroup } from "../section"

Expand Down Expand Up @@ -111,20 +111,41 @@ export const SettingDataControl = () => {
value: t("general.cache"),
},
isElectronBuild && AppCacheLimit,
isElectronBuild && {
label: t("data_control.clean_cache.button"),
description: t("data_control.clean_cache.description"),
buttonText: t("data_control.clean_cache.button"),
action: async () => {
await tipcClient?.clearCache()
queryClient.invalidateQueries({ queryKey: ["app", "cache", "size"] })
},
},
isElectronBuild && CleanCache,
]}
/>
</div>
)
}
const CleanCache = () => {
const { t } = useTranslation("settings")

return (
<SettingItemGroup>
<SettingActionItem
label={
<span className="flex items-center gap-1">
{t("data_control.clean_cache.button")}
<MotionButtonBase
onClick={() => {
tipcClient?.openCacheFolder()
}}
className="center flex"
>
<i className="i-mgc-folder-open-cute-re" />
</MotionButtonBase>
</span>
}
action={async () => {
await tipcClient?.clearCache()
queryClient.setQueryData(["app", "cache", "size"], 0)
}}
buttonText={t("data_control.clean_cache.button")}
/>
<SettingDescription>{t("data_control.clean_cache.description")}</SettingDescription>
</SettingItemGroup>
)
}
const AppCacheLimit = () => {
const { t } = useTranslation("settings")
const { data: cacheSize, isLoading: isLoadingCacheSize } = useQuery({
Expand Down
1 change: 1 addition & 0 deletions icons/mgc/folder_open_cute_re.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ce6546

Please sign in to comment.