pull/2114/head
kunfei 2 years ago
parent a6c6e76343
commit 1e6f0e5b97
  1. 4
      app/src/main/assets/defaultData/directLinkUpload.json
  2. 3
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  3. 3
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  4. 11
      app/src/main/java/io/legado/app/help/CacheManager.kt
  5. 65
      app/src/main/java/io/legado/app/help/DirectLinkUpload.kt
  6. 6
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  7. 11
      app/src/main/java/io/legado/app/help/storage/Restore.kt
  8. 2
      app/src/main/java/io/legado/app/ui/about/DonateFragment.kt
  9. 10
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  10. 2
      app/src/main/java/io/legado/app/ui/book/local/rule/TxtTocRuleActivity.kt
  11. 2
      app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt
  12. 2
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt
  13. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  14. 6
      app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt
  15. 14
      app/src/main/java/io/legado/app/ui/config/DirectLinkUploadConfig.kt
  16. 4
      app/src/main/java/io/legado/app/ui/main/explore/ExploreAdapter.kt
  17. 2
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt
  18. 6
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  19. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  20. 6
      app/src/main/java/io/legado/app/utils/ACache.kt

@ -1,5 +1,5 @@
{
"UploadUrl": "http://sy.mgz6.cc/shuyuan,{\"method\":\"POST\",\"body\": {\"file\": \"fileRequest\"},\"type\": \"multipart/form-data\"}",
"DownloadUrlRule": "$.data@js:if (result == '') \n '' \n else \n 'http://shuyuan.mgz6.cc/shuyuan/' + result",
"uploadUrl": "http://sy.mgz6.cc/shuyuan,{\"method\":\"POST\",\"body\": {\"file\": \"fileRequest\"},\"type\": \"multipart/form-data\"}",
"downloadUrlRule": "$.data@js:if (result == '') \n '' \n else \n 'http://shuyuan.mgz6.cc/shuyuan/' + result",
"summary": "有效期2天"
}

