pull/981/head
gedoor 4 years ago
parent c6cffe7008
commit bdd75cc157
  1. 23
      app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt
  2. 42
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt

@ -123,4 +123,27 @@ fun Request.Builder.postJson(json: String?) {
fun Request.Builder.mkCol() {
method("MKCOL", null)
}
fun Request.Builder.propFind(fields: List<String>?) {
var propFind =
"""<?xml version="1.0"?>
<a:propfind xmlns:a="DAV:">
<a:prop>
%s
</a:prop>
</a:propfind>"""
// 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性
// 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。
if (fields.isNullOrEmpty()) {
method("PROPFIND", null)
} else {
val requestProps = StringBuilder()
for (p in fields) {
requestProps.append("<a:").append(p).append("/>\n")
}
propFind = String.format(propFind, requestProps.toString() + "\n")
val requestBody = propFind.toRequestBody("text/plain".toMediaType())
method("PROPFIND", requestBody)
}
}

@ -1,9 +1,6 @@
package io.legado.app.lib.webdav
import io.legado.app.help.http.mkCol
import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient
import io.legado.app.help.http.text
import io.legado.app.help.http.*
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.asRequestBody
@ -18,21 +15,6 @@ import java.util.*
@Suppress("unused", "MemberVisibilityCanBePrivate")
class WebDav(urlStr: String) {
companion object {
// 指定返回哪些属性
private const val DIR =
"""<?xml version="1.0"?>
<a:propfind xmlns:a="DAV:">
<a:prop>
<a:displayname/>
<a:resourcetype/>
<a:getcontentlength/>
<a:creationdate/>
<a:getlastmodified/>
%s
</a:prop>
</a:propfind>"""
}
private val url: URL = URL(urlStr)
private val httpUrl: String? by lazy {
@ -64,26 +46,17 @@ class WebDav(urlStr: String) {
/**
* 列出当前路径下的文件
*
* @param propsList 指定列出文件的哪些属性
* @param fields 指定列出文件的哪些属性
* @return 文件列表
*/
suspend fun listFiles(propsList: ArrayList<String> = ArrayList()): List<WebDav> {
propFindResponse(propsList)?.let { body ->
suspend fun listFiles(fields: ArrayList<String> = ArrayList()): List<WebDav> {
propFindResponse(fields)?.let { body ->
return parseDir(body)
}
return ArrayList()
}
private suspend fun propFindResponse(propsList: ArrayList<String>): String? {
val requestProps = StringBuilder()
for (p in propsList) {
requestProps.append("<a:").append(p).append("/>\n")
}
val requestPropsStr: String = if (requestProps.toString().isEmpty()) {
DIR.replace("%s", "")
} else {
String.format(DIR, requestProps.toString() + "\n")
}
private suspend fun propFindResponse(fields: ArrayList<String>?): String? {
val url = httpUrl
val auth = HttpAuth.auth
if (url != null && auth != null) {
@ -91,10 +64,7 @@ class WebDav(urlStr: String) {
okHttpClient.newCall {
url(url)
addHeader("Authorization", Credentials.basic(auth.user, auth.pass))
// 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性
// 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。
val requestBody = requestPropsStr.toRequestBody("text/plain".toMediaType())
method("PROPFIND", requestBody)
propFind(fields)
}.text()
}.onFailure {
it.printStackTrace()

Loading…
Cancel
Save