pull/1822/head
kunfei 3 years ago
parent 4c38acd664
commit 3bd220a99c
  1. 23
      app/src/main/java/io/legado/app/help/storage/AppWebDav.kt

@ -1,8 +1,6 @@
package io.legado.app.help.storage
import android.content.Context
import android.os.Handler
import android.os.Looper
import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.data.appDb
@ -24,11 +22,14 @@ import java.io.File
import java.text.SimpleDateFormat
import java.util.*
/**
* webDav初始化会访问网络,不要放到主线程
*/
object AppWebDav {
private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/"
private val bookProgressUrl = "${rootWebDavUrl}bookProgress/"
private val zipFilePath = "${appCtx.externalFiles.absolutePath}${File.separator}backup.zip"
private val bookProgressUrl get() = "${rootWebDavUrl}bookProgress/"
private val exportsWebDavUrl get() = "$rootWebDavUrl${EncoderUtils.escape("exports")}/"
val syncBookProgress get() = appCtx.getPrefBoolean(PreferKey.syncBookProgress, true)
var authorization: Authorization? = null
@ -70,7 +71,7 @@ object AppWebDav {
val mAuthorization = Authorization(account, password)
WebDav(rootWebDavUrl, mAuthorization).makeAsDir()
WebDav(bookProgressUrl, mAuthorization).makeAsDir()
WebDav(exportsWebDavUrl, mAuthorization).makeAsDir()
authorization = mAuthorization
}
}
@ -155,20 +156,14 @@ object AppWebDav {
suspend fun exportWebDav(byteArray: ByteArray, fileName: String) {
try {
authorization?.let {
// 默认导出到legado文件夹下exports目录
val exportsWebDavUrl = rootWebDavUrl + EncoderUtils.escape("exports") + "/"
// 在legado文件夹创建exports目录,如果不存在的话
WebDav(exportsWebDavUrl, it).makeAsDir()
// 如果导出的本地文件存在,开始上传
val putUrl = exportsWebDavUrl + fileName
WebDav(putUrl, it).upload(byteArray, "text/plain")
}
} catch (e: Exception) {
Handler(Looper.getMainLooper()).post {
appCtx.toastOnUi("WebDav导出\n${e.localizedMessage}")
}
}
}
fun uploadBookProgress(book: Book) {
val authorization = authorization ?: return
@ -206,9 +201,9 @@ object AppWebDav {
authorization ?: return
appDb.bookDao.all.forEach { book ->
getBookProgress(book)?.let { bookProgress ->
if (bookProgress.durChapterIndex > book.durChapterIndex ||
(bookProgress.durChapterIndex == book.durChapterIndex &&
bookProgress.durChapterPos > book.durChapterPos)
if (bookProgress.durChapterIndex > book.durChapterIndex
|| (bookProgress.durChapterIndex == book.durChapterIndex
&& bookProgress.durChapterPos > book.durChapterPos)
) {
book.durChapterIndex = bookProgress.durChapterIndex
book.durChapterPos = bookProgress.durChapterPos

Loading…
Cancel
Save