From 3bf00d6048259816c64e33f3d8006ab604653582 Mon Sep 17 00:00:00 2001 From: dw9 Date: Sat, 18 Oct 2025 17:04:19 +0800 Subject: [PATCH 1/3] fix(sync): fix import button bug and prevent backup overwriting during import --- src/main/presenter/syncPresenter/index.ts | 25 ++++++++++++++++--- .../settings/components/DataSettings.vue | 3 +-- src/renderer/src/i18n/en-US/sync.json | 2 +- src/renderer/src/i18n/fa-IR/sync.json | 2 +- src/renderer/src/i18n/fr-FR/sync.json | 2 +- src/renderer/src/i18n/ja-JP/sync.json | 2 +- src/renderer/src/i18n/ko-KR/sync.json | 2 +- src/renderer/src/i18n/pt-BR/sync.json | 2 +- src/renderer/src/i18n/ru-RU/sync.json | 2 +- src/renderer/src/i18n/zh-CN/sync.json | 2 +- src/renderer/src/i18n/zh-HK/sync.json | 2 +- src/renderer/src/i18n/zh-TW/sync.json | 2 +- src/renderer/src/stores/sync.ts | 2 +- .../types/presenters/legacy.presenters.d.ts | 4 ++- 14 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/main/presenter/syncPresenter/index.ts b/src/main/presenter/syncPresenter/index.ts index f4422b573..3e65d48c1 100644 --- a/src/main/presenter/syncPresenter/index.ts +++ b/src/main/presenter/syncPresenter/index.ts @@ -1,6 +1,7 @@ import { app, shell } from 'electron' import path from 'path' import fs from 'fs' +import Database from 'better-sqlite3-multiple-ciphers' import { ISyncPresenter, IConfigPresenter, ISQLitePresenter } from '@shared/presenter' import { eventBus, SendTarget } from '@/eventbus' import { SYNC_EVENTS } from '@/events' @@ -121,7 +122,13 @@ export class SyncPresenter implements ISyncPresenter { */ public async importFromSync( importMode: ImportMode = ImportMode.INCREMENT - ): Promise<{ success: boolean; message: string }> { + ): Promise<{ success: boolean; message: string; count?: number }> { + // Cancel any pending backup to prevent overwriting the backup files during import + if (this.backupTimer) { + clearTimeout(this.backupTimer) + this.backupTimer = null + } + // 检查同步文件夹是否存在 const { exists, path: syncFolderPath } = await this.checkSyncFolder() if (!exists) { @@ -174,14 +181,24 @@ export class SyncPresenter implements ISyncPresenter { this.copyDirectory(this.PROVIDER_MODELS_DIR_PATH, tempProviderModelsPath) } + let importedCount = 0 try { + // Count conversations in backup db first (before any modifications) + const backupDb = new Database(dbBackupPath) + backupDb.pragma('journal_mode = WAL') + const totalInBackup = backupDb + .prepare('SELECT COUNT(*) as count FROM conversations') + .get() as { count: number } + backupDb.close() + if (importMode === ImportMode.OVERWRITE) { fs.copyFileSync(dbBackupPath, this.DB_PATH) + importedCount = totalInBackup.count } else { // 使用 DataImporter 导入数据 const importer = new DataImporter(dbBackupPath, this.DB_PATH) - const importedCount = await importer.importData() - console.log(`成功导入 ${importedCount} 个会话`) + importedCount = await importer.importData() + console.log(`成功导入 ${importedCount} 个会话 (备份中共有 ${totalInBackup.count} 个会话)`) importer.close() } // 合并 app-settings.json 文件 (排除同步相关的设置) @@ -229,7 +246,7 @@ export class SyncPresenter implements ISyncPresenter { } eventBus.send(SYNC_EVENTS.IMPORT_COMPLETED, SendTarget.ALL_WINDOWS) - return { success: true, message: 'sync.success.importComplete' } + return { success: true, message: 'sync.success.importComplete', count: importedCount } } catch (error: unknown) { console.error('导入文件失败,恢复备份:', error) diff --git a/src/renderer/settings/components/DataSettings.vue b/src/renderer/settings/components/DataSettings.vue index 97a7ea242..9abc6a354 100644 --- a/src/renderer/settings/components/DataSettings.vue +++ b/src/renderer/settings/components/DataSettings.vue @@ -69,7 +69,6 @@ @@ -218,7 +214,11 @@ : t('settings.data.importErrorTitle') }} - {{ syncStore.importResult?.message ? t(syncStore.importResult.message, { count: syncStore.importResult.count || 0 }) : '' }} + {{ + syncStore.importResult?.message + ? t(syncStore.importResult.message, { count: syncStore.importResult.count || 0 }) + : '' + }} From 08497cb758a97ddb20d7bf3b5aa5c1965389ead2 Mon Sep 17 00:00:00 2001 From: dw9 Date: Sat, 18 Oct 2025 17:59:05 +0800 Subject: [PATCH 3/3] fix(sync): fix import button bug and prevent backup overwriting during import --- src/main/presenter/syncPresenter/index.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/main/presenter/syncPresenter/index.ts b/src/main/presenter/syncPresenter/index.ts index 3e65d48c1..208fd9826 100644 --- a/src/main/presenter/syncPresenter/index.ts +++ b/src/main/presenter/syncPresenter/index.ts @@ -183,22 +183,21 @@ export class SyncPresenter implements ISyncPresenter { let importedCount = 0 try { - // Count conversations in backup db first (before any modifications) - const backupDb = new Database(dbBackupPath) - backupDb.pragma('journal_mode = WAL') - const totalInBackup = backupDb - .prepare('SELECT COUNT(*) as count FROM conversations') - .get() as { count: number } - backupDb.close() - if (importMode === ImportMode.OVERWRITE) { + // For overwrite mode, count conversations from backup db in read-only mode + const backupDb = new Database(dbBackupPath, { readonly: true }) + const result = backupDb.prepare('SELECT COUNT(*) as count FROM conversations').get() as { + count: number + } + importedCount = result.count + backupDb.close() + fs.copyFileSync(dbBackupPath, this.DB_PATH) - importedCount = totalInBackup.count } else { - // 使用 DataImporter 导入数据 + // For incremental mode, DataImporter returns the actual imported count const importer = new DataImporter(dbBackupPath, this.DB_PATH) importedCount = await importer.importData() - console.log(`成功导入 ${importedCount} 个会话 (备份中共有 ${totalInBackup.count} 个会话)`) + console.log(`成功导入 ${importedCount} 个会话`) importer.close() } // 合并 app-settings.json 文件 (排除同步相关的设置)