|
|
@ -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) { |
|
|
|