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

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

@ -4,16 +4,8 @@ import okhttp3.OkHttpClient
class OkHttp private constructor() {
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