From bdd75cc157773b7518711e56191c25533f145be5 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 9 May 2021 23:07:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/http/OkHttpUtils.kt | 23 ++++++++++ .../java/io/legado/app/lib/webdav/WebDav.kt | 42 +++---------------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt index bb50bf60a..efdc5839a 100644 --- a/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt +++ b/app/src/main/java/io/legado/app/help/http/OkHttpUtils.kt @@ -123,4 +123,27 @@ fun Request.Builder.postJson(json: String?) { fun Request.Builder.mkCol() { method("MKCOL", null) +} + +fun Request.Builder.propFind(fields: List?) { + var propFind = + """ + + + %s + + """ + // 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性 + // 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。 + if (fields.isNullOrEmpty()) { + method("PROPFIND", null) + } else { + val requestProps = StringBuilder() + for (p in fields) { + requestProps.append("\n") + } + propFind = String.format(propFind, requestProps.toString() + "\n") + val requestBody = propFind.toRequestBody("text/plain".toMediaType()) + method("PROPFIND", requestBody) + } } \ No newline at end of file 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 8d20fdd17..654a6d277 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 @@ -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 = - """ - - - - - - - - %s - - """ - } 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 = ArrayList()): List { - propFindResponse(propsList)?.let { body -> + suspend fun listFiles(fields: ArrayList = ArrayList()): List { + propFindResponse(fields)?.let { body -> return parseDir(body) } return ArrayList() } - private suspend fun propFindResponse(propsList: ArrayList): String? { - val requestProps = StringBuilder() - for (p in propsList) { - requestProps.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? { 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()