BackupModels.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/backup/BackupModels.kt
1package eu.friedcutlet.yokan.services.backup23import eu.friedcutlet.yokan.database.DbCategory4import eu.friedcutlet.yokan.database.DbChangedPart5import eu.friedcutlet.yokan.database.DbChapter6import eu.friedcutlet.yokan.database.DbCustomButton7import eu.friedcutlet.yokan.database.DbHistory8import eu.friedcutlet.yokan.database.DbItemType9import eu.friedcutlet.yokan.database.DbManga10import eu.friedcutlet.yokan.database.DbSettings11import eu.friedcutlet.yokan.database.DbSource12import eu.friedcutlet.yokan.database.DbTrack13import eu.friedcutlet.yokan.database.DbTrackPreference14import eu.friedcutlet.yokan.database.DbUpdate15import kotlinx.serialization.Serializable16import kotlinx.serialization.json.Json1718/**19 * Output container for a backup.20 * - [Yokan]: native `.json` / `.json.gz` (gzipped JSON).21 * - [Mangayomi]: a `.backup` ZIP holding one `*.backup.db` plain-JSON entry,22 * in the schema the old Flutter Mangayomi app reads (version "2", singular23 * keys, enum-as-index). Lets the old app restore Yokan backups.24 * - [Tachiyomi]: an Animetail `Backup` protobuf Mihon/Aniyomi/Animetail restore;25 * gzipped `.tachibk`, or raw `.proto` (SyncYomi wire form) at compression level 0.26 * Novels and Yokan-only data are omitted.27 */28enum class BackupFormat { Yokan, Mangayomi, Tachiyomi }2930sealed interface BackupOpenResult {31 data class Ready(val plan: BackupPlan) : BackupOpenResult32 data class Failure(val error: BackupOpenError) : BackupOpenResult33}3435enum class BackupOpenError {36 PasswordRequired,37 AuthenticationFailed,38 Invalid,39 Unsupported,40}4142enum class RestoreMode {43 Replace,44 Merge,45}4647@Serializable48data class SafetySnapshot(49 val backupPath: String,50 val createdAt: Long,51 val description: String,52)5354class BackupPlan internal constructor(55 val preview: RestorePreview,56 internal val snapshot: BackupSnapshot,57 internal val includedSections: Set<String>,58 internal val resolvedPassword: String? = null,59 val mode: RestoreMode = RestoreMode.Replace,60) {61 fun withMode(newMode: RestoreMode): BackupPlan = BackupPlan(62 preview = preview,63 snapshot = snapshot,64 includedSections = includedSections,65 resolvedPassword = resolvedPassword,66 mode = newMode,67 )68}6970data class BackupOptions(71 val includeManga: Boolean = true,72 val includeAnime: Boolean = true,73 val includeNovel: Boolean = true,74 val includeCategories: Boolean = true,75 val includeChapters: Boolean = true,76 val includeHistory: Boolean = true,77 val includeUpdates: Boolean = true,78 val includeSettings: Boolean = true,79 /** Tracker OAuth/credential rows; explicitly opt-in like Mangayomi's option 8. */80 val includeSensitiveSettings: Boolean = false,81 val includeSources: Boolean = true,82 val includeTracks: Boolean = true,83 val includeCustomButtons: Boolean = true,84 val compressionLevel: Int? = null,85 val format: BackupFormat = BackupFormat.Yokan,86 val encrypt: Boolean = false,87 val password: String? = null,88 /** Cloud transports always disable this; native device backups include private text. */89 val includePrivateNotes: Boolean = true,90)9192data class BackupResult(93 val fileName: String,94 val bytesWritten: Long,95 val itemCount: Int,96 val path: String? = null,97)9899data class BackupExport(100 val fileName: String,101 val bytes: ByteArray,102 val itemCount: Int,103 val format: BackupFormat,104 val compressed: Boolean,105 val contentType: String,106) {107 override fun equals(other: Any?): Boolean {108 if (this === other) return true109 if (other !is BackupExport) return false110 return fileName == other.fileName &&111 bytes.contentEquals(other.bytes) &&112 itemCount == other.itemCount &&113 format == other.format &&114 compressed == other.compressed &&115 contentType == other.contentType116 }117118 override fun hashCode(): Int {119 var result = fileName.hashCode()120 result = 31 * result + bytes.contentHashCode()121 result = 31 * result + itemCount122 result = 31 * result + format.hashCode()123 result = 31 * result + compressed.hashCode()124 result = 31 * result + contentType.hashCode()125 return result126 }127}128129data class BackupFileEntry(130 val path: String,131 val fileName: String,132 val sizeBytes: Long,133 val lastModified: Long,134 val compressed: Boolean,135)136137data class RestorePreview(138 val itemCount: Int,139 val warnings: List<String>,140 val mangaCount: Int = 0,141 val animeCount: Int = 0,142 val novelCount: Int = 0,143 val categoryCount: Int = 0,144 val chapterCount: Int = 0,145 val historyCount: Int = 0,146 val updateCount: Int = 0,147 val sourceCount: Int = 0,148 val trackCount: Int = 0,149 val newSeriesCount: Int = 0,150 val updatedSeriesCount: Int = 0,151 val newChapterCount: Int = 0,152 val conflictingCategories: List<String> = emptyList(),153 val unmatchedSourceNames: List<String> = emptyList(),154)155156const val BackupSchemaVersion: Int = 1157158/** Native Yokan backup payload; also the decoded form of every external format. */159@Serializable160data class BackupSnapshot(161 val schemaVersion: Int = BackupSchemaVersion,162 val createdAtMillis: Long,163 val sourceApp: String = "yokan-kmp",164 val includedSections: List<String>? = null,165 val settings: DbSettings? = null,166 val mangas: List<DbManga> = emptyList(),167 val chapters: List<DbChapter> = emptyList(),168 val categories: List<DbCategory> = emptyList(),169 val sources: List<DbSource> = emptyList(),170 val histories: List<DbHistory> = emptyList(),171 val updates: List<DbUpdate> = emptyList(),172 val tracks: List<DbTrack> = emptyList(),173 val trackPreferences: List<DbTrackPreference> = emptyList(),174 val customButtons: List<DbCustomButton> = emptyList(),175 val changedParts: List<DbChangedPart> = emptyList(),176 /** False for cloud exports; replacement preserves matching local notes. Legacy backups replace normally. */177 val privateNotesIncluded: Boolean = true,178)179180/** Section names of [BackupSnapshot.includedSections]; restore only touches included sections. */181internal object BackupSection {182 const val Settings = "settings"183 const val Manga = "manga"184 const val Anime = "anime"185 const val Novel = "novel"186 const val Chapters = "chapters"187 const val Categories = "categories"188 const val Sources = "sources"189 const val History = "history"190 const val Updates = "updates"191 const val Tracks = "tracks"192 const val TrackPreferences = "trackPreferences"193 const val CustomButtons = "customButtons"194 const val ChangedParts = "changedParts"195196 val all = setOf(197 Settings,198 Manga,199 Anime,200 Novel,201 Chapters,202 Categories,203 Sources,204 History,205 Updates,206 Tracks,207 TrackPreferences,208 CustomButtons,209 ChangedParts,210 )211}212213/** Title types whose section is included. */214internal fun Set<String>.titleTypes(): Set<DbItemType> = buildSet {215 if (BackupSection.Manga in this@titleTypes) add(DbItemType.Manga)216 if (BackupSection.Anime in this@titleTypes) add(DbItemType.Anime)217 if (BackupSection.Novel in this@titleTypes) add(DbItemType.Novel)218}219220/** JSON settings shared by every native backup reader and writer. */221internal val BackupJson = Json {222 ignoreUnknownKeys = true223 encodeDefaults = true224 prettyPrint = false225}226