pull/2380/head
kunfei 2 years ago
parent 1a8ca99e42
commit d4875a8e7d
  1. 22
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  2. 6
      app/src/main/java/io/legado/app/lib/webdav/WebDavException.kt
  3. 13
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt

@ -62,12 +62,14 @@ open class WebDav(val path: String, val authorization: Authorization) {
private val url: URL = URL(path)
private val httpUrl: String? by lazy {
val raw = url.toString().replace("davs://", "https://").replace("dav://", "http://")
val raw = url.toString()
.replace("davs://", "https://")
.replace("dav://", "http://")
return@lazy kotlin.runCatching {
URLEncoder.encode(raw, "UTF-8")
.replace("\\+".toRegex(), "%20")
.replace("%3A".toRegex(), ":")
.replace("%2F".toRegex(), "/")
.replace("+", "%20")
.replace("%3A", ":")
.replace("%2F", "/")
}.getOrNull()
}
private val webDavClient by lazy {
@ -348,7 +350,17 @@ open class WebDav(val path: String, val authorization: Authorization) {
*/
private fun checkResult(response: Response) {
if (!response.isSuccessful) {
throw WebDavException("${url}\n${response.code}:${response.message}")
val body = response.body?.string()
if (response.message.isNotBlank() || body.isNullOrBlank()) {
throw WebDavException("${url}\n${response.code}:${response.message}")
}
val document = Jsoup.parse(body)
val exception = document.getElementsByTag("s:exception").firstOrNull()?.text()
val message = document.getElementsByTag("s:message").firstOrNull()?.text()
if (exception == "ObjectNotFound") {
throw ObjectNotFoundException(message ?: "$path doesn't exist")
}
throw WebDavException(message ?: "未知错误")
}
}

@ -1,9 +1,11 @@
package io.legado.app.lib.webdav
class WebDavException(msg: String) : Exception(msg) {
open class WebDavException(msg: String) : Exception(msg) {
override fun fillInStackTrace(): Throwable {
return this
}
}
}
class ObjectNotFoundException(msg: String) : WebDavException(msg)

@ -19,12 +19,14 @@ import io.legado.app.help.book.BookHelp
import io.legado.app.help.book.getRemoteUrl
import io.legado.app.help.book.isLocal
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.webdav.ObjectNotFoundException
import io.legado.app.model.BookCover
import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.remote.RemoteBookWebDav
import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.CoroutineScope
@ -120,12 +122,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
book.origin = BookType.localTag
} else if (remoteBook.lastModify > book.lastCheckTime) {
val uri = RemoteBookWebDav.downloadRemoteBook(remoteBook)
book.origin = if (uri.isContentScheme()) uri.toString() else uri.path!!
}
}
}
}.onError {
AppLog.put("下载远程书籍<${book.name}>失败", it)
when (it) {
is ObjectNotFoundException -> {
book.origin = BookType.localTag
}
else -> {
AppLog.put("下载远程书籍<${book.name}>失败", it)
}
}
}.onFinally {
loadBookInfo(book, false)
}

Loading…
Cancel
Save