@ -10,7 +10,6 @@ import io.legado.app.help.SourceAnalyzer
import io.legado.app.utils.*
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import splitties.init.appCtx
import java.io.InputStream
@Suppress("unused")
@ -98,7 +97,7 @@ data class BookSource(
if (exploreUrl.startsWith("<js>", false)
|| exploreUrl.startsWith("@js:", false)
) {
val aCache = ACache.get(appCtx, "explore")
val aCache = ACache.get("explore")
ruleStr = aCache.getAsString(bookSourceUrl) ?: ""
if (ruleStr.isBlank()) {
val jsStr = if (exploreUrl.startsWith("@")) {

@ -8,7 +8,6 @@ import androidx.room.PrimaryKey
import com.jayway.jsonpath.DocumentContext
import io.legado.app.utils.*
import kotlinx.parcelize.Parcelize
import splitties.init.appCtx
@Parcelize
@Entity(tableName = "rssSources", indices = [(Index(value = ["sourceUrl"], unique = false))])
@ -121,7 +120,7 @@ data class RssSource(
if (sortUrl?.startsWith("<js>", false) == true
|| sortUrl?.startsWith("@js:", false) == true
) {
val aCache = ACache.get(appCtx, "rssSortUrl")
val aCache = ACache.get("rssSortUrl")
a = aCache.getAsString(sourceUrl) ?: ""
if (a.isBlank()) {
val jsStr = if (sortUrl!!.startsWith("@")) {

@ -5,7 +5,6 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Cache
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.utils.ACache
import splitties.init.appCtx
@Suppress("unused")
object CacheManager {
@ -22,7 +21,7 @@ object CacheManager {
if (saveTime == 0) 0 else System.currentTimeMillis() + saveTime * 1000
when (value) {
is QueryTTF -> queryTTFMap[key] = Pair(deadline, value)
is ByteArray -> ACache.get(appCtx).put(key, value, saveTime)
is ByteArray -> ACache.get().put(key, value, saveTime)
else -> {
val cache = Cache(key, value.toString(), deadline)
putMemory(key, value.toString())
@ -73,7 +72,7 @@ object CacheManager {
}
fun getByteArray(key: String): ByteArray? {
return ACache.get(appCtx).getAsBinary(key)
return ACache.get().getAsBinary(key)
}
fun getQueryTTF(key: String): QueryTTF? {
@ -87,16 +86,16 @@ object CacheManager {
}
fun putFile(key: String, value: String, saveTime: Int = 0) {
ACache.get(appCtx).put(key, value, saveTime)
ACache.get().put(key, value, saveTime)
}
fun getFile(key: String): String? {
return ACache.get(appCtx).getAsString(key)
return ACache.get().getAsString(key)
}
fun delete(key: String) {
appDb.cacheDao.delete(key)
deleteMemory(key)
ACache.get(appCtx).remove(key)
ACache.get().remove(key)
}
}

@ -3,25 +3,27 @@ package io.legado.app.help
import io.legado.app.exception.NoStackTraceException
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.jsonPath
import io.legado.app.utils.readString
import io.legado.app.utils.ACache
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject
import splitties.init.appCtx
import java.io.File
@Suppress("MemberVisibilityCanBePrivate")
object DirectLinkUpload {
private const val uploadUrlKey = "directLinkUploadUrl"
private const val downloadUrlRuleKey = "directLinkDownloadUrlRule"
private const val summaryKey = "directSummary"
const val ruleFileName = "directLinkUploadRule.json"
@Throws(NoStackTraceException::class)
suspend fun upLoad(fileName: String, file: Any, contentType: String): String {
val url = getUploadUrl()
if (url.isNullOrBlank()) {
val rule = defaultRule ?: getConfigRule()
rule ?: throw NoStackTraceException("直链上传规则未配置")
val url = rule.uploadUrl
if (url.isBlank()) {
throw NoStackTraceException("上传url未配置")
}
val downloadUrlRule = getDownloadUrlRule()
if (downloadUrlRule.isNullOrBlank()) {
val downloadUrlRule = rule.downloadUrlRule
if (downloadUrlRule.isBlank()) {
throw NoStackTraceException("下载地址规则未配置")
}
val analyzeUrl = AnalyzeUrl(url)
@ -34,49 +36,36 @@ object DirectLinkUpload {
return downloadUrl
}
private val ruleDoc by lazy {
private val defaultRule: Rule? by lazy {
val json = String(
appCtx.assets.open("defaultData${File.separator}directLinkUpload.json")
.readBytes()
)
jsonPath.parse(json)
GSON.fromJsonObject<Rule>(json).getOrNull()
}
fun getUploadUrl(): String? {
return CacheManager.get(uploadUrlKey)
?: ruleDoc.readString("$.UploadUrl")
fun getConfigRule(): Rule? {
val json = ACache.get(cacheDir = false).getAsString(ruleFileName)
return GSON.fromJsonObject<Rule>(json).getOrNull()
}
fun putUploadUrl(url: String) {
CacheManager.put(uploadUrlKey, url)
fun putConfigRule(uploadUrl: String, downloadUrlRule: String, summary: String?) {
val rule = Rule(uploadUrl, downloadUrlRule, summary)
ACache.get(cacheDir = false).put(ruleFileName, GSON.toJson(rule))
}
fun getDownloadUrlRule(): String? {
return CacheManager.get(downloadUrlRuleKey)
?: ruleDoc.readString("$.DownloadUrlRule")
}
fun putDownloadUrlRule(rule: String) {
CacheManager.put(downloadUrlRuleKey, rule)
fun delConfigRule() {
ACache.get(cacheDir = false).remove(ruleFileName)
}
fun getSummary(): String? {
return CacheManager.get(summaryKey)
?: ruleDoc.readString("summary")
return getConfigRule()?.summary
}
fun putSummary(summary: String?) {
if (summary != null) {
CacheManager.put(summaryKey, summary)
} else {
CacheManager.delete(summaryKey)
}
}
fun delete() {
CacheManager.delete(uploadUrlKey)
CacheManager.delete(downloadUrlRuleKey)
CacheManager.delete(summaryKey)
}
data class Rule(
var uploadUrl: String,
var downloadUrlRule: String,
var summary: String?
)
}

@ -7,6 +7,7 @@ import io.legado.app.constant.AppLog
import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb
import io.legado.app.help.AppWebDav
import io.legado.app.help.DirectLinkUpload
import io.legado.app.help.config.LocalConfig
import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ThemeConfig
@ -41,6 +42,7 @@ object Backup {
"txtTocRule.json",
"httpTTS.json",
"keyboardAssists.json",
DirectLinkUpload.ruleFileName,
ReadBookConfig.configFileName,
ReadBookConfig.shareConfigFileName,
ThemeConfig.configFileName,
@ -92,6 +94,10 @@ object Backup {
FileUtils.createFileIfNotExist(backupPath + File.separator + ThemeConfig.configFileName)
.writeText(it)
}
DirectLinkUpload.getConfigRule()?.let {
FileUtils.createFileIfNotExist(backupPath + File.separator + DirectLinkUpload.ruleFileName)
.writeText(GSON.toJson(it))
}
Preferences.getSharedPreferences(appCtx, backupPath, "config")?.let { sp ->
val edit = sp.edit()
appCtx.defaultSharedPreferences.all.forEach { (key, value) ->

@ -11,6 +11,7 @@ import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb
import io.legado.app.data.entities.*
import io.legado.app.help.DirectLinkUpload
import io.legado.app.help.LauncherIconHelp
import io.legado.app.help.config.LocalConfig
import io.legado.app.help.config.ReadBookConfig
@ -118,6 +119,16 @@ object Restore {
suspend fun restoreConfig(path: String = Backup.backupPath) {
withContext(IO) {
try {
val file =
FileUtils.createFileIfNotExist("$path${File.separator}${DirectLinkUpload.ruleFileName}")
if (file.exists()) {
val json = file.readText()
ACache.get(cacheDir = false).put(DirectLinkUpload.ruleFileName, json)
}
} catch (e: Exception) {
AppLog.put("直链上传出错\n${e.localizedMessage}", e)
}
try {
val file =
FileUtils.createFileIfNotExist("$path${File.separator}${ThemeConfig.configFileName}")

@ -53,7 +53,7 @@ class DonateFragment : PreferenceFragmentCompat() {
} catch (e: Exception) {
e.printOnDebug()
} finally {
ACache.get(requireContext(), cacheDir = false)
ACache.get(cacheDir = false)
.put("proTime", System.currentTimeMillis())
}
}

@ -54,11 +54,11 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
private val exportDir = registerForActivityResult(HandleFileContract()) { result ->
result.uri?.let { uri ->
if (uri.isContentScheme()) {
ACache.get(this@CacheActivity).put(exportBookPathKey, uri.toString())
ACache.get().put(exportBookPathKey, uri.toString())
startExport(uri.toString(), result.requestCode)
} else {
uri.path?.let { path ->
ACache.get(this@CacheActivity).put(exportBookPathKey, path)
ACache.get().put(exportBookPathKey, path)
startExport(path, result.requestCode)
}
}
@ -244,7 +244,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
}
override fun export(position: Int) {
val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey)
val path = ACache.get().getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) {
selectExportFolder(position)
} else {
@ -253,7 +253,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
}
private fun exportAll() {
val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey)
val path = ACache.get().getAsString(exportBookPathKey)
if (path.isNullOrEmpty()) {
selectExportFolder(-10)
} else {
@ -263,7 +263,7 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
private fun selectExportFolder(exportPosition: Int) {
val default = arrayListOf<SelectItem<Int>>()
val path = ACache.get(this@CacheActivity).getAsString(exportBookPathKey)
val path = ACache.get().getAsString(exportBookPathKey)
if (!path.isNullOrEmpty()) {
default.add(SelectItem(path, -1))
}

@ -156,7 +156,7 @@ class TxtTocRuleActivity : VMBaseActivity<ActivityTxtTocRuleBinding, TxtTocRuleV
@SuppressLint("InflateParams")
private fun showImportDialog() {
val aCache = ACache.get(this, cacheDir = false)
val aCache = ACache.get(cacheDir = false)
val defaultUrl = "https://gitee.com/fisher52/YueDuJson/raw/master/myTxtChapterRule.json"
val cacheUrls: MutableList<String> = aCache
.getAsString(importTocRuleKey)

@ -163,7 +163,7 @@ class SpeakEngineDialog(val callBack: CallBack) : BaseDialogFragment(R.layout.di
}
private fun importAlert() {
val aCache = ACache.get(requireContext(), cacheDir = false)
val aCache = ACache.get(cacheDir = false)
val cacheUrls: MutableList<String> = aCache
.getAsString(ttsUrlKey)
?.splitNotBlank(",")

@ -127,7 +127,7 @@ class TocRegexDialog() : BaseDialogFragment(R.layout.dialog_toc_regex),
@SuppressLint("InflateParams")
private fun showImportDialog() {
val aCache = ACache.get(requireContext(), cacheDir = false)
val aCache = ACache.get(cacheDir = false)
val defaultUrl = "https://gitee.com/fisher52/YueDuJson/raw/master/myTxtChapterRule.json"
val cacheUrls: MutableList<String> = aCache
.getAsString(importTocRuleKey)

@ -447,7 +447,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
@SuppressLint("InflateParams")
private fun showImportDialog() {
val aCache = ACache.get(this, cacheDir = false)
val aCache = ACache.get(cacheDir = false)
val cacheUrls: MutableList<String> = aCache
.getAsString(importRecordKey)
?.splitNotBlank(",")

@ -38,7 +38,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
private var webPic: String? = null
private val saveImage = registerForActivityResult(HandleFileContract()) {
it.uri?.let { uri ->
ACache.get(this).put(imagePathKey, uri.toString())
ACache.get().put(imagePathKey, uri.toString())
viewModel.saveImage(webPic, uri.toString())
}
}
@ -140,7 +140,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
private fun saveImage(webPic: String) {
this.webPic = webPic
val path = ACache.get(this).getAsString(imagePathKey)
val path = ACache.get().getAsString(imagePathKey)
if (path.isNullOrEmpty()) {
selectSaveFolder()
} else {
@ -149,7 +149,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
}
private fun selectSaveFolder() {
val default = arrayListOf<SelectItem<Int>>()
val path = ACache.get(this).getAsString(imagePathKey)
val path = ACache.get().getAsString(imagePathKey)
if (!path.isNullOrEmpty()) {
default.add(SelectItem(path, -1))
}

@ -24,14 +24,16 @@ class DirectLinkUploadConfig : BaseDialogFragment(R.layout.dialog_direct_link_up
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
binding.toolBar.setBackgroundColor(primaryColor)
binding.editUploadUrl.setText(DirectLinkUpload.getUploadUrl())
binding.editDownloadUrlRule.setText(DirectLinkUpload.getDownloadUrlRule())
binding.editSummary.setText(DirectLinkUpload.getSummary())
DirectLinkUpload.getConfigRule()?.let {
binding.editUploadUrl.setText(it.uploadUrl)
binding.editDownloadUrlRule.setText(it.downloadUrlRule)
binding.editSummary.setText(it.summary)
}
binding.tvCancel.onClick {
dismiss()
}
binding.tvFooterLeft.onClick {
DirectLinkUpload.delete()
DirectLinkUpload.delConfigRule()
dismiss()
}
binding.tvOk.onClick {
@ -46,9 +48,7 @@ class DirectLinkUploadConfig : BaseDialogFragment(R.layout.dialog_direct_link_up
toastOnUi("下载Url规则不能为空")
return@onClick
}
DirectLinkUpload.putUploadUrl(uploadUrl)
DirectLinkUpload.putDownloadUrlRule(downloadUrlRule)
DirectLinkUpload.putSummary(summary)
DirectLinkUpload.putConfigRule(uploadUrl, downloadUrlRule, summary)
dismiss()
}
}

@ -77,7 +77,7 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
}
private fun upKindList(flexbox: FlexboxLayout, sourceUrl: String, kinds: List<ExploreKind>) {
if (!kinds.isNullOrEmpty()) kotlin.runCatching {
if (kinds.isNotEmpty()) kotlin.runCatching {
recyclerFlexbox(flexbox)
flexbox.visible()
kinds.forEach { kind ->
@ -168,7 +168,7 @@ class ExploreAdapter(context: Context, val callBack: CallBack) :
putExtra("key", source.bookSourceUrl)
}
R.id.menu_refresh -> Coroutine.async(callBack.scope) {
ACache.get(context, "explore").remove(source.bookSourceUrl)
ACache.get("explore").remove(source.bookSourceUrl)
}.onSuccess {
callBack.refreshData()
}

@ -266,7 +266,7 @@ class ReplaceRuleActivity : VMBaseActivity<ActivityReplaceRuleBinding, ReplaceRu
@SuppressLint("InflateParams")
private fun showImportDialog() {
val aCache = ACache.get(this, cacheDir = false)
val aCache = ACache.get(cacheDir = false)
val cacheUrls: MutableList<String> = aCache
.getAsString(importRecordKey)
?.splitNotBlank(",")

@ -45,7 +45,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
private var webPic: String? = null
private val saveImage = registerForActivityResult(HandleFileContract()) {
it.uri?.let { uri ->
ACache.get(this).put(imagePathKey, uri.toString())
ACache.get().put(imagePathKey, uri.toString())
viewModel.saveImage(webPic, uri)
}
}
@ -154,7 +154,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
private fun saveImage(webPic: String) {
this.webPic = webPic
val path = ACache.get(this@ReadRssActivity).getAsString(imagePathKey)
val path = ACache.get().getAsString(imagePathKey)
if (path.isNullOrEmpty()) {
selectSaveFolder()
} else {
@ -164,7 +164,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
private fun selectSaveFolder() {
val default = arrayListOf<SelectItem<Int>>()
val path = ACache.get(this@ReadRssActivity).getAsString(imagePathKey)
val path = ACache.get().getAsString(imagePathKey)
if (!path.isNullOrEmpty()) {
default.add(SelectItem(path, -1))
}

@ -270,7 +270,7 @@ class RssSourceActivity : VMBaseActivity<ActivityRssSourceBinding, RssSourceView
@SuppressLint("InflateParams")
private fun showImportDialog() {
val aCache = ACache.get(this, cacheDir = false)
val aCache = ACache.get(cacheDir = false)
val cacheUrls: MutableList<String> = aCache
.getAsString(importRecordKey)
?.splitNotBlank(",")

@ -1,7 +1,6 @@
//Copyright (c) 2017. 章钦豪. All rights reserved.
package io.legado.app.utils
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
@ -11,7 +10,6 @@ import android.graphics.drawable.Drawable
import org.json.JSONArray
import org.json.JSONObject
import splitties.init.appCtx
import java.io.*
import java.util.*
import java.util.concurrent.atomic.AtomicInteger
@ -34,13 +32,13 @@ class ACache private constructor(cacheDir: File, max_size: Long, max_count: Int)
@JvmOverloads
fun get(
ctx: Context,
cacheName: String = "ACache",
maxSize: Long = MAX_SIZE.toLong(),
maxCount: Int = MAX_COUNT,
cacheDir: Boolean = true
): ACache {
val f = if (cacheDir) File(ctx.cacheDir, cacheName) else File(ctx.filesDir, cacheName)
val f =
if (cacheDir) File(appCtx.cacheDir, cacheName) else File(appCtx.filesDir, cacheName)
return get(f, maxSize, maxCount)
}

Loading…
Cancel
Save