Read from streams for local source manga details and legacy backups

(cherry picked from commit e942b8a4023be55737d9c11e5e62c7ea9b072c3f)

# Conflicts:
#	app/src/main/java/eu/kanade/tachiyomi/data/backup/legacy/LegacyBackupRestore.kt
#	app/src/main/java/eu/kanade/tachiyomi/source/LocalSource.kt
This commit is contained in:
arkon 2021-09-06 11:54:00 -04:00 committed by Jobobby04
parent cf742c65aa
commit 71533fb2bc
4 changed files with 22 additions and 29 deletions

View File

@ -162,9 +162,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")

View File

@ -15,13 +15,12 @@ import eu.kanade.tachiyomi.data.database.models.Track
import eu.kanade.tachiyomi.source.Source
import exh.EXHMigrations
import exh.merged.sql.models.MergedMangaReference
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
@ -34,8 +33,8 @@ class LegacyBackupRestore(context: Context, notifier: BackupNotifier) : Abstract
// Read the json and create a Json Object,
// cannot use the backupManager json deserializer one because its not initialized yet
val backupObject = Json.decodeFromString<JsonObject>(
context.contentResolver.openInputStream(uri)!!.source().buffer().use { it.readUtf8() }
val backupObject = Json.decodeFromStream<JsonObject>(
context.contentResolver.openInputStream(uri)!!
)
// Get parser version

View File

@ -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<Backup>(
context.contentResolver.openInputStream(uri)!!.source().buffer().use { it.readUtf8() }
val backup = backupManager.parser.decodeFromStream<Backup>(
context.contentResolver.openInputStream(uri)!!
)
if (backup.version == null) {

View File

@ -16,15 +16,13 @@ import eu.kanade.tachiyomi.util.storage.DiskUtil
import eu.kanade.tachiyomi.util.storage.EpubFile
import eu.kanade.tachiyomi.util.system.ImageUtil
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okio.buffer
import okio.source
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import rx.Observable
import timber.log.Timber
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
import java.io.File
import java.io.FileInputStream
import java.io.InputStream
@ -74,14 +72,13 @@ class LocalSource(private val context: Context) : CatalogueSource {
val c = context.getString(R.string.app_name) + File.separator + "local"
return DiskUtil.getExternalStorages(context).map { File(it.absolutePath, c) }
}
// SY -->
val json = Json {
prettyPrint = true
}
// SY <--
}
private val json: Json by injectLazy()
// SY -->
private val preferences: PreferencesHelper by injectLazy()
// SY <--
override val id = ID
override val name = context.getString(R.string.local_source)
override val lang = ""
@ -94,7 +91,7 @@ class LocalSource(private val context: Context) : CatalogueSource {
override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> {
val baseDirs = getBaseDirectories(context)
// SY -->
val allowLocalSourceHiddenFolders = Injekt.get<PreferencesHelper>().allowLocalSourceHiddenFolders().get()
val allowLocalSourceHiddenFolders = preferences.allowLocalSourceHiddenFolders().get()
// SY <--
val time = if (filters === LATEST_FILTERS) System.currentTimeMillis() - LATEST_THRESHOLD else 0L
@ -172,7 +169,9 @@ class LocalSource(private val context: Context) : CatalogueSource {
} ?: return
val existingFileName = directory.listFiles()?.find { it.extension == "json" }?.name
val file = File(directory, existingFileName ?: "info.json")
file.writeText(json.encodeToString(manga.toJson()))
file.outputStream().use {
json.encodeToStream(manga.toJson(), it)
}
}
private fun SManga.toJson(): MangaJson {
@ -199,12 +198,7 @@ class LocalSource(private val context: Context) : CatalogueSource {
.flatten()
.firstOrNull { it.extension == "json" }
?.apply {
val json = json.decodeFromString<MangaJson>(
this.inputStream()
.source()
.buffer()
.use { it.readUtf8() }
)
val json = json.decodeFromStream<MangaJson>(inputStream())
manga.title = json.title ?: manga.title
manga.author = json.author ?: manga.author