pull/2380/head
kunfei 2 years ago
parent 1a8ca99e42
commit d4875a8e7d
  1. 20
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  2. 4
      app/src/main/java/io/legado/app/lib/webdav/WebDavException.kt
  3. 11
      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 url: URL = URL(path)
private val httpUrl: String? by lazy { 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 { return@lazy kotlin.runCatching {
URLEncoder.encode(raw, "UTF-8") URLEncoder.encode(raw, "UTF-8")
.replace("\\+".toRegex(), "%20") .replace("+", "%20")
.replace("%3A".toRegex(), ":") .replace("%3A", ":")
.replace("%2F".toRegex(), "/") .replace("%2F", "/")
}.getOrNull() }.getOrNull()
} }
private val webDavClient by lazy { private val webDavClient by lazy {
@ -348,8 +350,18 @@ open class WebDav(val path: String, val authorization: Authorization) {
*/ */
private fun checkResult(response: Response) { private fun checkResult(response: Response) {
if (!response.isSuccessful) { if (!response.isSuccessful) {
val body = response.body?.string()
if (response.message.isNotBlank() || body.isNullOrBlank()) {
throw WebDavException("${url}\n${response.code}:${response.message}") 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 package io.legado.app.lib.webdav
class WebDavException(msg: String) : Exception(msg) { open class WebDavException(msg: String) : Exception(msg) {
override fun fillInStackTrace(): Throwable { override fun fillInStackTrace(): Throwable {
return this 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.getRemoteUrl
import io.legado.app.help.book.isLocal import io.legado.app.help.book.isLocal
import io.legado.app.help.coroutine.Coroutine 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.BookCover
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.remote.RemoteBookWebDav import io.legado.app.model.remote.RemoteBookWebDav
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -120,12 +122,19 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
book.origin = BookType.localTag book.origin = BookType.localTag
} else if (remoteBook.lastModify > book.lastCheckTime) { } else if (remoteBook.lastModify > book.lastCheckTime) {
val uri = RemoteBookWebDav.downloadRemoteBook(remoteBook) val uri = RemoteBookWebDav.downloadRemoteBook(remoteBook)
book.origin = if (uri.isContentScheme()) uri.toString() else uri.path!!
} }
} }
} }
}.onError { }.onError {
when (it) {
is ObjectNotFoundException -> {
book.origin = BookType.localTag
}
else -> {
AppLog.put("下载远程书籍<${book.name}>失败", it) AppLog.put("下载远程书籍<${book.name}>失败", it)
}
}
}.onFinally { }.onFinally {
loadBookInfo(book, false) loadBookInfo(book, false)
} }

Loading…
Cancel
Save