diff --git a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt index e747a9195..cb17b2ead 100644 --- a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt +++ b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt @@ -13,6 +13,7 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.selector import io.legado.app.lib.webdav.Authorization import io.legado.app.lib.webdav.WebDav +import io.legado.app.lib.webdav.WebDavException import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.Main @@ -111,7 +112,7 @@ object AppWebDav { Coroutine.async { restoreWebDav(names[index]) }.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) { authorization?.let { val webDav = WebDav(rootWebDavUrl + name, it) @@ -195,10 +197,12 @@ object AppWebDav { suspend fun getBookProgress(book: Book): BookProgress? { authorization?.let { val url = getProgressUrl(book) - WebDav(url, it).download()?.let { byteArray -> - val json = String(byteArray) - if (json.isJson()) { - return GSON.fromJsonObject(json).getOrNull() + kotlin.runCatching { + WebDav(url, it).download().let { byteArray -> + val json = String(byteArray) + if (json.isJson()) { + return GSON.fromJsonObject(json).getOrNull() + } } } } diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index 6f5c20d17..c21ddab47 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -3,7 +3,6 @@ package io.legado.app.lib.webdav import io.legado.app.constant.AppLog import io.legado.app.exception.NoStackTraceException 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.text import io.legado.app.utils.printOnDebug @@ -14,6 +13,7 @@ import okhttp3.Response import org.intellij.lang.annotations.Language import org.jsoup.Jsoup import java.io.File +import java.io.FileOutputStream import java.io.InputStream import java.net.MalformedURLException import java.net.URL @@ -178,23 +178,31 @@ class WebDav(urlStr: String, val authorization: Authorization) { /** * 下载到本地 - * * @param savedPath 本地的完整路径,包括最后的文件名 * @param replaceExisting 是否替换本地的同名文件 - * @return 下载是否成功 */ - suspend fun downloadTo(savedPath: String, replaceExisting: Boolean): Boolean { - if (File(savedPath).exists()) { - if (!replaceExisting) return false + @Suppress("BlockingMethodInNonBlockingContext") + @Throws(WebDavException::class) + 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 - return inputS.readBytes() + /** + * 下载文件,返回ByteArray + */ + @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? { - val url = httpUrl ?: return null - return kotlin.runCatching { - okHttpClient.newCallResponseBody { - url(url) - addHeader(authorization.name, authorization.data) - }.byteStream() - }.getOrNull() + @Throws(WebDavException::class) + private suspend fun downloadInputStream(): InputStream { + val url = httpUrl ?: throw WebDavException("WebDav下载出错\nurl为空") + val byteStream = okHttpClient.newCallResponse { + url(url) + addHeader(authorization.name, authorization.data) + }.apply { + checkResult(this) + }.body?.byteStream() + return byteStream ?: throw WebDavException("WebDav下载出错\nNull Exception") } private fun checkResult(response: Response) {