WebDav恢复出错时报错

pull/348/head
gedoor 4 years ago
parent aac445c4d6
commit 64e55854f7
  1. 22
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt
  2. 29
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt

@ -51,28 +51,25 @@ object WebDavHelp {
return false return false
} }
@Throws(Exception::class)
private fun getWebDavFileNames(): ArrayList<String> { private fun getWebDavFileNames(): ArrayList<String> {
val url = rootWebDavUrl val url = rootWebDavUrl
val names = arrayListOf<String>() val names = arrayListOf<String>()
if (initWebDav()) { if (initWebDav()) {
try { var files = WebDav(url).listFiles()
var files = WebDav(url).listFiles() files = files.reversed()
files = files.reversed() for (index: Int in 0 until min(10, files.size)) {
for (index: Int in 0 until min(10, files.size)) { files[index].displayName?.let {
files[index].displayName?.let { names.add(it)
names.add(it)
}
} }
} catch (e: Exception) {
return names
} }
} }
return names return names
} }
suspend fun showRestoreDialog(context: Context): Boolean { suspend fun showRestoreDialog(context: Context) {
val names = withContext(IO) { getWebDavFileNames() } val names = withContext(IO) { getWebDavFileNames() }
return if (names.isNotEmpty()) { if (names.isNotEmpty()) {
withContext(Main) { withContext(Main) {
context.selector( context.selector(
title = context.getString(R.string.select_restore_file), title = context.getString(R.string.select_restore_file),
@ -83,9 +80,8 @@ object WebDavHelp {
} }
} }
} }
true
} else { } else {
false throw Exception("Web dav no back up file")
} }
} }

@ -19,6 +19,7 @@ import io.legado.app.help.storage.WebDavHelp
import io.legado.app.ui.filechooser.FilePicker import io.legado.app.ui.filechooser.FilePicker
import io.legado.app.utils.getPrefString import io.legado.app.utils.getPrefString
import io.legado.app.utils.isContentPath import io.legado.app.utils.isContentPath
import io.legado.app.utils.longToast
import io.legado.app.utils.toast import io.legado.app.utils.toast
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
import org.jetbrains.anko.toast import org.jetbrains.anko.toast
@ -77,24 +78,24 @@ object BackupRestoreUi {
fun restore(fragment: Fragment) { fun restore(fragment: Fragment) {
Coroutine.async(context = Main) { Coroutine.async(context = Main) {
val restoreFromWebDav = WebDavHelp.showRestoreDialog(fragment.requireContext()) WebDavHelp.showRestoreDialog(fragment.requireContext())
if (!restoreFromWebDav) { }.onError {
val backupPath = fragment.getPrefString(PreferKey.backupPath) fragment.longToast("WebDavError:${it.localizedMessage},将从本地备份恢复。")
if (backupPath?.isNotEmpty() == true) { val backupPath = fragment.getPrefString(PreferKey.backupPath)
if (backupPath.isContentPath()) { if (backupPath?.isNotEmpty() == true) {
val uri = Uri.parse(backupPath) if (backupPath.isContentPath()) {
val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri) val uri = Uri.parse(backupPath)
if (doc?.canWrite() == true) { val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri)
Restore.restore(fragment.requireContext(), backupPath) if (doc?.canWrite() == true) {
} else { Restore.restore(fragment.requireContext(), backupPath)
selectBackupFolder(fragment, restoreSelectRequestCode)
}
} else { } else {
restoreUsePermission(fragment, backupPath) selectBackupFolder(fragment, restoreSelectRequestCode)
} }
} else { } else {
selectBackupFolder(fragment, restoreSelectRequestCode) restoreUsePermission(fragment, backupPath)
} }
} else {
selectBackupFolder(fragment, restoreSelectRequestCode)
} }
} }
} }

Loading…
Cancel
Save