pull/1301/head
gedoor 3 years ago
parent c4a4517600
commit c96c644555
  1. 4
      app/src/main/assets/defaultData/directLinkUpload.json
  2. 1
      app/src/main/java/io/legado/app/help/BookSourceAnalyzer.kt
  3. 1
      app/src/main/java/io/legado/app/help/DefaultData.kt
  4. 29
      app/src/main/java/io/legado/app/help/DirectLinkUpload.kt
  5. 3
      app/src/main/java/io/legado/app/help/LocalConfig.kt
  6. 9
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  7. 9
      app/src/main/java/io/legado/app/help/storage/OldBook.kt
  8. 6
      app/src/main/java/io/legado/app/help/storage/OldReplace.kt
  9. 14
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  10. 14
      app/src/main/java/io/legado/app/ui/association/ImportBookSourceViewModel.kt
  11. 9
      app/src/main/java/io/legado/app/ui/association/ImportRssSourceViewModel.kt
  12. 5
      app/src/main/java/io/legado/app/ui/book/source/edit/BookSourceEditViewModel.kt
  13. 10
      app/src/main/java/io/legado/app/utils/JsonExtensions.kt

@ -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"
}

@ -6,7 +6,6 @@ import io.legado.app.constant.AppConst
import io.legado.app.constant.BookType import io.legado.app.constant.BookType
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.* import io.legado.app.data.entities.rule.*
import io.legado.app.help.storage.Restore.jsonPath
import io.legado.app.utils.* import io.legado.app.utils.*
import java.util.regex.Pattern import java.util.regex.Pattern

@ -68,4 +68,5 @@ object DefaultData {
fun importDefaultRssSources() { fun importDefaultRssSources() {
appDb.rssSourceDao.insert(*rssSources.toTypedArray()) appDb.rssSourceDao.insert(*rssSources.toTypedArray())
} }
} }

