优化
This commit is contained in:
@@ -99,8 +99,7 @@ object AppWebDav {
|
||||
webDav.downloadTo(Backup.zipFilePath, true)
|
||||
FileUtils.delete(Backup.backupPath)
|
||||
ZipUtils.unZipToPath(File(Backup.zipFilePath), Backup.backupPath)
|
||||
Restore.restoreDatabase()
|
||||
Restore.restoreConfig()
|
||||
Restore.restore(Backup.backupPath)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -92,6 +92,7 @@ object Backup {
|
||||
suspend fun backup(context: Context, path: String?) {
|
||||
LocalConfig.lastBackup = System.currentTimeMillis()
|
||||
withContext(IO) {
|
||||
val aes = BackupAES()
|
||||
FileUtils.delete(backupPath)
|
||||
writeListToJson(appDb.bookDao.all, "bookshelf.json", backupPath)
|
||||
writeListToJson(appDb.bookmarkDao.all, "bookmark.json", backupPath)
|
||||
@@ -108,7 +109,10 @@ object Backup {
|
||||
writeListToJson(appDb.httpTTSDao.all, "httpTTS.json", backupPath)
|
||||
writeListToJson(appDb.keyboardAssistsDao.all, "keyboardAssists.json", backupPath)
|
||||
writeListToJson(appDb.dictRuleDao.all, "dictRule.json", backupPath)
|
||||
writeListToJson(appDb.serverDao.all, "servers.json", backupPath)
|
||||
aes.encryptBase64(GSON.toJson(appDb.serverDao.all)).let {
|
||||
FileUtils.createFileIfNotExist(backupPath + File.separator + "servers.json")
|
||||
.writeText(it)
|
||||
}
|
||||
ensureActive()
|
||||
GSON.toJson(ReadBookConfig.configList).let {
|
||||
FileUtils.createFileIfNotExist(backupPath + File.separator + ReadBookConfig.configFileName)
|
||||
@@ -129,7 +133,6 @@ object Backup {
|
||||
ensureActive()
|
||||
appCtx.getSharedPreferences(backupPath, "config")?.let { sp ->
|
||||
val edit = sp.edit()
|
||||
val aes = BackupAES()
|
||||
appCtx.defaultSharedPreferences.all.forEach { (key, value) ->
|
||||
if (BackupConfig.keyIsNotIgnore(key)) {
|
||||
when (key) {
|
||||
@@ -159,15 +162,15 @@ object Backup {
|
||||
if (ZipUtils.zipFiles(paths, zipFilePath)) {
|
||||
when {
|
||||
path.isNullOrBlank() -> {
|
||||
copyBackup(context.getExternalFilesDir(null)!!, "backup.zip")
|
||||
copyBackup(context.getExternalFilesDir(null)!!, zipFileName)
|
||||
}
|
||||
|
||||
path.isContentScheme() -> {
|
||||
copyBackup(context, Uri.parse(path), "backup.zip")
|
||||
copyBackup(context, Uri.parse(path), zipFileName)
|
||||
}
|
||||
|
||||
else -> {
|
||||
copyBackup(File(path), "backup.zip")
|
||||
copyBackup(File(path), zipFileName)
|
||||
}
|
||||
}
|
||||
AppWebDav.backUpWebDav(zipFileName)
|
||||
|
||||
@@ -5,5 +5,5 @@ import io.legado.app.help.config.LocalConfig
|
||||
import io.legado.app.utils.MD5Utils
|
||||
|
||||
class BackupAES : AES(
|
||||
MD5Utils.md5Encode(LocalConfig.password).encodeToByteArray(0, 16)
|
||||
MD5Utils.md5Encode(LocalConfig.password ?: "").encodeToByteArray(0, 16)
|
||||
)
|
||||
@@ -71,15 +71,15 @@ object Restore {
|
||||
ZipUtils.unZipToPath(File(uri.path!!), Backup.backupPath)
|
||||
}
|
||||
}.onSuccess {
|
||||
restoreDatabase()
|
||||
restoreConfig()
|
||||
restore(Backup.backupPath)
|
||||
LocalConfig.lastBackup = System.currentTimeMillis()
|
||||
}.onFailure {
|
||||
AppLog.put("复制解压文件出错\n${it.localizedMessage}", it)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun restoreDatabase(path: String = Backup.backupPath) {
|
||||
suspend fun restore(path: String) {
|
||||
val aes = BackupAES()
|
||||
withContext(IO) {
|
||||
fileToListT<Book>(path, "bookshelf.json")?.let {
|
||||
it.forEach { book ->
|
||||
@@ -140,9 +140,6 @@ object Restore {
|
||||
fileToListT<DictRule>(path, "dictRule.json")?.let {
|
||||
appDb.dictRuleDao.insert(*it.toTypedArray())
|
||||
}
|
||||
fileToListT<Server>(path, "servers.json")?.let {
|
||||
appDb.serverDao.insert(*it.toTypedArray())
|
||||
}
|
||||
fileToListT<KeyboardAssist>(path, "keyboardAssists.json")?.let {
|
||||
appDb.keyboardAssistsDao.insert(*it.toTypedArray())
|
||||
}
|
||||
@@ -160,11 +157,14 @@ object Restore {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun restoreConfig(path: String = Backup.backupPath) {
|
||||
withContext(IO) {
|
||||
File(path, "servers.json").takeIf {
|
||||
it.exists()
|
||||
}?.run {
|
||||
val json = aes.decryptStr(readText())
|
||||
GSON.fromJsonArray<Server>(json).getOrNull()?.let {
|
||||
appDb.serverDao.insert(*it.toTypedArray())
|
||||
}
|
||||
}
|
||||
try {
|
||||
val file = File(path, DirectLinkUpload.ruleFileName)
|
||||
if (file.exists()) {
|
||||
@@ -209,7 +209,7 @@ object Restore {
|
||||
}
|
||||
appCtx.getSharedPreferences(path, "config")?.all?.let { map ->
|
||||
val edit = appCtx.defaultSharedPreferences.edit()
|
||||
val aes = BackupAES()
|
||||
|
||||
map.forEach { (key, value) ->
|
||||
if (BackupConfig.keyIsNotIgnore(key)) {
|
||||
when (key) {
|
||||
|
||||
Reference in New Issue
Block a user