diff --git a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt index dc6f66aef..082e85434 100644 --- a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt +++ b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt @@ -40,6 +40,13 @@ object AppWebDav { return url } + private val backupFileName: String + get() { + val backupDate = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) + .format(Date(System.currentTimeMillis())) + return "backup${backupDate}.zip" + } + @Throws(Exception::class) suspend fun initWebDav(): Boolean { val account = appCtx.getPrefString(PreferKey.webDavAccount) @@ -105,6 +112,11 @@ object AppWebDav { } } + suspend fun hasBackUp(): Boolean { + val url = "${rootWebDavUrl}${backupFileName}" + return WebDav(url).exists() + } + suspend fun backUpWebDav(path: String) { try { if (initWebDav() && NetworkUtils.isAvailable()) { @@ -114,9 +126,7 @@ object AppWebDav { } FileUtils.deleteFile(zipFilePath) if (ZipUtils.zipFiles(paths, zipFilePath)) { - val backupDate = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) - .format(Date(System.currentTimeMillis())) - val putUrl = "${rootWebDavUrl}backup${backupDate}.zip" + val putUrl = "${rootWebDavUrl}${backupFileName}" WebDav(putUrl).upload(zipFilePath) } } diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index 5ff67a0fa..2979a0c31 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -51,7 +51,9 @@ object Backup : BackupRestore() { val lastBackup = context.getPrefLong(PreferKey.lastBackup) if (lastBackup + TimeUnit.DAYS.toMillis(1) < System.currentTimeMillis()) { Coroutine.async { - backup(context, context.getPrefString(PreferKey.backupPath) ?: "", true) + if (!AppWebDav.hasBackUp()) { + backup(context, context.getPrefString(PreferKey.backupPath) ?: "", true) + } }.onError { AppLog.put("备份出错\n${it.localizedMessage}", it) appCtx.toastOnUi(appCtx.getString(R.string.autobackup_fail, it.localizedMessage)) diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index 3f4b2ab8c..55f2090f6 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -49,7 +49,6 @@ class WebDav(urlStr: String) { val path get() = url.toString() var displayName: String? = null var size: Long = 0 - var exists = false var parent = "" var urlName = "" var contentType = "" @@ -141,6 +140,13 @@ class WebDav(urlStr: String) { return list } + suspend fun exists(): Boolean { + val response = propFindResponse() ?: return false + val document = Jsoup.parse(response) + val elements = document.getElementsByTag("d:response") + return elements.isNotEmpty() + } + /** * 根据自己的URL,在远程处创建对应的文件夹 * @return 是否创建成功