From 30d1c90ada568723131f94e7186faa0aaaec46af Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 17 Sep 2019 10:52:35 +0800 Subject: [PATCH] up --- .../main/java/io/legado/app/help/FileHelp.kt | 17 ++++++++++++++ .../java/io/legado/app/help/storage/Backup.kt | 2 ++ .../io/legado/app/help/storage/WebDavHelp.kt | 22 +++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/FileHelp.kt b/app/src/main/java/io/legado/app/help/FileHelp.kt index cb0228445..a208a74a2 100644 --- a/app/src/main/java/io/legado/app/help/FileHelp.kt +++ b/app/src/main/java/io/legado/app/help/FileHelp.kt @@ -38,4 +38,21 @@ object FileHelp { return App.INSTANCE.externalCacheDir?.absolutePath ?: App.INSTANCE.cacheDir.absolutePath } + + //递归删除文件夹下的数据 + @Synchronized + fun deleteFile(filePath: String) { + val file = File(filePath) + if (!file.exists()) return + + if (file.isDirectory) { + val files = file.listFiles() + for (subFile in files) { + val path = subFile.path + deleteFile(path) + } + } + //删除文件 + file.delete() + } } \ No newline at end of file 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 cd7c69edc..683cf34c1 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 @@ -24,6 +24,7 @@ object Backup { backupBookSource(path) backupRssSource(path) backupReplaceRule(path) + WebDavHelp.backUpWebDav() uiThread { App.INSTANCE.toast(R.string.backup_success) } @@ -37,6 +38,7 @@ object Backup { backupBookSource(path) backupRssSource(path) backupReplaceRule(path) + WebDavHelp.backUpWebDav() } } 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 fb5b9045a..c19c3b46f 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,9 +10,13 @@ import io.legado.app.utils.getPrefString import org.jetbrains.anko.doAsync import org.jetbrains.anko.selector import org.jetbrains.anko.uiThread +import java.io.File +import java.text.SimpleDateFormat +import java.util.* import kotlin.math.min object WebDavHelp { + private val zipFilePath = FileHelp.getCachePath() + "/backup" + ".zip" fun getWebDavUrl(): String? { var url = App.INSTANCE.getPrefString("web_dav_url") @@ -67,7 +71,6 @@ object WebDavHelp { doAsync { getWebDavUrl()?.let { val file = WebDav(it + "legado/" + name) - val zipFilePath = FileHelp.getCachePath() + "/backup" + ".zip" file.downloadTo(zipFilePath, true) ZipUtils.unzipFile(zipFilePath, Backup.defaultPath) Restore.restore() @@ -76,6 +79,21 @@ object WebDavHelp { } fun backUpWebDav() { - + if (initWebDav()) { + val paths = arrayListOf() + paths.add(Backup.defaultPath + File.separator + "bookshelf.json") + paths.add(Backup.defaultPath + File.separator + "bookSource.json") + paths.add(Backup.defaultPath + File.separator + "rssSource.json") + paths.add(Backup.defaultPath + File.separator + "replaceRule.json") + FileHelp.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" + WebDav(putUrl).upload(zipFilePath) + } + } } } \ No newline at end of file