|
|
|
@ -3,12 +3,7 @@ package io.legado.app.lib.webdav |
|
|
|
|
import io.legado.app.help.http.HttpHelper |
|
|
|
|
import io.legado.app.lib.webdav.http.Handler |
|
|
|
|
import io.legado.app.lib.webdav.http.HttpAuth |
|
|
|
|
import okhttp3.Credentials |
|
|
|
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull |
|
|
|
|
import okhttp3.Request |
|
|
|
|
import okhttp3.RequestBody.Companion.asRequestBody |
|
|
|
|
import okhttp3.RequestBody.Companion.toRequestBody |
|
|
|
|
import okhttp3.Response |
|
|
|
|
import okhttp3.* |
|
|
|
|
import org.jsoup.Jsoup |
|
|
|
|
import java.io.File |
|
|
|
|
import java.io.IOException |
|
|
|
@ -82,7 +77,7 @@ constructor(urlStr: String) { |
|
|
|
|
this.exists = false |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
response.body?.let { |
|
|
|
|
response.body()?.let { |
|
|
|
|
if (it.string().isNotEmpty()) { |
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
@ -102,7 +97,7 @@ constructor(urlStr: String) { |
|
|
|
|
fun listFiles(propsList: ArrayList<String> = ArrayList()): List<WebDav> { |
|
|
|
|
propFindResponse(propsList)?.let { response -> |
|
|
|
|
if (response.isSuccessful) { |
|
|
|
|
response.body?.let { body -> |
|
|
|
|
response.body()?.let { body -> |
|
|
|
|
return parseDir(body.string()) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -127,7 +122,10 @@ constructor(urlStr: String) { |
|
|
|
|
.url(url) |
|
|
|
|
// 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性 |
|
|
|
|
// 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。 |
|
|
|
|
.method("PROPFIND", requestPropsStr.toRequestBody("text/plain".toMediaTypeOrNull())) |
|
|
|
|
.method( |
|
|
|
|
"PROPFIND", |
|
|
|
|
RequestBody.create(MediaType.parse("text/plain"), requestPropsStr) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
HttpAuth.auth?.let { |
|
|
|
|
request.header( |
|
|
|
@ -205,9 +203,9 @@ constructor(urlStr: String) { |
|
|
|
|
fun upload(localPath: String, contentType: String? = null): Boolean { |
|
|
|
|
val file = File(localPath) |
|
|
|
|
if (!file.exists()) return false |
|
|
|
|
val mediaType = contentType?.toMediaTypeOrNull() |
|
|
|
|
val mediaType = contentType?.let { MediaType.parse(it) } |
|
|
|
|
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 |
|
|
|
|
val fileBody = file.asRequestBody(mediaType) |
|
|
|
|
val fileBody = RequestBody.create(mediaType, file) |
|
|
|
|
httpUrl?.let { |
|
|
|
|
val request = Request.Builder() |
|
|
|
|
.url(it) |
|
|
|
@ -241,7 +239,7 @@ constructor(urlStr: String) { |
|
|
|
|
request.header("Authorization", Credentials.basic(it.user, it.pass)) |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
return HttpHelper.client.newCall(request.build()).execute().body?.byteStream() |
|
|
|
|
return HttpHelper.client.newCall(request.build()).execute().body()?.byteStream() |
|
|
|
|
} catch (e: IOException) { |
|
|
|
|
e.printStackTrace() |
|
|
|
|
} catch (e: IllegalArgumentException) { |
|
|
|
|