From 822aaf2c7cd84c4e9d4c4d1fd956591d86cf47b3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 14 May 2022 23:26:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/storage/Restore.kt | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/Restore.kt b/app/src/main/java/io/legado/app/help/storage/Restore.kt index 10f874cd6..edf0b4eba 100644 --- a/app/src/main/java/io/legado/app/help/storage/Restore.kt +++ b/app/src/main/java/io/legado/app/help/storage/Restore.kt @@ -22,40 +22,36 @@ import kotlinx.coroutines.withContext import splitties.init.appCtx import java.io.File import java.io.FileInputStream +import java.io.FileOutputStream object Restore { suspend fun restore(context: Context, path: String) { - withContext(IO) { + kotlin.runCatching { if (path.isContentScheme()) { DocumentFile.fromTreeUri(context, Uri.parse(path))?.listFiles()?.forEach { doc -> - for (fileName in Backup.backupFileNames) { - if (doc.name == fileName) { - DocumentUtils.readText(context, doc.uri).let { - FileUtils.createFileIfNotExist("${Backup.backupPath}${File.separator}$fileName") - .writeText(it) + if (Backup.backupFileNames.contains(doc.name)) { + context.contentResolver.openInputStream(doc.uri)?.use { inputStream -> + val file = File("${Backup.backupPath}${File.separator}${doc.name}") + FileOutputStream(file).use { outputStream -> + inputStream.copyTo(outputStream) } } } } } else { - try { - val file = File(path) - for (fileName in Backup.backupFileNames) { - file.getFile(fileName).let { - if (it.exists()) { - it.copyTo( - FileUtils.createFileIfNotExist("${Backup.backupPath}${File.separator}$fileName"), - true - ) - } - } + val dir = File(path) + for (fileName in Backup.backupFileNames) { + val file = dir.getFile(fileName) + if (file.exists()) { + val target = File("${Backup.backupPath}${File.separator}$fileName") + file.copyTo(target, true) } - } catch (e: Exception) { - e.printOnDebug() } } + }.onFailure { + AppLog.put("", it) } restoreDatabase() restoreConfig() @@ -129,7 +125,7 @@ object Restore { ThemeConfig.upConfig() } } catch (e: Exception) { - e.printOnDebug() + AppLog.put("恢复主题出错\n${e.localizedMessage}", e) } if (!BackupConfig.ignoreReadConfig) { //恢复阅读界面配置 @@ -142,7 +138,7 @@ object Restore { ReadBookConfig.initConfigs() } } catch (e: Exception) { - e.printOnDebug() + AppLog.put("恢复阅读界面出错\n${e.localizedMessage}", e) } try { val file = @@ -153,7 +149,7 @@ object Restore { ReadBookConfig.initShareConfig() } } catch (e: Exception) { - e.printOnDebug() + AppLog.put("恢复阅读界面出错\n${e.localizedMessage}", e) } } Preferences.getSharedPreferences(appCtx, path, "config")?.all?.let { map ->