pull/32/head
gedoor 6 years ago
parent a17c33e989
commit 7475d4213f
  1. 60
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt
  2. 12
      app/src/main/java/io/legado/app/lib/webdav/http/OkHttp.kt

@ -29,7 +29,6 @@ import okhttp3.Response
class WebDav @Throws(MalformedURLException::class) class WebDav @Throws(MalformedURLException::class)
constructor(url: String) { constructor(url: String) {
private val url: URL = URL(null, url, Handler.HANDLER) private val url: URL = URL(null, url, Handler.HANDLER)
private var httpUrl: String? = null private var httpUrl: String? = null
@ -45,7 +44,7 @@ constructor(url: String) {
return field return field
} }
private val okHttpClient: OkHttpClient = OkHttp.instance.okHttpClient private val okHttpClient: OkHttpClient = OkHttp.SingletonHelper.okHttpClient
val path: String val path: String
get() = url.toString() get() = url.toString()
@ -157,21 +156,22 @@ constructor(url: String) {
val list = ArrayList<WebDav>() val list = ArrayList<WebDav>()
val document = Jsoup.parse(s) val document = Jsoup.parse(s)
val elements = document.getElementsByTag("d:response") val elements = document.getElementsByTag("d:response")
val baseUrl = if (getUrl()!!.endsWith("/")) getUrl() else getUrl()!! + "/" getUrl()?.let { url->
for (element in elements) { val baseUrl = if (url.endsWith("/")) url else "$url/"
val href = element.getElementsByTag("d:href")[0].text() for (element in elements) {
if (!href.endsWith("/")) { val href = element.getElementsByTag("d:href")[0].text()
val fileName = href.substring(href.lastIndexOf("/") + 1) if (!href.endsWith("/")) {
val webDavFile: WebDav val fileName = href.substring(href.lastIndexOf("/") + 1)
try { val webDavFile: WebDav
webDavFile = WebDav(baseUrl!! + fileName) try {
webDavFile.displayName = fileName webDavFile = WebDav(baseUrl + fileName)
webDavFile.urlName = href webDavFile.displayName = fileName
list.add(webDavFile) webDavFile.urlName = href
} catch (e: MalformedURLException) { list.add(webDavFile)
e.printStackTrace() } catch (e: MalformedURLException) {
e.printStackTrace()
}
} }
} }
} }
return list return list
@ -184,10 +184,13 @@ constructor(url: String) {
*/ */
@Throws(IOException::class) @Throws(IOException::class)
fun makeAsDir(): Boolean { fun makeAsDir(): Boolean {
val request = Request.Builder() getUrl()?.let { url->
.url(getUrl()!!) val request = Request.Builder()
.method("MKCOL", null) .url(url)
return execRequest(request) .method("MKCOL", null)
return execRequest(request)
}
return false
} }
/** /**
@ -219,11 +222,13 @@ constructor(url: String) {
val mediaType = if (contentType == null) null else MediaType.parse(contentType) val mediaType = if (contentType == null) null else MediaType.parse(contentType)
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
val fileBody = RequestBody.create(mediaType, file) val fileBody = RequestBody.create(mediaType, file)
val request = Request.Builder() getUrl()?.let {
.url(getUrl()!!) val request = Request.Builder()
.put(fileBody) .url(it)
.put(fileBody)
return execRequest(request) return execRequest(request)
}
return false
} }
/** /**
@ -234,10 +239,7 @@ constructor(url: String) {
*/ */
@Throws(IOException::class) @Throws(IOException::class)
private fun execRequest(requestBuilder: Request.Builder): Boolean { private fun execRequest(requestBuilder: Request.Builder): Boolean {
val auth = HttpAuth.auth HttpAuth.auth?.let { requestBuilder.header("Authorization", Credentials.basic(it.user, it.pass)) }
if (auth != null) {
requestBuilder.header("Authorization", Credentials.basic(auth!!.user, auth!!.pass))
}
val response = okHttpClient.newCall(requestBuilder.build()).execute() val response = okHttpClient.newCall(requestBuilder.build()).execute()
return response.isSuccessful return response.isSuccessful

@ -4,16 +4,8 @@ import okhttp3.OkHttpClient
class OkHttp private constructor() { class OkHttp private constructor() {
val okHttpClient: OkHttpClient = OkHttpClient.Builder().build() object SingletonHelper {
val okHttpClient: OkHttpClient = OkHttpClient.Builder().build()
private object SingletonHelper {
val INSTANCE = OkHttp()
}
companion object {
val instance: OkHttp
get() = SingletonHelper.INSTANCE
} }
} }

Loading…
Cancel
Save