diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestoreValidator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestoreValidator.kt index 2dc959691..47f0fb1c8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestoreValidator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/AbstractBackupRestoreValidator.kt @@ -14,3 +14,5 @@ abstract class AbstractBackupRestoreValidator { data class Results(val missingSources: List, val missingTrackers: List) } + +class ValidatorParseException(e: Exception) : RuntimeException(e) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestoreValidator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestoreValidator.kt index 90e6be5c1..c938dc2bc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestoreValidator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/full/FullBackupRestoreValidator.kt @@ -4,12 +4,14 @@ import android.content.Context import android.net.Uri import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.backup.AbstractBackupRestoreValidator +import eu.kanade.tachiyomi.data.backup.ValidatorParseException import eu.kanade.tachiyomi.data.backup.full.models.BackupSerializer import okio.buffer import okio.gzip import okio.source class FullBackupRestoreValidator : AbstractBackupRestoreValidator() { + /** * Checks for critical backup file data. * @@ -19,14 +21,20 @@ class FullBackupRestoreValidator : AbstractBackupRestoreValidator() { override fun validate(context: Context, uri: Uri): Results { val backupManager = FullBackupManager(context) - val backupString = context.contentResolver.openInputStream(uri)!!.source().gzip().buffer().use { it.readByteArray() } - val backup = backupManager.parser.decodeFromByteArray(BackupSerializer, backupString) + val backup = try { + val backupString = + context.contentResolver.openInputStream(uri)!!.source().gzip().buffer() + .use { it.readByteArray() } + backupManager.parser.decodeFromByteArray(BackupSerializer, backupString) + } catch (e: Exception) { + throw ValidatorParseException(e) + } if (backup.backupManga.isEmpty()) { throw Exception(context.getString(R.string.invalid_backup_file_missing_manga)) } - val sources = backup.backupSources.map { it.sourceId to it.name }.toMap() + val sources = backup.backupSources.associate { it.sourceId to it.name } val missingSources = sources .filter { sourceManager.get(it.key) == null } .values diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestoreValidator.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestoreValidator.kt index b82544f2a..d1dafb6ce 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestoreValidator.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestoreValidator.kt @@ -4,10 +4,12 @@ import android.content.Context import android.net.Uri import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.backup.AbstractBackupRestoreValidator +import eu.kanade.tachiyomi.data.backup.ValidatorParseException import eu.kanade.tachiyomi.data.backup.legacy.models.Backup import kotlinx.serialization.json.decodeFromStream class LegacyBackupRestoreValidator : AbstractBackupRestoreValidator() { + /** * Checks for critical backup file data. * @@ -17,9 +19,13 @@ class LegacyBackupRestoreValidator : AbstractBackupRestoreValidator() { override fun validate(context: Context, uri: Uri): Results { val backupManager = LegacyBackupManager(context) - val backup = backupManager.parser.decodeFromStream( - context.contentResolver.openInputStream(uri)!! - ) + val backup = try { + backupManager.parser.decodeFromStream( + context.contentResolver.openInputStream(uri)!! + ) + } catch (e: Exception) { + throw ValidatorParseException(e) + } if (backup.version == null) { throw Exception(context.getString(R.string.invalid_backup_file_missing_data)) @@ -51,12 +57,10 @@ class LegacyBackupRestoreValidator : AbstractBackupRestoreValidator() { companion object { fun getSourceMapping(extensionsMapping: List): Map { - return extensionsMapping - .map { - val items = it.split(":") - items[0].toLong() to items[1] - } - .toMap() + return extensionsMapping.associate { + val items = it.split(":") + items[0].toLong() to items[1] + } } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index 744cc595a..76eee3a8f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -20,6 +20,7 @@ import eu.kanade.tachiyomi.data.backup.BackupConst import eu.kanade.tachiyomi.data.backup.BackupCreateService import eu.kanade.tachiyomi.data.backup.BackupCreatorJob import eu.kanade.tachiyomi.data.backup.BackupRestoreService +import eu.kanade.tachiyomi.data.backup.ValidatorParseException import eu.kanade.tachiyomi.data.backup.full.FullBackupRestoreValidator import eu.kanade.tachiyomi.data.backup.full.models.BackupFull import eu.kanade.tachiyomi.data.backup.legacy.LegacyBackupRestoreValidator @@ -262,12 +263,12 @@ class SettingsBackupController : SettingsController() { return try { var type = BackupConst.BACKUP_TYPE_FULL - val results = runCatching { + val results = try { FullBackupRestoreValidator().validate(activity, uri) - }.recoverCatching { + } catch (_: ValidatorParseException) { type = BackupConst.BACKUP_TYPE_LEGACY LegacyBackupRestoreValidator().validate(activity, uri) - }.getOrThrow() + } var message = if (type == BackupConst.BACKUP_TYPE_FULL) { activity.getString(R.string.backup_restore_content_full)