mirror of
				https://github.com/mihonapp/mihon.git
				synced 2025-10-31 14:27:57 +01:00 
			
		
		
		
	Try to show more relevant exception messages when failing to restore a backup
This commit is contained in:
		| @@ -14,3 +14,5 @@ abstract class AbstractBackupRestoreValidator { | ||||
|  | ||||
|     data class Results(val missingSources: List<String>, val missingTrackers: List<String>) | ||||
| } | ||||
|  | ||||
| class ValidatorParseException(e: Exception) : RuntimeException(e) | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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<Backup>( | ||||
|             context.contentResolver.openInputStream(uri)!! | ||||
|         ) | ||||
|         val backup = try { | ||||
|             backupManager.parser.decodeFromStream<Backup>( | ||||
|                 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<String>): Map<Long, String> { | ||||
|             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] | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user