|
|
@ -10,6 +10,7 @@ import io.legado.app.lib.webdav.WebDav |
|
|
|
import io.legado.app.lib.webdav.http.HttpAuth |
|
|
|
import io.legado.app.lib.webdav.http.HttpAuth |
|
|
|
import io.legado.app.utils.FileUtils |
|
|
|
import io.legado.app.utils.FileUtils |
|
|
|
import io.legado.app.utils.ZipUtils |
|
|
|
import io.legado.app.utils.ZipUtils |
|
|
|
|
|
|
|
import io.legado.app.utils.getPrefBoolean |
|
|
|
import io.legado.app.utils.getPrefString |
|
|
|
import io.legado.app.utils.getPrefString |
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
import kotlinx.coroutines.Dispatchers.IO |
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
|
import kotlinx.coroutines.Dispatchers.Main |
|
|
@ -25,12 +26,16 @@ object WebDavHelp { |
|
|
|
private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" |
|
|
|
private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" |
|
|
|
private val zipFilePath = "${FileUtils.getCachePath()}${File.separator}backup.zip" |
|
|
|
private val zipFilePath = "${FileUtils.getCachePath()}${File.separator}backup.zip" |
|
|
|
|
|
|
|
|
|
|
|
fun getWebDavUrl(): String { |
|
|
|
val rootWebDavUrl: String |
|
|
|
|
|
|
|
get() { |
|
|
|
var url = App.INSTANCE.getPrefString(PreferKey.webDavUrl) |
|
|
|
var url = App.INSTANCE.getPrefString(PreferKey.webDavUrl) |
|
|
|
if (url.isNullOrEmpty()) { |
|
|
|
if (url.isNullOrEmpty()) { |
|
|
|
url = defaultWebDavUrl |
|
|
|
url = defaultWebDavUrl |
|
|
|
} |
|
|
|
} |
|
|
|
if (!url.endsWith("/")) url += "/" |
|
|
|
if (!url.endsWith("/")) url = "${url}/" |
|
|
|
|
|
|
|
if (App.INSTANCE.getPrefBoolean(PreferKey.webDavCreateDir, true)) { |
|
|
|
|
|
|
|
url = "${url}legado/" |
|
|
|
|
|
|
|
} |
|
|
|
return url |
|
|
|
return url |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -39,17 +44,18 @@ object WebDavHelp { |
|
|
|
val password = App.INSTANCE.getPrefString(PreferKey.webDavPassword) |
|
|
|
val password = App.INSTANCE.getPrefString(PreferKey.webDavPassword) |
|
|
|
if (!account.isNullOrBlank() && !password.isNullOrBlank()) { |
|
|
|
if (!account.isNullOrBlank() && !password.isNullOrBlank()) { |
|
|
|
HttpAuth.auth = HttpAuth.Auth(account, password) |
|
|
|
HttpAuth.auth = HttpAuth.Auth(account, password) |
|
|
|
|
|
|
|
WebDav(rootWebDavUrl).makeAsDir() |
|
|
|
return true |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun getWebDavFileNames(): ArrayList<String> { |
|
|
|
private fun getWebDavFileNames(): ArrayList<String> { |
|
|
|
val url = getWebDavUrl() |
|
|
|
val url = rootWebDavUrl |
|
|
|
val names = arrayListOf<String>() |
|
|
|
val names = arrayListOf<String>() |
|
|
|
if (initWebDav()) { |
|
|
|
if (initWebDav()) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
var files = WebDav(url + "legado/").listFiles() |
|
|
|
var files = WebDav(url).listFiles() |
|
|
|
files = files.reversed() |
|
|
|
files = files.reversed() |
|
|
|
for (index: Int in 0 until min(10, files.size)) { |
|
|
|
for (index: Int in 0 until min(10, files.size)) { |
|
|
|
files[index].displayName?.let { |
|
|
|
files[index].displayName?.let { |
|
|
@ -81,8 +87,8 @@ object WebDavHelp { |
|
|
|
|
|
|
|
|
|
|
|
private fun restoreWebDav(name: String, success: () -> Unit) { |
|
|
|
private fun restoreWebDav(name: String, success: () -> Unit) { |
|
|
|
Coroutine.async { |
|
|
|
Coroutine.async { |
|
|
|
getWebDavUrl().let { |
|
|
|
rootWebDavUrl.let { |
|
|
|
val webDav = WebDav(it + "legado/" + name) |
|
|
|
val webDav = WebDav(it + name) |
|
|
|
webDav.downloadTo(zipFilePath, true) |
|
|
|
webDav.downloadTo(zipFilePath, true) |
|
|
|
@Suppress("BlockingMethodInNonBlockingContext") |
|
|
|
@Suppress("BlockingMethodInNonBlockingContext") |
|
|
|
ZipUtils.unzipFile(zipFilePath, Backup.backupPath) |
|
|
|
ZipUtils.unzipFile(zipFilePath, Backup.backupPath) |
|
|
@ -102,10 +108,9 @@ object WebDavHelp { |
|
|
|
} |
|
|
|
} |
|
|
|
FileUtils.deleteFile(zipFilePath) |
|
|
|
FileUtils.deleteFile(zipFilePath) |
|
|
|
if (ZipUtils.zipFiles(paths, zipFilePath)) { |
|
|
|
if (ZipUtils.zipFiles(paths, zipFilePath)) { |
|
|
|
WebDav(getWebDavUrl() + "legado").makeAsDir() |
|
|
|
val backupDate = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) |
|
|
|
val putUrl = getWebDavUrl() + "legado/backup" + |
|
|
|
.format(Date(System.currentTimeMillis())) |
|
|
|
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()) |
|
|
|
val putUrl = "${rootWebDavUrl}backup${backupDate}.zip" |
|
|
|
.format(Date(System.currentTimeMillis())) + ".zip" |
|
|
|
|
|
|
|
WebDav(putUrl).upload(zipFilePath) |
|
|
|
WebDav(putUrl).upload(zipFilePath) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|