diff --git a/app/src/main/java/com/kunfei/bookshelf/help/storage/WebDavHelp.kt b/app/src/main/java/com/kunfei/bookshelf/help/storage/WebDavHelp.kt index 7cfe32314c..7a21edafbd 100644 --- a/app/src/main/java/com/kunfei/bookshelf/help/storage/WebDavHelp.kt +++ b/app/src/main/java/com/kunfei/bookshelf/help/storage/WebDavHelp.kt @@ -1,6 +1,8 @@ package com.kunfei.bookshelf.help.storage import android.content.Context +import android.os.Handler +import android.os.Looper import com.kunfei.bookshelf.MApplication import com.kunfei.bookshelf.base.observer.MySingleObserver import com.kunfei.bookshelf.constant.AppConstant @@ -13,6 +15,7 @@ import io.reactivex.SingleOnSubscribe import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers import org.jetbrains.anko.selector +import org.jetbrains.anko.toast import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -24,9 +27,11 @@ object WebDavHelp { FileHelp.getCachePath() } - private fun getWebDavUrl(): String? { + private fun getWebDavUrl(): String { var url = MApplication.getConfigPreferences().getString("web_dav_url", AppConstant.DEFAULT_WEB_DAV_URL) - if (url.isNullOrBlank()) return null + if (url.isNullOrEmpty()) { + url = AppConstant.DEFAULT_WEB_DAV_URL + } if (!url.endsWith("/")) url += "/" return url } @@ -45,7 +50,7 @@ object WebDavHelp { val url = getWebDavUrl() val names = arrayListOf() try { - if (!url.isNullOrBlank() && initWebDav()) { + if (initWebDav()) { var files = WebDav(url + "YueDu/").listFiles() files = files.reversed() for (index: Int in 0 until min(10, files.size)) { @@ -75,7 +80,7 @@ object WebDavHelp { private fun restoreWebDav(name: String, callBack: Restore.CallBack?) { Single.create(SingleOnSubscribe { e -> - getWebDavUrl()?.let { + getWebDavUrl().let { val file = WebDav(it + "YueDu/" + name) file.downloadTo(zipFilePath, true) @Suppress("BlockingMethodInNonBlockingContext") @@ -92,18 +97,24 @@ object WebDavHelp { } fun backUpWebDav(path: String) { - if (initWebDav()) { - val paths = arrayListOf(*Backup.backupFileNames) - for (i in 0 until paths.size) { - paths[i] = path + File.separator + paths[i] + try { + if (initWebDav()) { + val paths = arrayListOf(*Backup.backupFileNames) + for (i in 0 until paths.size) { + paths[i] = path + File.separator + paths[i] + } + FileHelp.deleteFile(zipFilePath) + if (ZipUtils.zipFiles(paths, zipFilePath)) { + WebDav(getWebDavUrl() + "YueDu").makeAsDir() + val putUrl = getWebDavUrl() + "YueDu/backup" + + SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) + .format(Date(System.currentTimeMillis())) + ".zip" + WebDav(putUrl).upload(zipFilePath) + } } - FileHelp.deleteFile(zipFilePath) - if (ZipUtils.zipFiles(paths, zipFilePath)) { - WebDav(getWebDavUrl() + "YueDu").makeAsDir() - val putUrl = getWebDavUrl() + "YueDu/backup" + - SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) - .format(Date(System.currentTimeMillis())) + ".zip" - WebDav(putUrl).upload(zipFilePath) + } catch (e: Exception) { + Handler(Looper.getMainLooper()).post { + MApplication.getInstance().toast("WebDav\n${e.localizedMessage}") } } }