From d14f6e9bb3c48c7cd382d0b7b95cebbfadb4c13f Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 1 May 2022 21:38:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/storage/AppWebDav.kt | 31 +++++----- .../java/io/legado/app/help/storage/Backup.kt | 3 +- .../java/io/legado/app/lib/webdav/WebDav.kt | 56 ++++++++++++------- .../io/legado/app/lib/webdav/WebDavFile.kt | 16 ++++++ 4 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 app/src/main/java/io/legado/app/lib/webdav/WebDavFile.kt 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 cb17b2ead..813671bec 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 @@ -14,6 +14,7 @@ import io.legado.app.lib.dialogs.selector import io.legado.app.lib.webdav.Authorization import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.WebDavException +import io.legado.app.lib.webdav.WebDavFile import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -80,19 +81,14 @@ object AppWebDav { } @Throws(Exception::class) - private suspend fun getWebDavFileNames(relativePath: String? = null): ArrayList { - val url = if (relativePath == null) { - rootWebDavUrl - } else { - NetworkUtils.getAbsoluteURL(rootWebDavUrl, relativePath) - } + private suspend fun getBackupNames(): ArrayList { val names = arrayListOf() authorization?.let { - var files = WebDav(url, it).listFiles() + var files = WebDav(rootWebDavUrl, it).listFiles() files = files.reversed() files.forEach { webDav -> val name = webDav.displayName - if (name?.startsWith("backup") == true) { + if (name.startsWith("backup")) { names.add(name) } } @@ -101,7 +97,7 @@ object AppWebDav { } suspend fun showRestoreDialog(context: Context) { - val names = withContext(IO) { getWebDavFileNames() } + val names = withContext(IO) { getBackupNames() } if (names.isNotEmpty()) { withContext(Main) { context.selector( @@ -134,12 +130,21 @@ object AppWebDav { } } - suspend fun hasBackUp(): Boolean { + suspend fun lastBackUp(): WebDavFile? { authorization?.let { - val url = "${rootWebDavUrl}${backupFileName}" - return WebDav(url, it).exists() + var lastBackupFile: WebDavFile? = null + WebDav(rootWebDavUrl, it).listFiles().forEach { webDavFile -> + if (webDavFile.displayName.startsWith("backup")) { + if (lastBackupFile == null + || webDavFile.lastModify > lastBackupFile!!.lastModify + ) { + lastBackupFile = webDavFile + } + } + } + return lastBackupFile } - return false + return null } @Throws(Exception::class) 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 6cfe2a562..02d66d420 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,8 @@ object Backup { val lastBackup = LocalConfig.lastBackup if (lastBackup + TimeUnit.DAYS.toMillis(1) < System.currentTimeMillis()) { Coroutine.async { - if (!AppWebDav.hasBackUp()) { + val lastBackupWebDav = AppWebDav.lastBackUp() + if (lastBackupWebDav == null || lastBackup > lastBackupWebDav.lastModify.time) { backup(context, context.getPrefString(PreferKey.backupPath), true) } else { LocalConfig.lastBackup = System.currentTimeMillis() 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 c21ddab47..1070af216 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 @@ -1,5 +1,6 @@ package io.legado.app.lib.webdav +import android.annotation.SuppressLint import io.legado.app.constant.AppLog import io.legado.app.exception.NoStackTraceException import io.legado.app.help.http.newCallResponse @@ -18,10 +19,16 @@ import java.io.InputStream import java.net.MalformedURLException import java.net.URL import java.net.URLEncoder +import java.text.SimpleDateFormat +import java.util.* @Suppress("unused", "MemberVisibilityCanBePrivate") -class WebDav(urlStr: String, val authorization: Authorization) { +open class WebDav(urlStr: String, val authorization: Authorization) { companion object { + + @SuppressLint("SimpleDateFormat") + private val dateFormat = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZ") + // 指定返回哪些属性 @Language("xml") private const val DIR = @@ -50,11 +57,7 @@ class WebDav(urlStr: String, val authorization: Authorization) { } val host: String? get() = url.host val path get() = url.toString() - var displayName: String? = null - var size: Long = 0 - var parent = "" - var urlName = "" - var contentType = "" + /** * 填充文件信息。实例化WebDAVFile对象时,并没有将远程文件的信息填充到实例中。需要手动填充! @@ -70,7 +73,7 @@ class WebDav(urlStr: String, val authorization: Authorization) { * @return 文件列表 */ @Throws(WebDavException::class) - suspend fun listFiles(): List { + suspend fun listFiles(): List { propFindResponse()?.let { body -> return parseDir(body) } @@ -108,8 +111,8 @@ class WebDav(urlStr: String, val authorization: Authorization) { }.body?.text() } - private fun parseDir(s: String): List { - val list = ArrayList() + private fun parseDir(s: String): List { + val list = ArrayList() val document = Jsoup.parse(s) val elements = document.getElementsByTag("d:response") httpUrl?.let { urlStr -> @@ -120,18 +123,31 @@ class WebDav(urlStr: String, val authorization: Authorization) { val fileName = href.substring(href.lastIndexOf("/") + 1) val webDavFile: WebDav try { - webDavFile = WebDav(baseUrl + fileName, authorization) - webDavFile.displayName = fileName - webDavFile.contentType = element - .getElementsByTag("d:getcontenttype") - .getOrNull(0)?.text() ?: "" - if (href.isEmpty()) { - webDavFile.urlName = - if (parent.isEmpty()) url.file.replace("/", "") - else url.toString().replace(parent, "").replace("/", "") - } else { - webDavFile.urlName = href + val urlName = href.ifEmpty { + url.file.replace("/", "") } + val contentType = element + .getElementsByTag("d:getcontenttype") + .firstOrNull()?.text().orEmpty() + val size = kotlin.runCatching { + element.getElementsByTag("d:getcontentlength") + .firstOrNull()?.text()?.toLong() ?: 0 + }.getOrDefault(0) + val lastModify: Date = kotlin.runCatching { + element.getElementsByTag("d:getcontentlength") + .firstOrNull()?.text()?.let { + dateFormat.parse(it) + } + }.getOrNull() ?: Date() + webDavFile = WebDavFile( + baseUrl + fileName, + authorization, + displayName = fileName, + urlName = urlName, + size = size, + contentType = contentType, + lastModify = lastModify + ) list.add(webDavFile) } catch (e: MalformedURLException) { e.printOnDebug() diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDavFile.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDavFile.kt new file mode 100644 index 000000000..4519e4055 --- /dev/null +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDavFile.kt @@ -0,0 +1,16 @@ +package io.legado.app.lib.webdav + +import java.util.* + +/** + * webDavFile + */ +class WebDavFile( + urlStr: String, + authorization: Authorization, + val displayName: String, + val urlName: String, + val size: Long, + val contentType: String, + val lastModify: Date +) : WebDav(urlStr, authorization) \ No newline at end of file