pull/34/head
kunfei 5 years ago
parent 8316e13987
commit 30d1c90ada
  1. 17
      app/src/main/java/io/legado/app/help/FileHelp.kt
  2. 2
      app/src/main/java/io/legado/app/help/storage/Backup.kt
  3. 22
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.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()
}
}

@ -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()
}
}

@ -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<String>()
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)
}
}
}
}
Loading…
Cancel
Save