优化
This commit is contained in:
@@ -40,6 +40,13 @@ object AppWebDav {
|
|||||||
return url
|
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)
|
@Throws(Exception::class)
|
||||||
suspend fun initWebDav(): Boolean {
|
suspend fun initWebDav(): Boolean {
|
||||||
val account = appCtx.getPrefString(PreferKey.webDavAccount)
|
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) {
|
suspend fun backUpWebDav(path: String) {
|
||||||
try {
|
try {
|
||||||
if (initWebDav() && NetworkUtils.isAvailable()) {
|
if (initWebDav() && NetworkUtils.isAvailable()) {
|
||||||
@@ -114,9 +126,7 @@ object AppWebDav {
|
|||||||
}
|
}
|
||||||
FileUtils.deleteFile(zipFilePath)
|
FileUtils.deleteFile(zipFilePath)
|
||||||
if (ZipUtils.zipFiles(paths, zipFilePath)) {
|
if (ZipUtils.zipFiles(paths, zipFilePath)) {
|
||||||
val backupDate = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
|
val putUrl = "${rootWebDavUrl}${backupFileName}"
|
||||||
.format(Date(System.currentTimeMillis()))
|
|
||||||
val putUrl = "${rootWebDavUrl}backup${backupDate}.zip"
|
|
||||||
WebDav(putUrl).upload(zipFilePath)
|
WebDav(putUrl).upload(zipFilePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,9 @@ object Backup : BackupRestore() {
|
|||||||
val lastBackup = context.getPrefLong(PreferKey.lastBackup)
|
val lastBackup = context.getPrefLong(PreferKey.lastBackup)
|
||||||
if (lastBackup + TimeUnit.DAYS.toMillis(1) < System.currentTimeMillis()) {
|
if (lastBackup + TimeUnit.DAYS.toMillis(1) < System.currentTimeMillis()) {
|
||||||
Coroutine.async {
|
Coroutine.async {
|
||||||
backup(context, context.getPrefString(PreferKey.backupPath) ?: "", true)
|
if (!AppWebDav.hasBackUp()) {
|
||||||
|
backup(context, context.getPrefString(PreferKey.backupPath) ?: "", true)
|
||||||
|
}
|
||||||
}.onError {
|
}.onError {
|
||||||
AppLog.put("备份出错\n${it.localizedMessage}", it)
|
AppLog.put("备份出错\n${it.localizedMessage}", it)
|
||||||
appCtx.toastOnUi(appCtx.getString(R.string.autobackup_fail, it.localizedMessage))
|
appCtx.toastOnUi(appCtx.getString(R.string.autobackup_fail, it.localizedMessage))
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ class WebDav(urlStr: String) {
|
|||||||
val path get() = url.toString()
|
val path get() = url.toString()
|
||||||
var displayName: String? = null
|
var displayName: String? = null
|
||||||
var size: Long = 0
|
var size: Long = 0
|
||||||
var exists = false
|
|
||||||
var parent = ""
|
var parent = ""
|
||||||
var urlName = ""
|
var urlName = ""
|
||||||
var contentType = ""
|
var contentType = ""
|
||||||
@@ -141,6 +140,13 @@ class WebDav(urlStr: String) {
|
|||||||
return list
|
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,在远程处创建对应的文件夹
|
* 根据自己的URL,在远程处创建对应的文件夹
|
||||||
* @return 是否创建成功
|
* @return 是否创建成功
|
||||||
|
|||||||
Reference in New Issue
Block a user