From b459234ddc783f2e987362b2bab7082f1d81725c Mon Sep 17 00:00:00 2001 From: arkon Date: Wed, 26 Jan 2022 22:43:27 -0500 Subject: [PATCH] Try to show more relevant exception messages when failing to restore a backup --- .../backup/AbstractBackupRestoreValidator.kt | 2 ++ .../backup/full/FullBackupRestoreValidator.kt | 14 +++++++++--- .../legacy/LegacyBackupRestoreValidator.kt | 22 +++++++++++-------- .../ui/setting/SettingsBackupController.kt | 7 +++--- 4 files changed, 30 insertions(+), 15 deletions(-) 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 2dc959691c..47f0fb1c84 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 90e6be5c11..c938dc2bcc 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 b82544f2af..d1dafb6ce2 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 744cc595a9..76eee3a8f4 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)