SafetySnapshotStore.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/backup/SafetySnapshotStore.kt
1package eu.friedcutlet.yokan.services.backup23import eu.friedcutlet.yokan.database.YokanDatabaseSnapshot4import eu.friedcutlet.yokan.services.files.LocalFileService5import eu.friedcutlet.yokan.util.nowMillis67/**8 * Full native backups taken right before a restore, kept under9 * `<backupRoot>/safety_snapshots` with a small JSON index. Only the newest10 * [MAX_SNAPSHOTS] are kept.11 */12internal class SafetySnapshotStore(private val files: LocalFileService) {13 private val json = BackupJson14 private val dir get() = "${files.backupRoot}/safety_snapshots"15 private val metaPath get() = "$dir/snapshots.json"1617 fun list(): List<SafetySnapshot> = readMeta().filter { files.exists(it.backupPath) }1819 fun record(snapshot: YokanDatabaseSnapshot, description: String): SafetySnapshot {20 val createdAt = nowMillis()21 val backupPath = "$dir/safety-$createdAt.json"22 val payload = BackupSnapshot(23 createdAtMillis = createdAt,24 sourceApp = "yokan-kmp",25 settings = snapshot.settings,26 includedSections = BackupSection.all.toList(),27 mangas = snapshot.mangas,28 chapters = snapshot.chapters,29 categories = snapshot.categories,30 sources = snapshot.sources,31 histories = snapshot.histories,32 updates = snapshot.updates,33 tracks = snapshot.tracks,34 trackPreferences = snapshot.trackPreferences,35 customButtons = snapshot.customButtons,36 changedParts = snapshot.changedParts,37 )38 files.writeBytes(backupPath, json.encodeToString(BackupSnapshot.serializer(), payload).encodeToByteArray())3940 val newSnapshot = SafetySnapshot(backupPath = backupPath, createdAt = createdAt, description = description)41 val updated = listOf(newSnapshot) + readMeta()42 for (old in updated.drop(MAX_SNAPSHOTS)) {43 files.deleteRecursively(old.backupPath)44 }45 writeMeta(updated.take(MAX_SNAPSHOTS))46 return newSnapshot47 }4849 fun remove(snapshot: SafetySnapshot) {50 files.deleteRecursively(snapshot.backupPath)51 val metaBytes = files.readBytes(metaPath)52 if (metaBytes.isEmpty()) return53 val remaining = decodeMeta(metaBytes).filterNot { it.backupPath == snapshot.backupPath }54 if (remaining.isEmpty()) files.deleteRecursively(metaPath) else writeMeta(remaining)55 }5657 fun clear() {58 for (s in list()) {59 files.deleteRecursively(s.backupPath)60 }61 files.deleteRecursively(metaPath)62 }6364 private fun readMeta(): List<SafetySnapshot> = decodeMeta(files.readBytes(metaPath))6566 private fun decodeMeta(metaBytes: ByteArray): List<SafetySnapshot> {67 if (metaBytes.isEmpty()) return emptyList()68 return runCatching { json.decodeFromString<List<SafetySnapshot>>(metaBytes.decodeToString()) }.getOrNull().orEmpty().filter {69 it.backupPath.startsWith("$dir/") && it.backupPath.removePrefix("$dir/").matches(Regex("safety-[0-9]+\\.json"))70 }71 }7273 private fun writeMeta(snapshots: List<SafetySnapshot>) {74 files.writeBytes(metaPath, json.encodeToString(snapshots).encodeToByteArray())75 }7677 private companion object {78 const val MAX_SNAPSHOTS = 379 }80}81