diff --git a/src/main/ipc.ts b/src/main/ipc.ts index 771aa6ba..ff5f005a 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -1,3 +1,4 @@ +import fs from 'node:fs' import path from 'node:path' import { ThemeMode } from '@types' @@ -56,6 +57,28 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { mainWindow.setTitleBarOverlay(theme === 'dark' ? titleBarOverlayDark : titleBarOverlayLight) }) + // clear cache + ipcMain.handle('app:clear-cache', async () => { + const sessions = [session.defaultSession, session.fromPartition('persist:webview')] + + try { + await Promise.all( + sessions.map(async (session) => { + await session.clearCache() + await session.clearStorageData({ + storages: ['cookies', 'filesystem', 'shadercache', 'websql', 'serviceworkers', 'cachestorage'] + }) + }) + ) + await fileManager.clearTemp() + await fs.writeFileSync(log.transports.file.getFile().path, '') + return { success: true } + } catch (error: any) { + log.error('Failed to clear cache:', error) + return { success: false, error: error.message } + } + }) + // check for update ipcMain.handle('app:check-for-update', async () => { return { diff --git a/src/main/services/FileStorage.ts b/src/main/services/FileStorage.ts index 29789d82..00919d77 100644 --- a/src/main/services/FileStorage.ts +++ b/src/main/services/FileStorage.ts @@ -267,6 +267,11 @@ class FileStorage { await this.initStorageDir() } + public clearTemp = async (): Promise => { + await fs.promises.rmdir(this.tempDir, { recursive: true }) + await fs.promises.mkdir(this.tempDir, { recursive: true }) + } + public open = async ( _: Electron.IpcMainInvokeEvent, options: OpenDialogOptions diff --git a/src/preload/index.d.ts b/src/preload/index.d.ts index dcfc76df..5ca6dd66 100644 --- a/src/preload/index.d.ts +++ b/src/preload/index.d.ts @@ -18,6 +18,7 @@ declare global { setTheme: (theme: 'light' | 'dark') => void minApp: (options: { url: string; windowOptions?: Electron.BrowserWindowConstructorOptions }) => void reload: () => void + clearCache: () => Promise<{ success: boolean; error?: string }> zip: { compress: (text: string) => Promise decompress: (text: Buffer) => Promise diff --git a/src/preload/index.ts b/src/preload/index.ts index 4a8612c5..823bb697 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -13,6 +13,7 @@ const api = { setTheme: (theme: 'light' | 'dark') => ipcRenderer.invoke('app:set-theme', theme), openWebsite: (url: string) => ipcRenderer.invoke('open:website', url), minApp: (url: string) => ipcRenderer.invoke('minapp', url), + clearCache: () => ipcRenderer.invoke('app:clear-cache'), zip: { compress: (text: string) => ipcRenderer.invoke('zip:compress', text), decompress: (text: Buffer) => ipcRenderer.invoke('zip:decompress', text) diff --git a/src/renderer/src/i18n/locales/en-us.json b/src/renderer/src/i18n/locales/en-us.json index 9fb93d9c..6bb6c0b6 100644 --- a/src/renderer/src/i18n/locales/en-us.json +++ b/src/renderer/src/i18n/locales/en-us.json @@ -259,7 +259,6 @@ "settings": { "title": "Settings", "general": "General Settings", - "data": "Data Settings", "model": "Default Model", "assistant": "Default Assistant", "about": "About & Feedback", @@ -283,15 +282,6 @@ "general.reset.title": "Data Reset", "general.reset.button": "Reset", "general.manually_check_update.title": "Turn off update checking", - "data.webdav.title": "WebDAV", - "data.webdav.host": "WebDAV Host", - "data.webdav.host.placeholder": "http://localhost:8080", - "data.webdav.user": "WebDAV User", - "data.webdav.password": "WebDAV Password", - "data.webdav.path": "WebDAV Path", - "data.webdav.path.placeholder": "/backup", - "data.webdav.backup.button": "Backup to WebDAV", - "data.webdav.restore.button": "Restore from WebDAV", "advanced.title": "Advanced Settings", "advanced.auto_switch_to_topics": "Auto switch to topic", "provider.api_key": "API Key", @@ -356,9 +346,6 @@ "topic.position.right": "Right", "topic.show.time": "Show Topic Time", "display.title": "Display Settings", - "data.title": "Data Directory", - "data.app_data": "App Data", - "data.app_logs": "App Logs", "shortcuts": { "title": "Keyboard Shortcuts", "action": "Action", @@ -403,6 +390,28 @@ "custom": "Custom Proxy", "none": "No Proxy" } + }, + "data": { + "title": "Data Settings", + "webdav.title": "WebDAV", + "webdav.host": "WebDAV Host", + "webdav.host.placeholder": "http://localhost:8080", + "webdav.user": "WebDAV User", + "webdav.password": "WebDAV Password", + "webdav.path": "WebDAV Path", + "webdav.path.placeholder": "/backup", + "webdav.backup.button": "Backup to WebDAV", + "webdav.restore.button": "Restore from WebDAV", + "data.title": "Data Directory", + "app_data": "App Data", + "app_logs": "App Logs", + "clear_cache": { + "title": "Clear Cache", + "button": "Clear Cache", + "confirm": "Clearing the cache will delete application cache data, including minapp data. This action is irreversible, continue?", + "success": "Cache cleared", + "error": "Error clearing cache" + } } }, "translate": { diff --git a/src/renderer/src/i18n/locales/ru-ru.json b/src/renderer/src/i18n/locales/ru-ru.json index 7c383103..80c21db9 100644 --- a/src/renderer/src/i18n/locales/ru-ru.json +++ b/src/renderer/src/i18n/locales/ru-ru.json @@ -259,7 +259,6 @@ "settings": { "title": "Настройки", "general": "Общие настройки", - "data": "Настройки данных", "model": "Модель по умолчанию", "assistant": "Ассистент по умолчанию", "about": "О программе и обратная связь", @@ -283,15 +282,6 @@ "general.reset.title": "Сброс данных", "general.reset.button": "Сброс", "general.manually_check_update.title": "Отключить проверку обновлений", - "data.webdav.title": "WebDAV", - "data.webdav.host": "Хост WebDAV", - "data.webdav.host.placeholder": "http://localhost:8080", - "data.webdav.user": "Пользователь WebDAV", - "data.webdav.password": "Пароль WebDAV", - "data.webdav.path": "Путь WebDAV", - "data.webdav.path.placeholder": "/backup", - "data.webdav.backup.button": "Резервное копирование на WebDAV", - "data.webdav.restore.button": "Восстановление с WebDAV", "advanced.title": "Расширенные настройки", "advanced.auto_switch_to_topics": "Автоматически переключаться на топик", "provider.api_key": "Ключ API", @@ -356,9 +346,6 @@ "topic.position.right": "Справа", "topic.show.time": "Показывать время топика", "display.title": "Настройки отображения", - "data.title": "Каталог данных", - "data.app_data": "Данные приложения", - "data.app_logs": "Логи приложения", "shortcuts": { "title": "Горячие клавиши", "action": "Действие", @@ -403,6 +390,28 @@ "custom": "Пользовательский прокси", "none": "Не использовать прокси" } + }, + "data": { + "title": "Настройки данных", + "webdav.title": "WebDAV", + "webdav.host": "Хост WebDAV", + "webdav.host.placeholder": "http://localhost:8080", + "webdav.user": "Пользователь WebDAV", + "webdav.password": "Пароль WebDAV", + "webdav.path": "Путь WebDAV", + "webdav.path.placeholder": "/backup", + "webdav.backup.button": "Резервное копирование на WebDAV", + "webdav.restore.button": "Восстановление с WebDAV", + "data.title": "Каталог данных", + "app_data": "Данные приложения", + "app_logs": "Логи приложения", + "clear_cache": { + "title": "Очистка кэша", + "button": "Очистка кэша", + "confirm": "Очистка кэша удалит данные приложения. Это действие необратимо, продолжить?", + "success": "Кэш очищен", + "error": "Ошибка при очистке кэша" + } } }, "translate": { diff --git a/src/renderer/src/i18n/locales/zh-cn.json b/src/renderer/src/i18n/locales/zh-cn.json index 87041ea6..87128d36 100644 --- a/src/renderer/src/i18n/locales/zh-cn.json +++ b/src/renderer/src/i18n/locales/zh-cn.json @@ -259,7 +259,6 @@ "settings": { "title": "设置", "general": "常规设置", - "data": "数据设置", "model": "默认模型", "assistant": "默认助手", "about": "关于我们", @@ -283,15 +282,6 @@ "general.reset.button": "重置", "general.view_webdav_settings": "查看 WebDAV 设置", "general.manually_check_update.title": "关闭更新检测", - "data.webdav.title": "WebDAV", - "data.webdav.host": "WebDAV 地址", - "data.webdav.host.placeholder": "http://localhost:8080", - "data.webdav.user": "WebDAV 用户名", - "data.webdav.password": "WebDAV 密码", - "data.webdav.path": "WebDAV 路径", - "data.webdav.path.placeholder": "/backup", - "data.webdav.backup.button": "备份到 WebDAV", - "data.webdav.restore.button": "从 WebDAV 恢复", "advanced.title": "高级设置", "advanced.auto_switch_to_topics": "自动切换到话题", "models.default_assistant_model": "默认助手模型", @@ -321,7 +311,7 @@ "about.title": "关于我们", "about.releases.title": "更新日志", "about.releases.button": "查看", - "about.website.title": "官方网���", + "about.website.title": "官方网站", "about.website.button": "查看", "about.feedback.title": "意见反馈", "about.feedback.button": "反馈", @@ -344,9 +334,6 @@ "topic.position.right": "右侧", "topic.show.time": "显示话题时间", "display.title": "显示设置", - "data.title": "数据目录", - "data.app_data": "应用数据", - "data.app_logs": "应用日志", "shortcuts": { "title": "快捷方式", "action": "操作", @@ -391,6 +378,28 @@ "custom": "自定义代理", "none": "不使用代理" } + }, + "data": { + "title": "数据设置", + "webdav.title": "WebDAV", + "webdav.host": "WebDAV 地址", + "webdav.host.placeholder": "http://localhost:8080", + "webdav.user": "WebDAV 用户名", + "webdav.password": "WebDAV 密码", + "webdav.path": "WebDAV 路径", + "webdav.path.placeholder": "/backup", + "webdav.backup.button": "备份到 WebDAV", + "webdav.restore.button": "从 WebDAV 恢复", + "data.title": "数据目录", + "app_data": "应用数据", + "app_logs": "应用日志", + "clear_cache": { + "title": "清除缓存", + "button": "清除缓存", + "confirm": "清除缓存将删除应用缓存的数据,包括小程序数据。此操作不可恢复,是否继续?", + "success": "缓存清除成功", + "error": "清除缓存失败" + } } }, "translate": { diff --git a/src/renderer/src/i18n/locales/zh-tw.json b/src/renderer/src/i18n/locales/zh-tw.json index 7e7d2331..02193a7e 100644 --- a/src/renderer/src/i18n/locales/zh-tw.json +++ b/src/renderer/src/i18n/locales/zh-tw.json @@ -259,7 +259,6 @@ "settings": { "title": "設定", "general": "一般設定", - "data": "數據設定", "model": "預設模型", "assistant": "預設助手", "about": "關於與回饋", @@ -283,15 +282,6 @@ "general.reset.title": "資料重置", "general.reset.button": "重置", "general.manually_check_update.title": "關閉更新檢查", - "data.webdav.title": "WebDAV", - "data.webdav.host": "WebDAV 主機位址", - "data.webdav.host.placeholder": "http://localhost:8080", - "data.webdav.user": "WebDAV 使用者名稱", - "data.webdav.password": "WebDAV 密碼", - "data.webdav.path": "WebDAV Path", - "data.webdav.path.placeholder": "/backup", - "data.webdav.backup.button": "從 WebDAV 備份", - "data.webdav.restore.button": "從 WebDAV 恢復", "advanced.title": "進階設定", "advanced.auto_switch_to_topics": "自動切換到話題", "models.default_assistant_model": "預設助手模型", @@ -344,9 +334,6 @@ "topic.position.right": "右側", "topic.show.time": "顯示話題時間", "display.title": "顯示設定", - "data.title": "數據目錄", - "data.app_data": "應用數據", - "data.app_logs": "應用日誌", "shortcuts": { "title": "快速方式", "action": "操作", @@ -391,6 +378,28 @@ "custom": "自定義代理", "none": "不使用代理" } + }, + "data": { + "title": "數據設定", + "webdav.title": "WebDAV", + "webdav.host": "WebDAV 主機位址", + "webdav.host.placeholder": "http://localhost:8080", + "webdav.user": "WebDAV 使用者名稱", + "webdav.password": "WebDAV 密碼", + "webdav.path": "WebDAV Path", + "webdav.path.placeholder": "/backup", + "webdav.backup.button": "從 WebDAV 備份", + "webdav.restore.button": "從 WebDAV 恢復", + "data.title": "數據目錄", + "data.app_data": "應用數據", + "data.app_logs": "應用日誌", + "clear_cache": { + "title": "清除緩存", + "button": "清除緩存", + "confirm": "清除緩存將刪除應用緩存數據,包括小程序數據。此操作不可恢復,是否繼續?", + "success": "緩存清除成功", + "error": "清除緩存失敗" + } } }, "translate": { diff --git a/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx b/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx index 1d0b93aa..b5135cb5 100644 --- a/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx +++ b/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx @@ -3,7 +3,7 @@ import { HStack } from '@renderer/components/Layout' import { useTheme } from '@renderer/context/ThemeProvider' import { backup, reset, restore } from '@renderer/services/BackupService' import { AppInfo } from '@renderer/types' -import { Button, Typography } from 'antd' +import { Button, message, Modal, Typography } from 'antd' import { FC, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import styled from 'styled-components' @@ -30,10 +30,30 @@ const DataSettings: FC = () => { } } + const handleClearCache = () => { + Modal.confirm({ + title: t('settings.data.clear_cache.title'), + content: t('settings.data.clear_cache.confirm'), + okText: t('settings.data.clear_cache.button'), + centered: true, + okButtonProps: { + danger: true + }, + onOk: async () => { + try { + await window.api.clearCache() + message.success(t('settings.data.clear_cache.success')) + } catch (error) { + message.error(t('settings.data.clear_cache.error')) + } + } + }) + } + return ( - {t('settings.data')} + {t('settings.data.title')} {t('settings.general.backup.title')} @@ -60,7 +80,7 @@ const DataSettings: FC = () => { - {t('settings.data.title')} + {t('settings.data.data.title')} {t('settings.data.app_data')} @@ -77,6 +97,15 @@ const DataSettings: FC = () => { handleOpenPath(appInfo?.logsPath)} /> + + + {t('settings.data.clear_cache.title')} + + + + ) diff --git a/src/renderer/src/pages/settings/SettingsPage.tsx b/src/renderer/src/pages/settings/SettingsPage.tsx index 39440934..10849922 100644 --- a/src/renderer/src/pages/settings/SettingsPage.tsx +++ b/src/renderer/src/pages/settings/SettingsPage.tsx @@ -57,7 +57,7 @@ const SettingsPage: FC = () => { - {t('settings.data')} + {t('settings.data.title')}