给cronet添加默认content-type防止报错

pull/1171/head
gedoor 3 years ago
parent 1f57bae657
commit f1bbea129c
  1. 2
      app/src/main/java/io/legado/app/help/http/cronet/CronetHelper.kt
  2. 4
      app/src/main/java/io/legado/app/help/storage/BookWebDav.kt
  3. 11
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt

@ -53,6 +53,8 @@ fun buildRequest(request: Request, callback: UrlRequest.Callback): UrlRequest {
val contentType: MediaType? = requestBody.contentType() val contentType: MediaType? = requestBody.contentType()
if (contentType != null) { if (contentType != null) {
requestBuilder.addHeader("Content-Type", contentType.toString()) requestBuilder.addHeader("Content-Type", contentType.toString())
} else {
requestBuilder.addHeader("Content-Type", "text/plain")
} }
val buffer = Buffer() val buffer = Buffer()
requestBody.writeTo(buffer) requestBody.writeTo(buffer)

@ -131,7 +131,7 @@ object BookWebDav {
WebDav(exportsWebDavUrl).makeAsDir() WebDav(exportsWebDavUrl).makeAsDir()
// 如果导出的本地文件存在,开始上传 // 如果导出的本地文件存在,开始上传
val putUrl = exportsWebDavUrl + fileName val putUrl = exportsWebDavUrl + fileName
WebDav(putUrl).upload(byteArray) WebDav(putUrl).upload(byteArray, "text/plain")
} }
} catch (e: Exception) { } catch (e: Exception) {
Handler(Looper.getMainLooper()).post { Handler(Looper.getMainLooper()).post {
@ -153,7 +153,7 @@ object BookWebDav {
val json = GSON.toJson(bookProgress) val json = GSON.toJson(bookProgress)
val url = getProgressUrl(book) val url = getProgressUrl(book)
if (initWebDav()) { if (initWebDav()) {
WebDav(url).upload(json.toByteArray()) WebDav(url).upload(json.toByteArray(), "application/json")
} }
} }
} }

@ -185,11 +185,14 @@ class WebDav(urlStr: String) {
/** /**
* 上传文件 * 上传文件
*/ */
suspend fun upload(localPath: String, contentType: String? = null): Boolean { suspend fun upload(
localPath: String,
contentType: String = "application/octet-stream"
): Boolean {
val file = File(localPath) val file = File(localPath)
if (!file.exists()) return false if (!file.exists()) return false
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
val fileBody = file.asRequestBody(contentType?.toMediaType()) val fileBody = file.asRequestBody(contentType.toMediaType())
val url = httpUrl val url = httpUrl
val auth = HttpAuth.auth val auth = HttpAuth.auth
if (url != null && auth != null) { if (url != null && auth != null) {
@ -204,9 +207,9 @@ class WebDav(urlStr: String) {
return false return false
} }
suspend fun upload(byteArray: ByteArray, contentType: String? = null): Boolean { suspend fun upload(byteArray: ByteArray, contentType: String): Boolean {
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息 // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
val fileBody = byteArray.toRequestBody(contentType?.toMediaType()) val fileBody = byteArray.toRequestBody(contentType.toMediaType())
val url = httpUrl val url = httpUrl
val auth = HttpAuth.auth val auth = HttpAuth.auth
if (url != null && auth != null) { if (url != null && auth != null) {

Loading…
Cancel
Save