From dfbb2022d56192327bcf0fcee49b58fe8d3c9bf8 Mon Sep 17 00:00:00 2001 From: Horis <821938089@qq.com> Date: Fri, 16 Sep 2022 13:27:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/association/BaseAssociationViewModel.kt | 2 +- .../ui/association/OnLineImportViewModel.kt | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt b/app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt index d3665bfe7..9e36dd7f6 100644 --- a/app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/BaseAssociationViewModel.kt @@ -21,7 +21,7 @@ abstract class BaseAssociationViewModel(application: Application) : BaseViewMode } } - fun importJson(json: String) { + private fun importJson(json: String) { //暂时根据文件内容判断属于什么 when { json.contains("sourceUrl") -> diff --git a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt index a71af4620..5a8aa2818 100644 --- a/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/OnLineImportViewModel.kt @@ -1,12 +1,16 @@ package io.legado.app.ui.association import android.app.Application +import androidx.core.net.toUri import io.legado.app.R import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.http.newCallResponseBody import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.text +import io.legado.app.utils.FileUtils +import io.legado.app.utils.externalFiles import okhttp3.MediaType.Companion.toMediaType +import splitties.init.appCtx class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) { @@ -72,8 +76,18 @@ class OnLineImportViewModel(app: Application) : BaseAssociationViewModel(app) { importReadConfig(rs.bytes(), finally) } else -> { - val json = rs.text("utf-8") - importJson(json) + val inputStream = rs.byteStream() + val file = FileUtils.createFileIfNotExist( + appCtx.externalFiles, + "book_cache", + "scheme_import_cache.json" + ) + file.outputStream().use { out -> + inputStream.use { + it.copyTo(out) + } + } + importJson(file.toUri()) } } }