|
|
@ -30,13 +30,11 @@ import okhttp3.Response |
|
|
|
class WebDav @Throws(MalformedURLException::class) |
|
|
|
class WebDav @Throws(MalformedURLException::class) |
|
|
|
constructor(url: String) { |
|
|
|
constructor(url: String) { |
|
|
|
|
|
|
|
|
|
|
|
private val url: URL |
|
|
|
private val url: URL = URL(null, url, Handler.HANDLER) |
|
|
|
private var httpUrl: String? = null |
|
|
|
private var httpUrl: String? = null |
|
|
|
|
|
|
|
|
|
|
|
var displayName: String? = null |
|
|
|
var displayName: String? = null |
|
|
|
var lastModified: Long = 0 |
|
|
|
|
|
|
|
var size: Long = 0 |
|
|
|
var size: Long = 0 |
|
|
|
var isDirectory = true |
|
|
|
|
|
|
|
private var exists = false |
|
|
|
private var exists = false |
|
|
|
var parent = "" |
|
|
|
var parent = "" |
|
|
|
var urlName = "" |
|
|
|
var urlName = "" |
|
|
@ -47,43 +45,30 @@ constructor(url: String) { |
|
|
|
return field |
|
|
|
return field |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private val okHttpClient: OkHttpClient |
|
|
|
private val okHttpClient: OkHttpClient = OkHttp.instance.okHttpClient |
|
|
|
|
|
|
|
|
|
|
|
val path: String |
|
|
|
val path: String |
|
|
|
get() = url.toString() |
|
|
|
get() = url.toString() |
|
|
|
|
|
|
|
|
|
|
|
private val inputStream: InputStream? |
|
|
|
private val inputStream: InputStream? |
|
|
|
get() { |
|
|
|
get() = getUrl()?.let {url -> |
|
|
|
val request = Request.Builder() |
|
|
|
val request = Request.Builder().url(url) |
|
|
|
.url(getUrl()!!) |
|
|
|
HttpAuth.auth?.let {request.header("Authorization", Credentials.basic(it.user, it.pass)) } |
|
|
|
|
|
|
|
|
|
|
|
val auth = HttpAuth.auth |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (auth != null) { |
|
|
|
|
|
|
|
request.header("Authorization", Credentials.basic(auth!!.user, auth!!.pass)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
val response = okHttpClient.newCall(request.build()).execute() |
|
|
|
return okHttpClient.newCall(request.build()).execute().body()?.byteStream() |
|
|
|
return response.body()!!.byteStream() |
|
|
|
|
|
|
|
} catch (e: IOException) { |
|
|
|
} catch (e: IOException) { |
|
|
|
e.printStackTrace() |
|
|
|
e.printStackTrace() |
|
|
|
} catch (e: IllegalArgumentException) { |
|
|
|
} catch (e: IllegalArgumentException) { |
|
|
|
e.printStackTrace() |
|
|
|
e.printStackTrace() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return null |
|
|
|
return null |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
val host: String |
|
|
|
val host: String |
|
|
|
get() = url.host |
|
|
|
get() = url.host |
|
|
|
|
|
|
|
|
|
|
|
init { |
|
|
|
private fun getUrl(): String? { |
|
|
|
this.url = URL(null, url, Handler.HANDLER) |
|
|
|
|
|
|
|
okHttpClient = OkHttp.instance.okHttpClient |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun getUrl(): String? { |
|
|
|
|
|
|
|
if (httpUrl == null) { |
|
|
|
if (httpUrl == null) { |
|
|
|
val raw = url.toString().replace("davs://", "https://").replace("dav://", "http://") |
|
|
|
val raw = url.toString().replace("davs://", "https://").replace("dav://", "http://") |
|
|
|
try { |
|
|
|
try { |
|
|
@ -106,19 +91,17 @@ constructor(url: String) { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
@Throws(IOException::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
fun indexFileInfo(): Boolean { |
|
|
|
fun indexFileInfo(): Boolean { |
|
|
|
val response = propFindResponse(ArrayList()) |
|
|
|
propFindResponse(ArrayList())?.let { response -> |
|
|
|
var s = "" |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (!response.isSuccessful) { |
|
|
|
if (!response.isSuccessful) { |
|
|
|
this.exists = false |
|
|
|
this.exists = false |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
s = response.body()!!.string() |
|
|
|
response.body()?.let { |
|
|
|
} catch (e: Exception) { |
|
|
|
if (it.string().isNotEmpty()) { |
|
|
|
e.printStackTrace() |
|
|
|
return true |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -131,44 +114,44 @@ constructor(url: String) { |
|
|
|
@Throws(IOException::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
@JvmOverloads |
|
|
|
@JvmOverloads |
|
|
|
fun listFiles(propsList: ArrayList<String> = ArrayList()): List<WebDav> { |
|
|
|
fun listFiles(propsList: ArrayList<String> = ArrayList()): List<WebDav> { |
|
|
|
val response = propFindResponse(propsList) |
|
|
|
propFindResponse(propsList)?.let {response-> |
|
|
|
try { |
|
|
|
|
|
|
|
if (response.isSuccessful) { |
|
|
|
if (response.isSuccessful) { |
|
|
|
return parseDir(response.body()!!.string()) |
|
|
|
response.body()?.let {body-> |
|
|
|
|
|
|
|
return parseDir(body.string()) |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (e: Exception) { |
|
|
|
|
|
|
|
e.printStackTrace() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
return ArrayList() |
|
|
|
return ArrayList() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Throws(IOException::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
private fun propFindResponse(propsList: ArrayList<String>, depth: Int = 1): Response { |
|
|
|
private fun propFindResponse(propsList: ArrayList<String>, depth: Int = 1): Response? { |
|
|
|
val requestProps = StringBuilder() |
|
|
|
val requestProps = StringBuilder() |
|
|
|
for (p in propsList) { |
|
|
|
for (p in propsList) { |
|
|
|
requestProps.append("<a:").append(p).append("/>\n") |
|
|
|
requestProps.append("<a:").append(p).append("/>\n") |
|
|
|
} |
|
|
|
} |
|
|
|
val requestPropsStr: String |
|
|
|
val requestPropsStr: String |
|
|
|
if (requestProps.toString().isEmpty()) { |
|
|
|
requestPropsStr = if (requestProps.toString().isEmpty()) { |
|
|
|
requestPropsStr = DIR.replace("%s", "") |
|
|
|
DIR.replace("%s", "") |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
requestPropsStr = String.format(DIR, requestProps.toString() + "\n") |
|
|
|
String.format(DIR, requestProps.toString() + "\n") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
getUrl()?.let {url-> |
|
|
|
val request = Request.Builder() |
|
|
|
val request = Request.Builder() |
|
|
|
.url(getUrl()!!) |
|
|
|
.url(url) |
|
|
|
// 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性 |
|
|
|
// 添加RequestBody对象,可以只返回的属性。如果设为null,则会返回全部属性 |
|
|
|
// 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。 |
|
|
|
// 注意:尽量手动指定需要返回的属性。若返回全部属性,可能后由于Prop.java里没有该属性名,而崩溃。 |
|
|
|
.method("PROPFIND", RequestBody.create(MediaType.parse("text/plain"), requestPropsStr)) |
|
|
|
.method("PROPFIND", RequestBody.create(MediaType.parse("text/plain"), requestPropsStr)) |
|
|
|
|
|
|
|
|
|
|
|
val auth = HttpAuth.auth |
|
|
|
HttpAuth.auth?.let {request.header("Authorization", Credentials.basic(it.user, it.pass)) } |
|
|
|
if (auth != null) { |
|
|
|
|
|
|
|
request.header("Authorization", Credentials.basic(auth!!.user, auth!!.pass)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
request.header("Depth", if (depth < 0) "infinity" else Integer.toString(depth)) |
|
|
|
request.header("Depth", if (depth < 0) "infinity" else Integer.toString(depth)) |
|
|
|
|
|
|
|
|
|
|
|
return okHttpClient.newCall(request.build()).execute() |
|
|
|
return okHttpClient.newCall(request.build()).execute() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return null |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private fun parseDir(s: String): List<WebDav> { |
|
|
|
private fun parseDir(s: String): List<WebDav> { |
|
|
|
val list = ArrayList<WebDav>() |
|
|
|
val list = ArrayList<WebDav>() |
|
|
@ -214,7 +197,7 @@ constructor(url: String) { |
|
|
|
* @param replaceExisting 是否替换本地的同名文件 |
|
|
|
* @param replaceExisting 是否替换本地的同名文件 |
|
|
|
* @return 下载是否成功 |
|
|
|
* @return 下载是否成功 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
fun download(savedPath: String, replaceExisting: Boolean): Boolean { |
|
|
|
fun downloadTo(savedPath: String, replaceExisting: Boolean): Boolean { |
|
|
|
if (File(savedPath).exists()) { |
|
|
|
if (File(savedPath).exists()) { |
|
|
|
if (!replaceExisting) { |
|
|
|
if (!replaceExisting) { |
|
|
|
return false |
|
|
|
return false |
|
|
@ -222,9 +205,12 @@ constructor(url: String) { |
|
|
|
} |
|
|
|
} |
|
|
|
val inputS = inputStream ?: return false |
|
|
|
val inputS = inputStream ?: return false |
|
|
|
File(savedPath).writeBytes(inputS.readBytes()) |
|
|
|
File(savedPath).writeBytes(inputS.readBytes()) |
|
|
|
return false |
|
|
|
return true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 上传文件 |
|
|
|
|
|
|
|
*/ |
|
|
|
@Throws(IOException::class) |
|
|
|
@Throws(IOException::class) |
|
|
|
@JvmOverloads |
|
|
|
@JvmOverloads |
|
|
|
fun upload(localPath: String, contentType: String? = null): Boolean { |
|
|
|
fun upload(localPath: String, contentType: String? = null): Boolean { |
|
|
|