增加读取缓存目录大小

close #55
This commit is contained in:
BTMuli
2023-10-26 21:55:49 +08:00
parent 56c6c4f70f
commit 9e4270603f
6 changed files with 131 additions and 100 deletions

View File

@@ -146,8 +146,8 @@
</template>
<script lang="ts" setup>
import { app, fs, os } from "@tauri-apps/api";
import { relaunch } from "@tauri-apps/api/process";
import { app, fs, invoke, os } from "@tauri-apps/api";
import { exit } from "@tauri-apps/api/process";
import { computed, onMounted, ref } from "vue";
import showConfirm from "../../components/func/confirm";
@@ -160,6 +160,7 @@ import { useAppStore } from "../../store/modules/app";
import { useHomeStore } from "../../store/modules/home";
import { useUserStore } from "../../store/modules/user";
import { getBuildTime } from "../../utils/TGBuild";
import { bytesToSize, getCacheDir } from "../../utils/toolFunc";
import { backupUiafData, restoreUiafData } from "../../utils/UIAF";
import TGRequest from "../../web/request/TGRequest";
import { backupAbyssData, backupCookieData } from "../../web/utils/backupData";
@@ -439,31 +440,54 @@ async function confirmUpdate(title?: string): Promise<void> {
// 清除用户缓存
async function confirmDelCache(): Promise<void> {
const CacheDir = await getCacheDir();
if (CacheDir === false) {
showSnackbar({
color: "error",
text: "不支持的平台!",
});
return;
}
let cacheBSize: number = 0;
loadingTitle.value = "正在检测缓存...";
loadingSub.value = "耗时较久,请稍作等候";
loading.value = true;
const timeStart = Date.now();
if (Array.isArray(CacheDir)) {
for (const dir of CacheDir) {
const size: number = await invoke("get_dir_size", { path: dir });
cacheBSize += size;
}
} else {
cacheBSize = await invoke("get_dir_size", { path: CacheDir });
}
let cacheSize = bytesToSize(cacheBSize);
loading.value = false;
const timeEnd = Date.now();
const res = await showConfirm({
title: "确认清除缓存吗?",
text: "只删除 Webview 缓存,不处理用户数据",
text: `当前缓存大小为 ${cacheSize},耗时 ${timeEnd - timeStart} 毫秒`,
});
if (!res) {
if (res === false) {
showSnackbar({
color: "grey",
text: "已取消清除缓存",
});
return;
}
await fs.removeDir("EBWebview\\Default\\Cache", {
dir: fs.BaseDirectory.AppLocalData,
recursive: true,
});
await fs.removeDir("EBWebview\\Default\\Code Cache", {
dir: fs.BaseDirectory.AppLocalData,
recursive: true,
});
if (Array.isArray(CacheDir)) {
for (const dir of CacheDir) {
await fs.removeDir(dir, { recursive: true });
}
} else {
await fs.removeDir(CacheDir, { recursive: true });
}
showSnackbar({
text: "缓存已清除!即将重启应用...",
text: "缓存已清除!请重新启动应用!",
});
await new Promise(() => {
setTimeout(async () => {
await relaunch();
await exit();
}, 1500);
});
}

View File

@@ -4,6 +4,8 @@
* @since Beta v0.3.4
*/
import { fs, os, path } from "@tauri-apps/api";
import type { FileEntry } from "@tauri-apps/api/fs";
import { v4 } from "uuid";
/**
@@ -34,3 +36,62 @@ export function getDeviceID(): string {
}
return deviceID;
}
/**
* @description byte 转成 KB MB GB
* @since Beta v0.3.4
* @param {number} bytes - 字节数
* @returns {string} KB MB GB
*/
export function bytesToSize(bytes: number): string {
if (bytes === 0) return "0 B";
const k = 1024;
const sizes = ["B", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return `${(bytes / Math.pow(k, i)).toFixed(2)} ${sizes[i]}`;
}
/**
* @description 获取文件夹大小
* @since Beta v0.3.4
* @param {FileEntry[]} cacheInfo - 文件夹信息
* @param {boolean} isRoot - 是否是根目录
* @returns {Promise<number>} 文件夹大小
*/
export async function getDirSize(cacheInfo: FileEntry[], isRoot: false): Promise<number>;
export async function getDirSize(cacheInfo: FileEntry[], isRoot?: true): Promise<string>;
export async function getDirSize(
cacheInfo: FileEntry[],
isRoot?: boolean,
): Promise<number | string> {
let size = 0;
for (const item of cacheInfo) {
if (item.children) {
const dir = await fs.readDir(item.path);
size += await getDirSize(dir, false);
} else {
const file = await fs.readBinaryFile(item.path);
size += file.byteLength;
}
}
if (isRoot === undefined || isRoot) return bytesToSize(size);
return size;
}
/**
* @description 获取缓存目录
* @since Beta v0.3.4
* @returns {string|string[]} 缓存目录
*/
export async function getCacheDir(): Promise<string | string[] | false> {
const cacheDir = await path.appCacheDir();
const osType = await os.type();
if (osType === "Windows_NT") {
const cache = `${cacheDir}EBWebview${path.sep}Default${path.sep}Cache`;
const codeCache = `${cacheDir}EBWebview${path.sep}Default${path.sep}Code Cache`;
return [cache, codeCache];
} else if (osType === "Darwin") {
return `${cacheDir}WebKit`;
}
return false;
}