(_context: IpcContext)
| 12 | import { isInsideAppInstallDir } from '../utils/pathUtils' |
| 13 | |
| 14 | export function registerCacheHandlers(_context: IpcContext): void { |
| 15 | console.log('[IPC] Registering cache handlers (IPC-only subset)...') |
| 16 | |
| 17 | ipcMain.handle('cache:selectDataDir', async () => { |
| 18 | try { |
| 19 | const result = await dialog.showOpenDialog({ |
| 20 | properties: ['openDirectory', 'createDirectory'], |
| 21 | defaultPath: getUserDataDir(), |
| 22 | title: '选择数据目录', |
| 23 | buttonLabel: '选择', |
| 24 | }) |
| 25 | |
| 26 | if (result.canceled || result.filePaths.length === 0) { |
| 27 | return { success: false } |
| 28 | } |
| 29 | |
| 30 | const selectedPath = result.filePaths[0] |
| 31 | |
| 32 | try { |
| 33 | const exePath = app.getPath('exe') |
| 34 | if (isInsideAppInstallDir(selectedPath, exePath)) { |
| 35 | return { success: false, error: 'INSTALL_DIR_FORBIDDEN' } |
| 36 | } |
| 37 | } catch { |
| 38 | // exe path unavailable, skip check |
| 39 | } |
| 40 | |
| 41 | return { success: true, path: selectedPath } |
| 42 | } catch (error) { |
| 43 | console.error('[Cache] Error selecting data dir:', error) |
| 44 | return { success: false, error: String(error) } |
| 45 | } |
| 46 | }) |
| 47 | |
| 48 | ipcMain.handle('cache:setDataDir', async (_, payload: { path?: string | null; migrate?: boolean }) => { |
| 49 | const targetPath = typeof payload?.path === 'string' ? payload.path : null |
| 50 | const migrate = payload?.migrate !== false |
| 51 | |
| 52 | const result = setCustomDataDir(targetPath, migrate) |
| 53 | if (!result.success) { |
| 54 | return { success: false, error: result.error } |
| 55 | } |
| 56 | |
| 57 | if (result.requiresRelaunch === false) { |
| 58 | ensureAppDirs() |
| 59 | } |
| 60 | |
| 61 | return { |
| 62 | success: true, |
| 63 | from: result.from, |
| 64 | to: result.to, |
| 65 | requiresRelaunch: result.requiresRelaunch, |
| 66 | } |
| 67 | }) |
| 68 | } |
no test coverage detected