Horis 3 лет назад
Родитель
Сommit
68ddd16b74

+ 23 - 4
app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt

@@ -127,6 +127,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
                         }
                     }.onFailure {
                         GSON.fromJsonObject<BookSource>(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<BookSource>(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<BookSource>(it).getOrThrow())
+                    uri.inputStream(context).getOrThrow().use { inputS ->
+                        GSON.fromJsonArray<BookSource>(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<BookSource>(it).getOrThrow())
+        }.byteStream().use { body ->
+            GSON.fromJsonArray<BookSource>(body).getOrThrow().let {
+                val source = it.firstOrNull() ?: return@let
+                if (source.bookSourceUrl.isEmpty()) {
+                    throw NoStackTraceException("不是书源")
+                }
+                allSources.addAll(it)
+            }
         }
     }
 

+ 18 - 4
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<List<String>>("$.sourceUrls")
                     if (!urls.isNullOrEmpty()) {
                         urls.forEach {
                             importSourceUrl(it)
                         }
-                    } else {
-                        GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
-                            allSources.addAll(it)
+                    }
+                }.onFailure {
+                    GSON.fromJsonArray<RssSource>(mText).getOrThrow().let {
+                        val source = it.firstOrNull() ?: return@let
+                        if (source.sourceUrl.isEmpty()) {
+                            throw NoStackTraceException("不是订阅源")
                         }
+                        allSources.addAll(it)
                     }
                 }
+
                 mText.isJsonArray() -> {
                     GSON.fromJsonArray<RssSource>(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<Map<String, Any>> = jsonPath.parse(body).read("$")
             for (item in items) {
+                if (!item.containsKey("sourceUrl")) {
+                    throw NoStackTraceException("不是订阅源")
+                }
                 val jsonItem = jsonPath.parse(item)
                 GSON.fromJsonObject<RssSource>(jsonItem.jsonString()).getOrThrow().let { source ->
                     allSources.add(source)