diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 72d8c6b15..b427f687b 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -32,6 +32,7 @@ object PreferKey { const val webDavUrl = "web_dav_url" const val webDavAccount = "web_dav_account" const val webDavPassword = "web_dav_password" + const val webDavCreateDir = "webDavCreateDir" const val changeSourceLoadToc = "changeSourceLoadToc" const val chineseConverterType = "chineseConverterType" const val launcherIcon = "launcherIcon" diff --git a/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt b/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt index 7966205fe..8f2bbaf47 100644 --- a/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt +++ b/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt @@ -11,7 +11,7 @@ import io.legado.app.utils.fromJsonArray @Suppress("BlockingMethodInNonBlockingContext") object SyncBookProgress { private val file = FileUtils.createFileIfNotExist(App.INSTANCE.cacheDir, "bookProgress.json") - private val webDavUrl = WebDavHelp.getWebDavUrl() + "legado/bookProgress.json" + private val webDavUrl = "${WebDavHelp.rootWebDavUrl}bookProgress.json" fun uploadBookProgress() { Coroutine.async { @@ -20,7 +20,6 @@ object SyncBookProgress { val json = GSON.toJson(value) file.writeText(json) if (WebDavHelp.initWebDav()) { - WebDav(WebDavHelp.getWebDavUrl() + "legado").makeAsDir() WebDav(webDavUrl).upload(file.absolutePath) } } diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index 0b4b97aab..fc14289c5 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -10,6 +10,7 @@ import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.http.HttpAuth import io.legado.app.utils.FileUtils import io.legado.app.utils.ZipUtils +import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.getPrefString import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -25,12 +26,16 @@ object WebDavHelp { private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" private val zipFilePath = "${FileUtils.getCachePath()}${File.separator}backup.zip" - fun getWebDavUrl(): String { + val rootWebDavUrl: String + get() { var url = App.INSTANCE.getPrefString(PreferKey.webDavUrl) if (url.isNullOrEmpty()) { url = defaultWebDavUrl } - if (!url.endsWith("/")) url += "/" + if (!url.endsWith("/")) url = "${url}/" + if (App.INSTANCE.getPrefBoolean(PreferKey.webDavCreateDir, true)) { + url = "${url}legado/" + } return url } @@ -39,17 +44,18 @@ object WebDavHelp { val password = App.INSTANCE.getPrefString(PreferKey.webDavPassword) if (!account.isNullOrBlank() && !password.isNullOrBlank()) { HttpAuth.auth = HttpAuth.Auth(account, password) + WebDav(rootWebDavUrl).makeAsDir() return true } return false } private fun getWebDavFileNames(): ArrayList { - val url = getWebDavUrl() + val url = rootWebDavUrl val names = arrayListOf() if (initWebDav()) { try { - var files = WebDav(url + "legado/").listFiles() + var files = WebDav(url).listFiles() files = files.reversed() for (index: Int in 0 until min(10, files.size)) { files[index].displayName?.let { @@ -81,8 +87,8 @@ object WebDavHelp { private fun restoreWebDav(name: String, success: () -> Unit) { Coroutine.async { - getWebDavUrl().let { - val webDav = WebDav(it + "legado/" + name) + rootWebDavUrl.let { + val webDav = WebDav(it + name) webDav.downloadTo(zipFilePath, true) @Suppress("BlockingMethodInNonBlockingContext") ZipUtils.unzipFile(zipFilePath, Backup.backupPath) @@ -102,10 +108,9 @@ object WebDavHelp { } FileUtils.deleteFile(zipFilePath) if (ZipUtils.zipFiles(paths, zipFilePath)) { - WebDav(getWebDavUrl() + "legado").makeAsDir() - val putUrl = getWebDavUrl() + "legado/backup" + - SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) - .format(Date(System.currentTimeMillis())) + ".zip" + val backupDate = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) + .format(Date(System.currentTimeMillis())) + val putUrl = "${rootWebDavUrl}backup${backupDate}.zip" WebDav(putUrl).upload(zipFilePath) } } diff --git a/app/src/main/res/xml/pref_config_backup.xml b/app/src/main/res/xml/pref_config_backup.xml index 3f99cfef2..b2442aa09 100644 --- a/app/src/main/res/xml/pref_config_backup.xml +++ b/app/src/main/res/xml/pref_config_backup.xml @@ -24,6 +24,13 @@ android:summary="@string/web_dav_pw_s" app:iconSpaceReserved="false" /> + +