|
|
|
@ -6,6 +6,7 @@ import io.legado.app.exception.NoStackTraceException |
|
|
|
|
import io.legado.app.help.http.newCallResponse |
|
|
|
|
import io.legado.app.help.http.okHttpClient |
|
|
|
|
import io.legado.app.help.http.text |
|
|
|
|
import io.legado.app.utils.NetworkUtils |
|
|
|
|
import io.legado.app.utils.printOnDebug |
|
|
|
|
import okhttp3.MediaType.Companion.toMediaType |
|
|
|
|
import okhttp3.RequestBody.Companion.asRequestBody |
|
|
|
@ -61,11 +62,14 @@ open class WebDav(urlStr: String, val authorization: Authorization) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 填充文件信息。实例化WebDAVFile对象时,并没有将远程文件的信息填充到实例中。需要手动填充! |
|
|
|
|
* @return 远程文件是否存在 |
|
|
|
|
* 获取当前url文件信息 |
|
|
|
|
*/ |
|
|
|
|
suspend fun indexFileInfo(): Boolean { |
|
|
|
|
return !propFindResponse(ArrayList()).isNullOrEmpty() |
|
|
|
|
suspend fun getWebDavFile(): WebDavFile? { |
|
|
|
|
return kotlin.runCatching { |
|
|
|
|
propFindResponse(depth = 0)?.let { |
|
|
|
|
return parseBody(it).firstOrNull() |
|
|
|
|
} |
|
|
|
|
}.getOrNull() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -76,9 +80,11 @@ open class WebDav(urlStr: String, val authorization: Authorization) { |
|
|
|
|
@Throws(WebDavException::class) |
|
|
|
|
suspend fun listFiles(): List<WebDavFile> { |
|
|
|
|
propFindResponse()?.let { body -> |
|
|
|
|
return parseDir(body) |
|
|
|
|
return parseBody(body).filter { |
|
|
|
|
it.path != path |
|
|
|
|
} |
|
|
|
|
return ArrayList() |
|
|
|
|
} |
|
|
|
|
return emptyList() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -112,12 +118,12 @@ open class WebDav(urlStr: String, val authorization: Authorization) { |
|
|
|
|
}.body?.text() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private fun parseDir(s: String): List<WebDavFile> { |
|
|
|
|
private fun parseBody(s: String): List<WebDavFile> { |
|
|
|
|
val list = ArrayList<WebDavFile>() |
|
|
|
|
val document = Jsoup.parse(s) |
|
|
|
|
val elements = document.getElementsByTag("d:response") |
|
|
|
|
httpUrl?.let { urlStr -> |
|
|
|
|
val baseUrl = if (urlStr.endsWith("/")) urlStr else "$urlStr/" |
|
|
|
|
val baseUrl = NetworkUtils.getBaseUrl(urlStr) |
|
|
|
|
for (element in elements) { |
|
|
|
|
val href = element.getElementsByTag("d:href")[0].text() |
|
|
|
|
if (!href.endsWith("/")) { |
|
|
|
@ -137,11 +143,12 @@ open class WebDav(urlStr: String, val authorization: Authorization) { |
|
|
|
|
val lastModify: Long = kotlin.runCatching { |
|
|
|
|
element.getElementsByTag("d:getlastmodified") |
|
|
|
|
.firstOrNull()?.text()?.let { |
|
|
|
|
LocalDateTime.parse(it, dateTimeFormatter).toInstant(ZoneOffset.of("+8")).toEpochMilli() |
|
|
|
|
LocalDateTime.parse(it, dateTimeFormatter) |
|
|
|
|
.toInstant(ZoneOffset.of("+8")).toEpochMilli() |
|
|
|
|
} |
|
|
|
|
}.getOrNull() ?: 0 |
|
|
|
|
webDavFile = WebDavFile( |
|
|
|
|
baseUrl + fileName, |
|
|
|
|
"$baseUrl$href", |
|
|
|
|
authorization, |
|
|
|
|
displayName = fileName, |
|
|
|
|
urlName = urlName, |
|
|
|
@ -163,12 +170,7 @@ open class WebDav(urlStr: String, val authorization: Authorization) { |
|
|
|
|
* 文件是否存在 |
|
|
|
|
*/ |
|
|
|
|
suspend fun exists(): Boolean { |
|
|
|
|
return kotlin.runCatching { |
|
|
|
|
val response = propFindResponse(depth = 0) ?: return false |
|
|
|
|
val document = Jsoup.parse(response) |
|
|
|
|
val elements = document.getElementsByTag("d:response") |
|
|
|
|
return elements.isNotEmpty() |
|
|
|
|
}.getOrDefault(false) |
|
|
|
|
return getWebDavFile() != null |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|