From c96c644555f3d3e7e6228b257959c92b071eb13b Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 7 Sep 2021 09:45:47 +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 --- .../assets/defaultData/directLinkUpload.json | 4 +++ .../io/legado/app/help/BookSourceAnalyzer.kt | 1 - .../java/io/legado/app/help/DefaultData.kt | 1 + .../io/legado/app/help/DirectLinkUpload.kt | 29 ++++++++++--------- .../java/io/legado/app/help/LocalConfig.kt | 3 -- .../legado/app/help/storage/ImportOldData.kt | 9 ++---- .../io/legado/app/help/storage/OldBook.kt | 9 ++---- .../io/legado/app/help/storage/OldReplace.kt | 6 ++-- .../io/legado/app/help/storage/Restore.kt | 14 ++------- .../association/ImportBookSourceViewModel.kt | 14 ++++----- .../association/ImportRssSourceViewModel.kt | 9 +++--- .../source/edit/BookSourceEditViewModel.kt | 5 ++-- .../io/legado/app/utils/JsonExtensions.kt | 10 ++++++- 13 files changed, 52 insertions(+), 62 deletions(-) create mode 100644 app/src/main/assets/defaultData/directLinkUpload.json diff --git a/app/src/main/assets/defaultData/directLinkUpload.json b/app/src/main/assets/defaultData/directLinkUpload.json new file mode 100644 index 000000000..560299e59 --- /dev/null +++ b/app/src/main/assets/defaultData/directLinkUpload.json @@ -0,0 +1,4 @@ +{ + "UploadUrl": "http://shuyuan.miaogongzi.cc:6564/shuyuan,{\"method\":\"POST\",\"body\": {\"file\": \"fileRequest\"},\"type\": \"multipart/form-data\"}", + "DownloadUrlRule": "$.data@js:if (result == '') \n '' \n else \n 'https://shuyuan.miaogongzi.cc/shuyuan/' + result" +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt b/app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt index 1132a6774..909755f95 100644 --- a/app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt +++ b/app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt @@ -6,7 +6,6 @@ import io.legado.app.constant.AppConst import io.legado.app.constant.BookType import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.rule.* -import io.legado.app.help.storage.Restore.jsonPath import io.legado.app.utils.* import java.util.regex.Pattern diff --git a/app/src/main/java/io/legado/app/help/DefaultData.kt b/app/src/main/java/io/legado/app/help/DefaultData.kt index b429ba05d..94231a2fe 100644 --- a/app/src/main/java/io/legado/app/help/DefaultData.kt +++ b/app/src/main/java/io/legado/app/help/DefaultData.kt @@ -68,4 +68,5 @@ object DefaultData { fun importDefaultRssSources() { appDb.rssSourceDao.insert(*rssSources.toTypedArray()) } + } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/DirectLinkUpload.kt b/app/src/main/java/io/legado/app/help/DirectLinkUpload.kt index b9cfb414d..fdfb7a72b 100644 --- a/app/src/main/java/io/legado/app/help/DirectLinkUpload.kt +++ b/app/src/main/java/io/legado/app/help/DirectLinkUpload.kt @@ -3,6 +3,10 @@ package io.legado.app.help import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.RuleData +import io.legado.app.utils.jsonPath +import io.legado.app.utils.readString +import splitties.init.appCtx +import java.io.File object DirectLinkUpload { @@ -28,27 +32,26 @@ object DirectLinkUpload { return downloadUrl } - fun getUploadUrl(): String { + private val ruleDoc by lazy { + val json = String( + appCtx.assets.open("defaultData${File.separator}directLinkUpload.json") + .readBytes() + ) + jsonPath.parse(json) + } + + fun getUploadUrl(): String? { return CacheManager.get(uploadUrlKey) - ?: """http://shuyuan.miaogongzi.site:6564/shuyuan,{ - "method":"POST", - "body": { - "file": "fileRequest" - }, - "type": "multipart/form-data" - }""".trimMargin() + ?: ruleDoc.readString("$.UploadUrl") } fun putUploadUrl(url: String) { CacheManager.put(uploadUrlKey, url) } - fun getDownloadUrlRule(): String { + fun getDownloadUrlRule(): String? { return CacheManager.get(downloadUrlRuleKey) - ?: """ - ${'$'}.data@js:if (result == '') '' - else 'https://shuyuan.miaogongzi.site/shuyuan/'+result - """.trimIndent() + ?: ruleDoc.readString("$.DownloadUrlRule") } fun putDownloadUrlRule(rule: String) { diff --git a/app/src/main/java/io/legado/app/help/LocalConfig.kt b/app/src/main/java/io/legado/app/help/LocalConfig.kt index 012651fa1..23f2a5fb5 100644 --- a/app/src/main/java/io/legado/app/help/LocalConfig.kt +++ b/app/src/main/java/io/legado/app/help/LocalConfig.kt @@ -22,9 +22,6 @@ object LocalConfig { val bookSourcesHelpVersionIsLast: Boolean get() = isLastVersion(1, "bookSourceHelpVersion", "firstOpenBookSources") - val debugHelpVersionIsLast: Boolean - get() = isLastVersion(1, "debugHelpVersion") - val ruleHelpVersionIsLast: Boolean get() = isLastVersion(1, "ruleHelpVersion") diff --git a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt index 0e62ca68f..112cc9481 100644 --- a/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt +++ b/app/src/main/java/io/legado/app/help/storage/ImportOldData.kt @@ -6,10 +6,7 @@ import androidx.documentfile.provider.DocumentFile import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource import io.legado.app.help.BookSourceAnalyzer -import io.legado.app.utils.DocumentUtils -import io.legado.app.utils.FileUtils -import io.legado.app.utils.isContentScheme -import io.legado.app.utils.toastOnUi +import io.legado.app.utils.* import java.io.File object ImportOldData { @@ -94,9 +91,9 @@ object ImportOldData { fun importOldSource(json: String): Int { val bookSources = mutableListOf() - val items: List> = Restore.jsonPath.parse(json).read("$") + val items: List> = jsonPath.parse(json).read("$") for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) + val jsonItem = jsonPath.parse(item) BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { bookSources.add(it) } diff --git a/app/src/main/java/io/legado/app/help/storage/OldBook.kt b/app/src/main/java/io/legado/app/help/storage/OldBook.kt index bc874d549..aab5c4aff 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldBook.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldBook.kt @@ -4,19 +4,16 @@ import android.util.Log import io.legado.app.constant.AppConst import io.legado.app.data.appDb import io.legado.app.data.entities.Book -import io.legado.app.utils.readBool -import io.legado.app.utils.readInt -import io.legado.app.utils.readLong -import io.legado.app.utils.readString +import io.legado.app.utils.* object OldBook { fun toNewBook(json: String): List { val books = mutableListOf() - val items: List> = Restore.jsonPath.parse(json).read("$") + val items: List> = jsonPath.parse(json).read("$") val existingBooks = appDb.bookDao.allBookUrls.toSet() for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) + val jsonItem = jsonPath.parse(item) val book = Book() book.bookUrl = jsonItem.readString("$.noteUrl") ?: "" if (book.bookUrl.isBlank()) continue diff --git a/app/src/main/java/io/legado/app/help/storage/OldReplace.kt b/app/src/main/java/io/legado/app/help/storage/OldReplace.kt index 1b6a9a89e..66664637d 100644 --- a/app/src/main/java/io/legado/app/help/storage/OldReplace.kt +++ b/app/src/main/java/io/legado/app/help/storage/OldReplace.kt @@ -7,9 +7,9 @@ object OldReplace { fun jsonToReplaceRules(json: String): List { val replaceRules = mutableListOf() - val items: List> = Restore.jsonPath.parse(json).read("$") + val items: List> = jsonPath.parse(json).read("$") for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) + val jsonItem = jsonPath.parse(item) jsonToReplaceRule(jsonItem.jsonString())?.let { if (it.isValid()) { replaceRules.add(it) @@ -26,7 +26,7 @@ object OldReplace { } runCatching { if (replaceRule == null || replaceRule?.pattern.isNullOrBlank()) { - val jsonItem = Restore.jsonPath.parse(json.trim()) + val jsonItem = jsonPath.parse(json.trim()) val rule = ReplaceRule() rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis() rule.pattern = jsonItem.readString("$.regex") ?: "" diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 7b0777ee3..84b3e3a5a 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -5,10 +5,6 @@ import android.app.PendingIntent import android.content.Context import android.net.Uri import androidx.documentfile.provider.DocumentFile -import com.jayway.jsonpath.Configuration -import com.jayway.jsonpath.JsonPath -import com.jayway.jsonpath.Option -import com.jayway.jsonpath.ParseContext import io.legado.app.BuildConfig import io.legado.app.R import io.legado.app.constant.AppConst.androidId @@ -61,6 +57,8 @@ object Restore { PreferKey.defaultCover, PreferKey.defaultCoverDark ) + + //阅读配置 private val readPrefKeys = arrayOf( PreferKey.readStyleSelect, PreferKey.shareLayout, @@ -69,14 +67,6 @@ object Restore { PreferKey.autoReadSpeed ) - val jsonPath: ParseContext by lazy { - JsonPath.using( - Configuration.builder() - .options(Option.SUPPRESS_EXCEPTIONS) - .build() - ) - } - suspend fun restore(context: Context, path: String) { withContext(IO) { if (path.isContentScheme()) { 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 06f865397..82b64796b 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 @@ -15,11 +15,7 @@ import io.legado.app.help.SourceHelp import io.legado.app.help.http.newCall import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.text -import io.legado.app.help.storage.Restore -import io.legado.app.utils.isAbsUrl -import io.legado.app.utils.isJsonArray -import io.legado.app.utils.isJsonObject -import io.legado.app.utils.splitNotBlank +import io.legado.app.utils.* class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { var isAddGroup = false @@ -107,9 +103,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { } } mText.isJsonArray() -> { - val items: List> = Restore.jsonPath.parse(mText).read("$") + val items: List> = jsonPath.parse(mText).read("$") for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) + val jsonItem = jsonPath.parse(item) BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { allSources.add(it) } @@ -134,9 +130,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { }.text("utf-8").let { body -> when { body.isJsonArray() -> { - val items: List> = Restore.jsonPath.parse(body).read("$") + val items: List> = jsonPath.parse(body).read("$") for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) + val jsonItem = jsonPath.parse(item) BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { source -> allSources.add(source) } 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 e7d19b555..c28d4c3f7 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 @@ -13,7 +13,6 @@ import io.legado.app.help.SourceHelp import io.legado.app.help.http.newCall import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.text -import io.legado.app.help.storage.Restore import io.legado.app.utils.* class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { @@ -101,9 +100,9 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { } } mText.isJsonArray() -> { - val items: List> = Restore.jsonPath.parse(mText).read("$") + val items: List> = jsonPath.parse(mText).read("$") for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) + val jsonItem = jsonPath.parse(item) GSON.fromJsonObject(jsonItem.jsonString())?.let { allSources.add(it) } @@ -125,9 +124,9 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { okHttpClient.newCall { url(url) }.text("utf-8").let { body -> - val items: List> = Restore.jsonPath.parse(body).read("$") + val items: List> = jsonPath.parse(body).read("$") for (item in items) { - val jsonItem = Restore.jsonPath.parse(item) + val jsonItem = jsonPath.parse(item) GSON.fromJsonObject(jsonItem.jsonString())?.let { source -> allSources.add(source) } diff --git a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt index c023b4d47..4fcb45f6b 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt @@ -8,7 +8,6 @@ import io.legado.app.data.entities.BookSource import io.legado.app.help.BookSourceAnalyzer import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.okHttpClient -import io.legado.app.help.storage.Restore import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers @@ -83,8 +82,8 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat text1?.let { importSource(text1) } } text.isJsonArray() -> { - val items: List> = Restore.jsonPath.parse(text).read("$") - val jsonItem = Restore.jsonPath.parse(items[0]) + val items: List> = jsonPath.parse(text).read("$") + val jsonItem = jsonPath.parse(items[0]) BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString()) } text.isJsonObject() -> { diff --git a/app/src/main/java/io/legado/app/utils/JsonExtensions.kt b/app/src/main/java/io/legado/app/utils/JsonExtensions.kt index 84665cd8c..1e9a0289d 100644 --- a/app/src/main/java/io/legado/app/utils/JsonExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/JsonExtensions.kt @@ -1,6 +1,14 @@ package io.legado.app.utils -import com.jayway.jsonpath.ReadContext +import com.jayway.jsonpath.* + +val jsonPath: ParseContext by lazy { + JsonPath.using( + Configuration.builder() + .options(Option.SUPPRESS_EXCEPTIONS) + .build() + ) +} fun ReadContext.readString(path: String): String? = this.read(path, String::class.java)