feat: 优化代码

pull/133/head
kunfei 5 years ago
parent 5c0e8ddbb5
commit 1b0dc675a9
  1. 1
      app/src/main/java/io/legado/app/constant/PreferKey.kt
  2. 3
      app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt
  3. 25
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt
  4. 7
      app/src/main/res/xml/pref_config_backup.xml

@ -32,6 +32,7 @@ object PreferKey {
const val webDavUrl = "web_dav_url" const val webDavUrl = "web_dav_url"
const val webDavAccount = "web_dav_account" const val webDavAccount = "web_dav_account"
const val webDavPassword = "web_dav_password" const val webDavPassword = "web_dav_password"
const val webDavCreateDir = "webDavCreateDir"
const val changeSourceLoadToc = "changeSourceLoadToc" const val changeSourceLoadToc = "changeSourceLoadToc"
const val chineseConverterType = "chineseConverterType" const val chineseConverterType = "chineseConverterType"
const val launcherIcon = "launcherIcon" const val launcherIcon = "launcherIcon"

@ -11,7 +11,7 @@ import io.legado.app.utils.fromJsonArray
@Suppress("BlockingMethodInNonBlockingContext") @Suppress("BlockingMethodInNonBlockingContext")
object SyncBookProgress { object SyncBookProgress {
private val file = FileUtils.createFileIfNotExist(App.INSTANCE.cacheDir, "bookProgress.json") 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() { fun uploadBookProgress() {
Coroutine.async { Coroutine.async {
@ -20,7 +20,6 @@ object SyncBookProgress {
val json = GSON.toJson(value) val json = GSON.toJson(value)
file.writeText(json) file.writeText(json)
if (WebDavHelp.initWebDav()) { if (WebDavHelp.initWebDav()) {
WebDav(WebDavHelp.getWebDavUrl() + "legado").makeAsDir()
WebDav(webDavUrl).upload(file.absolutePath) WebDav(webDavUrl).upload(file.absolutePath)
} }
} }

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

@ -24,6 +24,13 @@
android:summary="@string/web_dav_pw_s" android:summary="@string/web_dav_pw_s"
app:iconSpaceReserved="false" /> app:iconSpaceReserved="false" />
<io.legado.app.ui.widget.prefs.SwitchPreference
android:key="webDavCreateDir"
android:defaultValue="true"
android:title="创建子文件夹"
android:summary="创建legado文件夹作为备份文件夹"
app:iconSpaceReserved="false" />
</io.legado.app.ui.widget.prefs.PreferenceCategory> </io.legado.app.ui.widget.prefs.PreferenceCategory>
<io.legado.app.ui.widget.prefs.PreferenceCategory app:iconSpaceReserved="false"> <io.legado.app.ui.widget.prefs.PreferenceCategory app:iconSpaceReserved="false">

Loading…
Cancel
Save