diff --git a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt index fcd1cb298..69bed152c 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt @@ -127,6 +127,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { } }.onFailure { GSON.fromJsonObject(mText).getOrThrow().let { + if (it.bookSourceUrl.isEmpty()) { + throw NoStackTraceException("不是书源") + } allSources.add(it) } } @@ -134,6 +137,10 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { mText.isJsonArray() -> GSON.fromJsonArray(mText).getOrThrow() .let { items -> + val source = items.firstOrNull() ?: return@let + if (source.bookSourceUrl.isEmpty()) { + throw NoStackTraceException("不是书源") + } allSources.addAll(items) } @@ -143,8 +150,14 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { mText.isUri() -> { val uri = Uri.parse(mText) - uri.inputStream(context).getOrThrow().use { - allSources.addAll(GSON.fromJsonArray(it).getOrThrow()) + uri.inputStream(context).getOrThrow().use { inputS -> + GSON.fromJsonArray(inputS).getOrThrow().let { + val source = it.firstOrNull() ?: return@let + if (source.bookSourceUrl.isEmpty()) { + throw NoStackTraceException("不是书源") + } + allSources.addAll(it) + } } } @@ -166,8 +179,14 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { } else { url(url) } - }.byteStream().use { - allSources.addAll(GSON.fromJsonArray(it).getOrThrow()) + }.byteStream().use { body -> + GSON.fromJsonArray(body).getOrThrow().let { + val source = it.firstOrNull() ?: return@let + if (source.bookSourceUrl.isEmpty()) { + throw NoStackTraceException("不是书源") + } + allSources.addAll(it) + } } } diff --git a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt index 05b06a5e8..1c905b9ed 100644 --- a/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt @@ -91,27 +91,38 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { execute { val mText = text.trim() when { - mText.isJsonObject() -> { + mText.isJsonObject() -> kotlin.runCatching { val json = JsonPath.parse(mText) val urls = json.read>("$.sourceUrls") if (!urls.isNullOrEmpty()) { urls.forEach { importSourceUrl(it) } - } else { - GSON.fromJsonArray(mText).getOrThrow().let { - allSources.addAll(it) - } } - } - mText.isJsonArray() -> { + }.onFailure { GSON.fromJsonArray(mText).getOrThrow().let { + val source = it.firstOrNull() ?: return@let + if (source.sourceUrl.isEmpty()) { + throw NoStackTraceException("不是订阅源") + } allSources.addAll(it) } } + + mText.isJsonArray() -> { + GSON.fromJsonArray(mText).getOrThrow().let { + val source = it.firstOrNull() ?: return@let + if (source.sourceUrl.isEmpty()) { + throw NoStackTraceException("不是订阅源") + } + allSources.addAll(it) + } + } + mText.isAbsUrl() -> { importSourceUrl(mText) } + else -> throw NoStackTraceException(context.getString(R.string.wrong_format)) } }.onError { @@ -133,6 +144,9 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { }.byteStream().let { body -> val items: List> = jsonPath.parse(body).read("$") for (item in items) { + if (!item.containsKey("sourceUrl")) { + throw NoStackTraceException("不是订阅源") + } val jsonItem = jsonPath.parse(item) GSON.fromJsonObject(jsonItem.jsonString()).getOrThrow().let { source -> allSources.add(source)