From de8ef6dad7c89afb7041ccb489d68539a4849cb5 Mon Sep 17 00:00:00 2001 From: Vetle Ledaal Date: Wed, 31 Jul 2024 16:39:41 +0200 Subject: [PATCH] Improve error message if restoring from JSON file (#1056) * Improve error message if restoring from JSON file * Replace Exception with IOException * Use more generic error message if protobuf fails * fix lint --- .../tachiyomi/data/backup/BackupDecoder.kt | 27 ++++++++++++++----- .../moko-resources/base/strings.xml | 2 ++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupDecoder.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupDecoder.kt index 34f862548..55553082f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupDecoder.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupDecoder.kt @@ -3,18 +3,21 @@ package eu.kanade.tachiyomi.data.backup import android.content.Context import android.net.Uri import eu.kanade.tachiyomi.data.backup.models.Backup +import kotlinx.serialization.SerializationException import kotlinx.serialization.protobuf.ProtoBuf import okio.buffer import okio.gzip import okio.source +import tachiyomi.core.common.i18n.stringResource +import tachiyomi.i18n.MR import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get +import java.io.IOException class BackupDecoder( private val context: Context, private val parser: ProtoBuf = Injekt.get(), ) { - /** * Decode a potentially-gzipped backup. */ @@ -26,13 +29,25 @@ class BackupDecoder( require(2) } val id1id2 = peeked.readShort() - val backupString = if (id1id2.toInt() == 0x1f8b) { // 0x1f8b is gzip magic bytes - source.gzip().buffer() - } else { - source + val backupString = when (id1id2.toInt()) { + 0x1f8b -> source.gzip().buffer() // 0x1f8b is gzip magic bytes + MAGIC_JSON_SIGNATURE1, MAGIC_JSON_SIGNATURE2, MAGIC_JSON_SIGNATURE3 -> { + throw IOException(context.stringResource(MR.strings.invalid_backup_file_json)) + } + else -> source }.use { it.readByteArray() } - parser.decodeFromByteArray(Backup.serializer(), backupString) + try { + parser.decodeFromByteArray(Backup.serializer(), backupString) + } catch (_: SerializationException) { + throw IOException(context.stringResource(MR.strings.invalid_backup_file_unknown)) + } } } + + companion object { + private const val MAGIC_JSON_SIGNATURE1 = 0x7b7d // `{}` + private const val MAGIC_JSON_SIGNATURE2 = 0x7b22 // `{"` + private const val MAGIC_JSON_SIGNATURE3 = 0x7b0a // `{\n` + } } diff --git a/i18n/src/commonMain/moko-resources/base/strings.xml b/i18n/src/commonMain/moko-resources/base/strings.xml index 08bbe2d76..73f7d4a6e 100644 --- a/i18n/src/commonMain/moko-resources/base/strings.xml +++ b/i18n/src/commonMain/moko-resources/base/strings.xml @@ -518,6 +518,8 @@ Invalid backup file: Full error: Backup does not contain any library entries. + JSON backup not supported + Backup file is corrupted Missing sources: Trackers not logged into: You may need to install any missing extensions and log in to tracking services afterwards to use them.