ProfileBackupOperations.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/app/profiles/ProfileBackupOperations.kt
1package eu.friedcutlet.yokan.app.profiles23import eu.friedcutlet.yokan.database.DbSettings4import eu.friedcutlet.yokan.services.files.exportFile5import eu.friedcutlet.yokan.domain.ProfileId6import eu.friedcutlet.yokan.services.backup.SafetySnapshot7import eu.friedcutlet.yokan.services.backup.BackupPlan8import eu.friedcutlet.yokan.services.backup.BackupOpenResult9import eu.friedcutlet.yokan.services.backup.RestoreMode10import eu.friedcutlet.yokan.services.backup.BackupOptions11import eu.friedcutlet.yokan.services.backup.BackupResult12import eu.friedcutlet.yokan.services.backup.BackupSection13import eu.friedcutlet.yokan.services.backup.RestorePreview1415internal data class TargetedBackupPlan(val target: ProfileId, val plan: BackupPlan)1617/** All stages carry the selected ID. Payload metadata never chooses or creates a destination. */18internal class ProfileBackupOperations(private val registry: ProfileRegistry, private val coordinator: ProfileCoordinator) {19 suspend fun export(id: ProfileId, options: BackupOptions, password: String?): BackupResult? = coordinator.withProfile(id) { owner ->20 val backup = owner.backup21 val exported = if (password.isNullOrEmpty()) backup.exportBackup(options) else backup.exportEncryptedBackup(options, password)22 val path = exportFile(exported.bytes, exported.fileName, exported.contentType,23 owner.database.settings().autoBackupLocation?.takeIf { it.isNotBlank() } ?: owner.localFiles.backupRoot)24 path?.let {25 registry.recordExport(id, it)26 BackupResult(exported.fileName, exported.bytes.size.toLong(), exported.itemCount, it)27 }28 }29 suspend fun open(id: ProfileId, bytes: ByteArray, password: String?): BackupOpenResult = coordinator.withProfile(id) { owner ->30 when (val opened = owner.backup.openBackup(bytes, password)) {31 is BackupOpenResult.Ready -> BackupOpenResult.Ready(profileRestorePlan(opened.plan, owner.database.settings()))32 is BackupOpenResult.Failure -> opened33 }34 }35 suspend fun read(id: ProfileId, path: String): ByteArray = coordinator.withProfile(id) { it.backup.readBackupFile(path) }36 suspend fun apply(target: TargetedBackupPlan, mode: RestoreMode): RestorePreview = coordinator.replaceProfileData(target.target) { owner ->37 // Re-read destination device fields after final progress has drained; never adopt imported grants.38 val plan = profileRestorePlan(target.plan, owner.database.settings()).withMode(mode)39 owner.backup.applyBackup(plan, mode)40 }41 suspend fun snapshots(id: ProfileId): List<SafetySnapshot> = coordinator.withProfile(id) { it.backup.listSafetySnapshots() }42 suspend fun rollback(id: ProfileId, snapshot: SafetySnapshot): RestorePreview = coordinator.replaceProfileData(id) { owner ->43 require(snapshot in owner.backup.listSafetySnapshots()) { "Safety snapshot belongs to another target or no longer exists" }44 owner.backup.rollbackSafetySnapshot(snapshot)45 }46}4748/** Profile data can move; OS permissions, native paths and destination credentials stay with their owner. */49internal fun profileRestorePlan(plan: BackupPlan, current: DbSettings): BackupPlan {50 val incoming = plan.snapshot51 val settings = incoming.settings?.copy(52 cookiesList = current.cookiesList,53 sourcePreferences = current.sourcePreferences,54 downloadLocation = current.downloadLocation,55 autoBackupLocation = current.autoBackupLocation,56 localFolders = current.localFolders,57 namedLocalFolders = current.namedLocalFolders,58 downloadLocalFolderName = current.downloadLocalFolderName,59 jrePath = current.jrePath,60 extensionServerPath = current.extensionServerPath,61 androidProxyServer = current.androidProxyServer,62 themeSeedImagePath = current.themeSeedImagePath,63 lastTrackerLibraryLocation = current.lastTrackerLibraryLocation,64 desktopLaunchAtLogin = current.desktopLaunchAtLogin,65 backupEncryptionEnabled = current.backupEncryptionEnabled,66 backupEncryptionPassword = current.backupEncryptionPassword,67 metadataServerUrl = current.metadataServerUrl,68 metadataServerToken = current.metadataServerToken,69 features = current.features,70 btServerAddress = current.btServerAddress,71 btServerPort = current.btServerPort,72 )73 val sections = plan.includedSections - BackupSection.TrackPreferences74 val snapshot = incoming.copy(75 settings = settings,76 includedSections = sections.toList(),77 trackPreferences = emptyList(),78 chapters = incoming.chapters.map { it.copy(archivePath = "", downloadSize = null) },79 mangas = incoming.mangas.map { manga ->80 if (manga.isLocalArchive) manga.copy(link = null) else manga81 },82 )83 return BackupPlan(84 preview = plan.preview.copy(warnings = (plan.preview.warnings + "Device paths, local file links and login credentials are not imported. Downloaded files require relinking.").distinct()),85 snapshot = snapshot,86 includedSections = sections,87 resolvedPassword = null,88 mode = plan.mode,89 )90}91