This commit is contained in:
Horis
2025-12-10 14:20:01 +08:00
parent 083cb297b7
commit dbc5474314
3 changed files with 37 additions and 11 deletions
@@ -2,6 +2,7 @@ package io.legado.app.help.storage
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
import androidx.core.net.toUri
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
@@ -14,6 +15,7 @@ import io.legado.app.help.config.LocalConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ThemeConfig import io.legado.app.help.config.ThemeConfig
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.BookCover
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.LogUtils import io.legado.app.utils.LogUtils
@@ -29,6 +31,7 @@ import io.legado.app.utils.openOutputStream
import io.legado.app.utils.outputStream import io.legado.app.utils.outputStream
import io.legado.app.utils.writeToOutputStream import io.legado.app.utils.writeToOutputStream
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
@@ -41,7 +44,6 @@ import java.text.SimpleDateFormat
import java.util.Date import java.util.Date
import java.util.Locale import java.util.Locale
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.coroutines.coroutineContext
/** /**
* 备份 * 备份
@@ -78,6 +80,7 @@ object Backup {
ReadBookConfig.configFileName, ReadBookConfig.configFileName,
ReadBookConfig.shareConfigFileName, ReadBookConfig.shareConfigFileName,
ThemeConfig.configFileName, ThemeConfig.configFileName,
BookCover.configFileName,
"config.xml" "config.xml"
) )
} }
@@ -152,7 +155,7 @@ object Backup {
.writeText(it) .writeText(it)
} }
} }
coroutineContext.ensureActive() currentCoroutineContext().ensureActive()
GSON.toJson(ReadBookConfig.configList).let { GSON.toJson(ReadBookConfig.configList).let {
FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.configFileName) FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.configFileName)
.writeText(it) .writeText(it)
@@ -169,7 +172,11 @@ object Backup {
FileUtils.createFileIfNotExist(backupPath + File.separator + DirectLinkUpload.ruleFileName) FileUtils.createFileIfNotExist(backupPath + File.separator + DirectLinkUpload.ruleFileName)
.writeText(GSON.toJson(it)) .writeText(GSON.toJson(it))
} }
coroutineContext.ensureActive() BookCover.getConfig()?.let {
FileUtils.createFileIfNotExist(backupPath + File.separator + BookCover.configFileName)
.writeText(GSON.toJson(it))
}
currentCoroutineContext().ensureActive()
appCtx.getSharedPreferences(backupPath, "config")?.let { sp -> appCtx.getSharedPreferences(backupPath, "config")?.let { sp ->
val edit = sp.edit() val edit = sp.edit()
appCtx.defaultSharedPreferences.all.forEach { (key, value) -> appCtx.defaultSharedPreferences.all.forEach { (key, value) ->
@@ -193,7 +200,7 @@ object Backup {
} }
edit.commit() edit.commit()
} }
coroutineContext.ensureActive() currentCoroutineContext().ensureActive()
val zipFileName = getNowZipFileName() val zipFileName = getNowZipFileName()
val paths = arrayListOf(*backupFileNames) val paths = arrayListOf(*backupFileNames)
for (i in 0 until paths.size) { for (i in 0 until paths.size) {
@@ -213,7 +220,7 @@ object Backup {
} }
path.isContentScheme() -> { path.isContentScheme() -> {
copyBackup(context, Uri.parse(path), backupFileName) copyBackup(context, path.toUri(), backupFileName)
} }
else -> { else -> {
@@ -228,7 +235,7 @@ object Backup {
} }
FileUtils.delete(backupPath) FileUtils.delete(backupPath)
FileUtils.delete(zipFilePath) FileUtils.delete(zipFilePath)
coroutineContext.ensureActive() currentCoroutineContext().ensureActive()
ReadBookConfig.getAllPicBgStr().map { ReadBookConfig.getAllPicBgStr().map {
if (it.contains(File.separator)) { if (it.contains(File.separator)) {
File(it) File(it)
@@ -241,7 +248,7 @@ object Backup {
} }
private suspend fun writeListToJson(list: List<Any>, fileName: String, path: String) { private suspend fun writeListToJson(list: List<Any>, fileName: String, path: String) {
coroutineContext.ensureActive() currentCoroutineContext().ensureActive()
withContext(IO) { withContext(IO) {
if (list.isNotEmpty()) { if (list.isNotEmpty()) {
LogUtils.d(TAG, "阅读备份 $fileName 列表大小 ${list.size}") LogUtils.d(TAG, "阅读备份 $fileName 列表大小 ${list.size}")
@@ -32,6 +32,7 @@ import io.legado.app.help.book.upType
import io.legado.app.help.config.LocalConfig import io.legado.app.help.config.LocalConfig
import io.legado.app.help.config.ReadBookConfig import io.legado.app.help.config.ReadBookConfig
import io.legado.app.help.config.ThemeConfig import io.legado.app.help.config.ThemeConfig
import io.legado.app.model.BookCover
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.ACache import io.legado.app.utils.ACache
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
@@ -211,6 +212,14 @@ object Restore {
}?.onFailure { }?.onFailure {
AppLog.put("恢复主题出错\n${it.localizedMessage}", it) AppLog.put("恢复主题出错\n${it.localizedMessage}", it)
} }
File(path, BookCover.configFileName).takeIf {
it.exists()
}?.runCatching {
val json = readText()
BookCover.saveCoverRule(json)
}?.onFailure {
AppLog.put("恢复封面规则出错\n${it.localizedMessage}", it)
}
if (!BackupConfig.ignoreReadConfig) { if (!BackupConfig.ignoreReadConfig) {
//恢复阅读界面配置 //恢复阅读界面配置
File(path, ReadBookConfig.configFileName).takeIf { File(path, ReadBookConfig.configFileName).takeIf {
@@ -36,14 +36,17 @@ import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import kotlinx.coroutines.currentCoroutineContext
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import kotlin.coroutines.coroutineContext
@Keep @Keep
@Suppress("ConstPropertyName")
object BookCover { object BookCover {
private const val coverRuleConfigKey = "legadoCoverRuleConfig" private const val coverRuleConfigKey = "legadoCoverRuleConfig"
const val configFileName = "coverRule.json"
var drawBookName = true var drawBookName = true
private set private set
var drawBookAuthor = true var drawBookAuthor = true
@@ -200,9 +203,12 @@ object BookCover {
} }
fun getCoverRule(): CoverRule { fun getCoverRule(): CoverRule {
return getConfig() ?: DefaultData.coverRule
}
fun getConfig(): CoverRule? {
return GSON.fromJsonObject<CoverRule>(CacheManager.get(coverRuleConfigKey)) return GSON.fromJsonObject<CoverRule>(CacheManager.get(coverRuleConfigKey))
.getOrNull() .getOrNull()
?: DefaultData.coverRule
} }
suspend fun searchCover(book: Book): String? { suspend fun searchCover(book: Book): String? {
@@ -214,12 +220,12 @@ object BookCover {
config.searchUrl, config.searchUrl,
book.name, book.name,
source = config, source = config,
coroutineContext = coroutineContext, coroutineContext = currentCoroutineContext(),
hasLoginHeader = false hasLoginHeader = false
) )
val res = analyzeUrl.getStrResponseAwait() val res = analyzeUrl.getStrResponseAwait()
val analyzeRule = AnalyzeRule(book) val analyzeRule = AnalyzeRule(book)
analyzeRule.setCoroutineContext(coroutineContext) analyzeRule.setCoroutineContext(currentCoroutineContext())
analyzeRule.setContent(res.body) analyzeRule.setContent(res.body)
analyzeRule.setRedirectUrl(res.url) analyzeRule.setRedirectUrl(res.url)
return analyzeRule.getString(config.coverRule, isUrl = true) return analyzeRule.getString(config.coverRule, isUrl = true)
@@ -227,6 +233,10 @@ object BookCover {
fun saveCoverRule(config: CoverRule) { fun saveCoverRule(config: CoverRule) {
val json = GSON.toJson(config) val json = GSON.toJson(config)
saveCoverRule(json)
}
fun saveCoverRule(json: String) {
CacheManager.put(coverRuleConfigKey, json) CacheManager.put(coverRuleConfigKey, json)
} }