From e942b8a4023be55737d9c11e5e62c7ea9b072c3f Mon Sep 17 00:00:00 2001 From: arkon Date: Mon, 6 Sep 2021 11:54:00 -0400 Subject: [PATCH] Read from streams for local source manga details and legacy backups --- app/build.gradle.kts | 4 ++- .../data/backup/legacy/LegacyBackupRestore.kt | 7 +++-- .../legacy/LegacyBackupRestoreValidator.kt | 8 +++--- .../eu/kanade/tachiyomi/source/LocalSource.kt | 26 ++++++++++++------- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 39c9a983a6..18268561c9 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -174,9 +174,11 @@ dependencies { implementation("org.conscrypt:conscrypt-android:2.5.2") // Data serialization (JSON, protobuf) - val kotlinSerializationVersion = "1.2.2" + val kotlinSerializationVersion = "1.3.0-RC" implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlinSerializationVersion") implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:$kotlinSerializationVersion") + + // TODO: remove these once they're no longer used in any extensions implementation("com.google.code.gson:gson:2.8.7") implementation("com.github.salomonbrys.kotson:kotson:2.5.0") diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestore.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestore.kt index 19c73228f1..32ab16e873 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestore.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestore.kt @@ -13,13 +13,12 @@ import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.database.models.Track import eu.kanade.tachiyomi.source.Source -import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.decodeFromJsonElement +import kotlinx.serialization.json.decodeFromStream import kotlinx.serialization.json.intOrNull import kotlinx.serialization.json.jsonPrimitive -import okio.buffer import okio.source import java.util.Date @@ -28,8 +27,8 @@ class LegacyBackupRestore(context: Context, notifier: BackupNotifier) : Abstract override suspend fun performRestore(uri: Uri): Boolean { // Read the json and create a Json Object, // cannot use the backupManager json deserializer one because its not initialized yet - val backupObject = Json.decodeFromString( - context.contentResolver.openInputStream(uri)!!.source().buffer().use { it.readUtf8() } + val backupObject = Json.decodeFromStream( + context.contentResolver.openInputStream(uri)!! ) // Get parser version 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 ac6a83bc23..b82544f2af 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 @@ -5,9 +5,7 @@ import android.net.Uri import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.backup.AbstractBackupRestoreValidator import eu.kanade.tachiyomi.data.backup.legacy.models.Backup -import kotlinx.serialization.decodeFromString -import okio.buffer -import okio.source +import kotlinx.serialization.json.decodeFromStream class LegacyBackupRestoreValidator : AbstractBackupRestoreValidator() { /** @@ -19,8 +17,8 @@ class LegacyBackupRestoreValidator : AbstractBackupRestoreValidator() { override fun validate(context: Context, uri: Uri): Results { val backupManager = LegacyBackupManager(context) - val backup = backupManager.parser.decodeFromString( - context.contentResolver.openInputStream(uri)!!.source().buffer().use { it.readUtf8() } + val backup = backupManager.parser.decodeFromStream( + context.contentResolver.openInputStream(uri)!! ) if (backup.version == null) { diff --git a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt index c82b2868db..1c4e8c3cf1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt @@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.source import android.content.Context import com.github.junrar.Archive -import com.google.gson.JsonParser import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.model.Filter import eu.kanade.tachiyomi.source.model.FilterList @@ -15,8 +14,16 @@ import eu.kanade.tachiyomi.util.lang.compareToCaseInsensitiveNaturalOrder import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.EpubFile import eu.kanade.tachiyomi.util.system.ImageUtil +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.decodeFromStream +import kotlinx.serialization.json.intOrNull +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonPrimitive import rx.Observable import timber.log.Timber +import uy.kohesive.injekt.injectLazy import java.io.File import java.io.FileInputStream import java.io.InputStream @@ -68,6 +75,8 @@ class LocalSource(private val context: Context) : CatalogueSource { } } + private val json: Json by injectLazy() + override val id = ID override val name = context.getString(R.string.local_source) override val lang = "" @@ -157,16 +166,15 @@ class LocalSource(private val context: Context) : CatalogueSource { .flatten() .firstOrNull { it.extension == "json" } ?.apply { - val reader = this.inputStream().bufferedReader() - val json = JsonParser.parseReader(reader).asJsonObject + val obj = json.decodeFromStream(inputStream()) - manga.title = json["title"]?.asString ?: manga.title - manga.author = json["author"]?.asString ?: manga.author - manga.artist = json["artist"]?.asString ?: manga.artist - manga.description = json["description"]?.asString ?: manga.description - manga.genre = json["genre"]?.asJsonArray?.joinToString(", ") { it.asString } + manga.title = obj["title"]?.jsonPrimitive?.contentOrNull ?: manga.title + manga.author = obj["author"]?.jsonPrimitive?.contentOrNull ?: manga.author + manga.artist = obj["artist"]?.jsonPrimitive?.contentOrNull ?: manga.artist + manga.description = obj["description"]?.jsonPrimitive?.contentOrNull ?: manga.description + manga.genre = obj["genre"]?.jsonArray?.joinToString(", ") { it.jsonPrimitive.content } ?: manga.genre - manga.status = json["status"]?.asInt ?: manga.status + manga.status = obj["status"]?.jsonPrimitive?.intOrNull ?: manga.status } return Observable.just(manga)