pull/1827/head
kunfei 3 years ago
parent ab8e44ee03
commit 070a64c4a9
  1. 14
      app/src/main/java/io/legado/app/help/storage/AppWebDav.kt
  2. 50
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt

@ -13,6 +13,7 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.lib.dialogs.selector import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.webdav.Authorization import io.legado.app.lib.webdav.Authorization
import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.WebDav
import io.legado.app.lib.webdav.WebDavException
import io.legado.app.utils.* import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main import kotlinx.coroutines.Dispatchers.Main
@ -111,7 +112,7 @@ object AppWebDav {
Coroutine.async { Coroutine.async {
restoreWebDav(names[index]) restoreWebDav(names[index])
}.onError { }.onError {
appCtx.toastOnUi("WebDavError\n${it.localizedMessage}") appCtx.toastOnUi("WebDav恢复出错\n${it.localizedMessage}")
} }
} }
} }
@ -121,6 +122,7 @@ object AppWebDav {
} }
} }
@Throws(WebDavException::class)
private suspend fun restoreWebDav(name: String) { private suspend fun restoreWebDav(name: String) {
authorization?.let { authorization?.let {
val webDav = WebDav(rootWebDavUrl + name, it) val webDav = WebDav(rootWebDavUrl + name, it)
@ -195,10 +197,12 @@ object AppWebDav {
suspend fun getBookProgress(book: Book): BookProgress? { suspend fun getBookProgress(book: Book): BookProgress? {
authorization?.let { authorization?.let {
val url = getProgressUrl(book) val url = getProgressUrl(book)
WebDav(url, it).download()?.let { byteArray -> kotlin.runCatching {
val json = String(byteArray) WebDav(url, it).download().let { byteArray ->
if (json.isJson()) { val json = String(byteArray)
return GSON.fromJsonObject<BookProgress>(json).getOrNull() if (json.isJson()) {
return GSON.fromJsonObject<BookProgress>(json).getOrNull()
}
} }
} }
} }

@ -3,7 +3,6 @@ package io.legado.app.lib.webdav
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.http.newCallResponse import io.legado.app.help.http.newCallResponse
import io.legado.app.help.http.newCallResponseBody
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text import io.legado.app.help.http.text
import io.legado.app.utils.printOnDebug import io.legado.app.utils.printOnDebug
@ -14,6 +13,7 @@ import okhttp3.Response
import org.intellij.lang.annotations.Language import org.intellij.lang.annotations.Language
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.io.File import java.io.File
import java.io.FileOutputStream
import java.io.InputStream import java.io.InputStream
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URL import java.net.URL
@ -178,23 +178,31 @@ class WebDav(urlStr: String, val authorization: Authorization) {
/** /**
* 下载到本地 * 下载到本地
*
* @param savedPath 本地的完整路径包括最后的文件名 * @param savedPath 本地的完整路径包括最后的文件名
* @param replaceExisting 是否替换本地的同名文件 * @param replaceExisting 是否替换本地的同名文件
* @return 下载是否成功
*/ */
suspend fun downloadTo(savedPath: String, replaceExisting: Boolean): Boolean { @Suppress("BlockingMethodInNonBlockingContext")
if (File(savedPath).exists()) { @Throws(WebDavException::class)
if (!replaceExisting) return false suspend fun downloadTo(savedPath: String, replaceExisting: Boolean) {
val file = File(savedPath)
if (file.exists() && !replaceExisting) {
return
}
downloadInputStream().use { byteStream ->
FileOutputStream(file).use {
byteStream.copyTo(it)
}
} }
val inputS = getInputStream() ?: return false
File(savedPath).writeBytes(inputS.readBytes())
return true
} }
suspend fun download(): ByteArray? { /**
val inputS = getInputStream() ?: return null * 下载文件,返回ByteArray
return inputS.readBytes() */
@Throws(WebDavException::class)
suspend fun download(): ByteArray {
return downloadInputStream().use {
it.readBytes()
}
} }
/** /**
@ -241,14 +249,16 @@ class WebDav(urlStr: String, val authorization: Authorization) {
} }
} }
private suspend fun getInputStream(): InputStream? { @Throws(WebDavException::class)
val url = httpUrl ?: return null private suspend fun downloadInputStream(): InputStream {
return kotlin.runCatching { val url = httpUrl ?: throw WebDavException("WebDav下载出错\nurl为空")
okHttpClient.newCallResponseBody { val byteStream = okHttpClient.newCallResponse {
url(url) url(url)
addHeader(authorization.name, authorization.data) addHeader(authorization.name, authorization.data)
}.byteStream() }.apply {
}.getOrNull() checkResult(this)
}.body?.byteStream()
return byteStream ?: throw WebDavException("WebDav下载出错\nNull Exception")
} }
private fun checkResult(response: Response) { private fun checkResult(response: Response) {

Loading…
Cancel
Save