From a7fc8e45c054c13a755111527ec22d876b6513e1 Mon Sep 17 00:00:00 2001 From: xuncha <1658671838@qq.com> Date: Mon, 2 Feb 2026 10:25:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/main/paths.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/electron/main/paths.ts b/electron/main/paths.ts index 1c1e653c..a486d010 100644 --- a/electron/main/paths.ts +++ b/electron/main/paths.ts @@ -19,6 +19,9 @@ let _legacyDataDir: string | null = null // 存储配置文件名(放在 userData 根目录,避免受自定义数据目录影响) const STORAGE_CONFIG_FILE = 'storage.json' +// ChatLab 数据目录的标志性子目录列表(用于识别是否为 ChatLab 数据目录) +const CHATLAB_DATA_DIRS = ['databases', 'ai', 'settings', 'logs', 'temp'] + /** * 获取应用数据根目录 * 使用 userData/data 子目录,与 Electron 缓存隔离 @@ -170,8 +173,7 @@ function isDirectorySafeToUse(dirPath: string): boolean { if (entries.length === 0) return true // 如果包含 ChatLab 的标志性子目录,认为是之前的数据目录 - const chatlabDirs = ['databases', 'ai', 'settings', 'logs', 'temp'] - const hasChatlabStructure = chatlabDirs.some((d) => entries.includes(d)) + const hasChatlabStructure = CHATLAB_DATA_DIRS.some((d) => entries.includes(d)) return hasChatlabStructure } catch { return false @@ -249,7 +251,13 @@ export function setCustomDataDir( const newDir = getAppDataDir() if (migrate && oldDir !== newDir) { - copyDirMerge(oldDir, newDir) + const migrateResult = copyDirMerge(oldDir, newDir) + console.log( + `[Paths] 数据迁移完成: 复制 ${migrateResult.copied} 项, 跳过 ${migrateResult.skipped} 项, 错误 ${migrateResult.errors.length} 项` + ) + if (migrateResult.errors.length > 0) { + console.warn('[Paths] 迁移过程中出现错误:', migrateResult.errors) + } } return { success: true, from: oldDir, to: newDir } @@ -277,7 +285,13 @@ export function setCustomDataDir( _appDataDir = normalized if (migrate && oldDir !== normalized) { - copyDirMerge(oldDir, normalized) + const migrateResult = copyDirMerge(oldDir, normalized) + console.log( + `[Paths] 数据迁移完成: 复制 ${migrateResult.copied} 项, 跳过 ${migrateResult.skipped} 项, 错误 ${migrateResult.errors.length} 项` + ) + if (migrateResult.errors.length > 0) { + console.warn('[Paths] 迁移过程中出现错误:', migrateResult.errors) + } } return { success: true, from: oldDir, to: normalized } @@ -319,8 +333,7 @@ export function cleanupPendingDeleteDir(): void { // 安全检查:确保待删除目录确实是 ChatLab 数据目录(包含标志性子目录) if (fs.existsSync(pendingDir)) { const entries = fs.readdirSync(pendingDir) - const chatlabDirs = ['databases', 'ai', 'settings', 'logs', 'temp'] - const hasChatlabStructure = chatlabDirs.some((d) => entries.includes(d)) + const hasChatlabStructure = CHATLAB_DATA_DIRS.some((d) => entries.includes(d)) if (!hasChatlabStructure) { console.log('[Paths] 跳过清理:待删除目录不是 ChatLab 数据目录:', pendingDir) // 清除待删除标记