@ -3,6 +3,10 @@ package io.legado.app.help
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.RuleData 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 { object DirectLinkUpload {
@ -28,27 +32,26 @@ object DirectLinkUpload {
return downloadUrl 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) return CacheManager.get(uploadUrlKey)
?: """http://shuyuan.miaogongzi.site:6564/shuyuan,{ ?: ruleDoc.readString("$.UploadUrl")
"method":"POST",
"body": {
"file": "fileRequest"
},
"type": "multipart/form-data"
}""".trimMargin()
} }
fun putUploadUrl(url: String) { fun putUploadUrl(url: String) {
CacheManager.put(uploadUrlKey, url) CacheManager.put(uploadUrlKey, url)
} }
fun getDownloadUrlRule(): String { fun getDownloadUrlRule(): String? {
return CacheManager.get(downloadUrlRuleKey) return CacheManager.get(downloadUrlRuleKey)
?: """ ?: ruleDoc.readString("$.DownloadUrlRule")
${'$'}.data@js:if (result == '') ''
else 'https://shuyuan.miaogongzi.site/shuyuan/'+result
""".trimIndent()
} }
fun putDownloadUrlRule(rule: String) { fun putDownloadUrlRule(rule: String) {

@ -22,9 +22,6 @@ object LocalConfig {
val bookSourcesHelpVersionIsLast: Boolean val bookSourcesHelpVersionIsLast: Boolean
get() = isLastVersion(1, "bookSourceHelpVersion", "firstOpenBookSources") get() = isLastVersion(1, "bookSourceHelpVersion", "firstOpenBookSources")
val debugHelpVersionIsLast: Boolean
get() = isLastVersion(1, "debugHelpVersion")
val ruleHelpVersionIsLast: Boolean val ruleHelpVersionIsLast: Boolean
get() = isLastVersion(1, "ruleHelpVersion") get() = isLastVersion(1, "ruleHelpVersion")

@ -6,10 +6,7 @@ import androidx.documentfile.provider.DocumentFile
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookSourceAnalyzer import io.legado.app.help.BookSourceAnalyzer
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.*
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.toastOnUi
import java.io.File import java.io.File
object ImportOldData { object ImportOldData {
@ -94,9 +91,9 @@ object ImportOldData {
fun importOldSource(json: String): Int { fun importOldSource(json: String): Int {
val bookSources = mutableListOf<BookSource>() val bookSources = mutableListOf<BookSource>()
val items: List<Map<String, Any>> = Restore.jsonPath.parse(json).read("$") val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let {
bookSources.add(it) bookSources.add(it)
} }

@ -4,19 +4,16 @@ import android.util.Log
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.utils.readBool import io.legado.app.utils.*
import io.legado.app.utils.readInt
import io.legado.app.utils.readLong
import io.legado.app.utils.readString
object OldBook { object OldBook {
fun toNewBook(json: String): List<Book> { fun toNewBook(json: String): List<Book> {
val books = mutableListOf<Book>() val books = mutableListOf<Book>()
val items: List<Map<String, Any>> = Restore.jsonPath.parse(json).read("$") val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
val existingBooks = appDb.bookDao.allBookUrls.toSet() val existingBooks = appDb.bookDao.allBookUrls.toSet()
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
val book = Book() val book = Book()
book.bookUrl = jsonItem.readString("$.noteUrl") ?: "" book.bookUrl = jsonItem.readString("$.noteUrl") ?: ""
if (book.bookUrl.isBlank()) continue if (book.bookUrl.isBlank()) continue

@ -7,9 +7,9 @@ object OldReplace {
fun jsonToReplaceRules(json: String): List<ReplaceRule> { fun jsonToReplaceRules(json: String): List<ReplaceRule> {
val replaceRules = mutableListOf<ReplaceRule>() val replaceRules = mutableListOf<ReplaceRule>()
val items: List<Map<String, Any>> = Restore.jsonPath.parse(json).read("$") val items: List<Map<String, Any>> = jsonPath.parse(json).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
jsonToReplaceRule(jsonItem.jsonString())?.let { jsonToReplaceRule(jsonItem.jsonString())?.let {
if (it.isValid()) { if (it.isValid()) {
replaceRules.add(it) replaceRules.add(it)
@ -26,7 +26,7 @@ object OldReplace {
} }
runCatching { runCatching {
if (replaceRule == null || replaceRule?.pattern.isNullOrBlank()) { if (replaceRule == null || replaceRule?.pattern.isNullOrBlank()) {
val jsonItem = Restore.jsonPath.parse(json.trim()) val jsonItem = jsonPath.parse(json.trim())
val rule = ReplaceRule() val rule = ReplaceRule()
rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis() rule.id = jsonItem.readLong("$.id") ?: System.currentTimeMillis()
rule.pattern = jsonItem.readString("$.regex") ?: "" rule.pattern = jsonItem.readString("$.regex") ?: ""

@ -5,10 +5,6 @@ import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.documentfile.provider.DocumentFile 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.BuildConfig
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppConst.androidId import io.legado.app.constant.AppConst.androidId
@ -61,6 +57,8 @@ object Restore {
PreferKey.defaultCover, PreferKey.defaultCover,
PreferKey.defaultCoverDark PreferKey.defaultCoverDark
) )
//阅读配置
private val readPrefKeys = arrayOf( private val readPrefKeys = arrayOf(
PreferKey.readStyleSelect, PreferKey.readStyleSelect,
PreferKey.shareLayout, PreferKey.shareLayout,
@ -69,14 +67,6 @@ object Restore {
PreferKey.autoReadSpeed PreferKey.autoReadSpeed
) )
val jsonPath: ParseContext by lazy {
JsonPath.using(
Configuration.builder()
.options(Option.SUPPRESS_EXCEPTIONS)
.build()
)
}
suspend fun restore(context: Context, path: String) { suspend fun restore(context: Context, path: String) {
withContext(IO) { withContext(IO) {
if (path.isContentScheme()) { if (path.isContentScheme()) {

@ -15,11 +15,7 @@ import io.legado.app.help.SourceHelp
import io.legado.app.help.http.newCall import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text import io.legado.app.help.http.text
import io.legado.app.help.storage.Restore import io.legado.app.utils.*
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJsonArray
import io.legado.app.utils.isJsonObject
import io.legado.app.utils.splitNotBlank
class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) { class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
var isAddGroup = false var isAddGroup = false
@ -107,9 +103,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
} }
} }
mText.isJsonArray() -> { mText.isJsonArray() -> {
val items: List<Map<String, Any>> = Restore.jsonPath.parse(mText).read("$") val items: List<Map<String, Any>> = jsonPath.parse(mText).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let {
allSources.add(it) allSources.add(it)
} }
@ -134,9 +130,9 @@ class ImportBookSourceViewModel(app: Application) : BaseViewModel(app) {
}.text("utf-8").let { body -> }.text("utf-8").let { body ->
when { when {
body.isJsonArray() -> { body.isJsonArray() -> {
val items: List<Map<String, Any>> = Restore.jsonPath.parse(body).read("$") val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { source -> BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())?.let { source ->
allSources.add(source) allSources.add(source)
} }

@ -13,7 +13,6 @@ import io.legado.app.help.SourceHelp
import io.legado.app.help.http.newCall import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text import io.legado.app.help.http.text
import io.legado.app.help.storage.Restore
import io.legado.app.utils.* import io.legado.app.utils.*
class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) { class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
@ -101,9 +100,9 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
} }
} }
mText.isJsonArray() -> { mText.isJsonArray() -> {
val items: List<Map<String, Any>> = Restore.jsonPath.parse(mText).read("$") val items: List<Map<String, Any>> = jsonPath.parse(mText).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let { GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let {
allSources.add(it) allSources.add(it)
} }
@ -125,9 +124,9 @@ class ImportRssSourceViewModel(app: Application) : BaseViewModel(app) {
okHttpClient.newCall { okHttpClient.newCall {
url(url) url(url)
}.text("utf-8").let { body -> }.text("utf-8").let { body ->
val items: List<Map<String, Any>> = Restore.jsonPath.parse(body).read("$") val items: List<Map<String, Any>> = jsonPath.parse(body).read("$")
for (item in items) { for (item in items) {
val jsonItem = Restore.jsonPath.parse(item) val jsonItem = jsonPath.parse(item)
GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let { source -> GSON.fromJsonObject<RssSource>(jsonItem.jsonString())?.let { source ->
allSources.add(source) allSources.add(source)
} }

@ -8,7 +8,6 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookSourceAnalyzer import io.legado.app.help.BookSourceAnalyzer
import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.storage.Restore
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -83,8 +82,8 @@ class BookSourceEditViewModel(application: Application) : BaseViewModel(applicat
text1?.let { importSource(text1) } text1?.let { importSource(text1) }
} }
text.isJsonArray() -> { text.isJsonArray() -> {
val items: List<Map<String, Any>> = Restore.jsonPath.parse(text).read("$") val items: List<Map<String, Any>> = jsonPath.parse(text).read("$")
val jsonItem = Restore.jsonPath.parse(items[0]) val jsonItem = jsonPath.parse(items[0])
BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString()) BookSourceAnalyzer.jsonToBookSource(jsonItem.jsonString())
} }
text.isJsonObject() -> { text.isJsonObject() -> {

@ -1,6 +1,14 @@
package io.legado.app.utils 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) fun ReadContext.readString(path: String): String? = this.read(path, String::class.java)

Loading…
Cancel
Save