This commit is contained in:
kunfei
2022-02-25 15:17:04 +08:00
parent c094fb991c
commit dff79f55e8
3 changed files with 23 additions and 5 deletions

View File

@@ -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)
}
}

View File

@@ -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))

View File

@@ -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 是否创建成功