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