优化
This commit is contained in:
@@ -135,9 +135,9 @@ object AppWebDav {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Throws(Exception::class)
|
||||||
suspend fun backUpWebDav(path: String) {
|
suspend fun backUpWebDav(path: String) {
|
||||||
if (!NetworkUtils.isAvailable()) return
|
if (!NetworkUtils.isAvailable()) return
|
||||||
try {
|
|
||||||
authorization?.let {
|
authorization?.let {
|
||||||
val paths = arrayListOf(*Backup.backupFileNames)
|
val paths = arrayListOf(*Backup.backupFileNames)
|
||||||
for (i in 0 until paths.size) {
|
for (i in 0 until paths.size) {
|
||||||
@@ -149,9 +149,6 @@ object AppWebDav {
|
|||||||
WebDav(putUrl, it).upload(zipFilePath)
|
WebDav(putUrl, it).upload(zipFilePath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
|
||||||
appCtx.toastOnUi("WebDav\n${e.localizedMessage}")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun exportWebDav(byteArray: ByteArray, fileName: String) {
|
suspend fun exportWebDav(byteArray: ByteArray, fileName: String) {
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package io.legado.app.help.storage
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.documentfile.provider.DocumentFile
|
import androidx.documentfile.provider.DocumentFile
|
||||||
import io.legado.app.R
|
|
||||||
import io.legado.app.constant.AppLog
|
|
||||||
import io.legado.app.constant.PreferKey
|
import io.legado.app.constant.PreferKey
|
||||||
import io.legado.app.data.appDb
|
import io.legado.app.data.appDb
|
||||||
import io.legado.app.help.config.ReadBookConfig
|
import io.legado.app.help.config.ReadBookConfig
|
||||||
@@ -56,9 +54,6 @@ object Backup {
|
|||||||
} else {
|
} else {
|
||||||
context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis())
|
context.putPrefLong(PreferKey.lastBackup, System.currentTimeMillis())
|
||||||
}
|
}
|
||||||
}.onError {
|
|
||||||
AppLog.put("备份出错\n${it.localizedMessage}", it)
|
|
||||||
appCtx.toastOnUi(appCtx.getString(R.string.autobackup_fail, it.localizedMessage))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -107,7 +102,6 @@ object Backup {
|
|||||||
}
|
}
|
||||||
edit.commit()
|
edit.commit()
|
||||||
}
|
}
|
||||||
AppWebDav.backUpWebDav(backupPath)
|
|
||||||
when {
|
when {
|
||||||
path.isNullOrBlank() -> {
|
path.isNullOrBlank() -> {
|
||||||
copyBackup(context.getExternalFilesDir(null)!!, false)
|
copyBackup(context.getExternalFilesDir(null)!!, false)
|
||||||
@@ -119,6 +113,7 @@ object Backup {
|
|||||||
copyBackup(File(path), isAuto)
|
copyBackup(File(path), isAuto)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
AppWebDav.backUpWebDav(backupPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -195,16 +195,17 @@ class WebDav(urlStr: String, val authorization: Authorization) {
|
|||||||
/**
|
/**
|
||||||
* 上传文件
|
* 上传文件
|
||||||
*/
|
*/
|
||||||
|
@Throws(Exception::class)
|
||||||
suspend fun upload(
|
suspend fun upload(
|
||||||
localPath: String,
|
localPath: String,
|
||||||
contentType: String = "application/octet-stream"
|
contentType: String = "application/octet-stream"
|
||||||
): Boolean {
|
) {
|
||||||
|
kotlin.runCatching {
|
||||||
val file = File(localPath)
|
val file = File(localPath)
|
||||||
if (!file.exists()) return false
|
if (!file.exists()) throw NoStackTraceException("文件不存在")
|
||||||
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
|
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
|
||||||
val fileBody = file.asRequestBody(contentType.toMediaType())
|
val fileBody = file.asRequestBody(contentType.toMediaType())
|
||||||
val url = httpUrl ?: return false
|
val url = httpUrl ?: throw NoStackTraceException("url不能为空")
|
||||||
return kotlin.runCatching {
|
|
||||||
okHttpClient.newCallResponse {
|
okHttpClient.newCallResponse {
|
||||||
url(url)
|
url(url)
|
||||||
put(fileBody)
|
put(fileBody)
|
||||||
@@ -213,15 +214,18 @@ class WebDav(urlStr: String, val authorization: Authorization) {
|
|||||||
checkResult(it)
|
checkResult(it)
|
||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
AppLog.put("WebDav上传失败\n${it.localizedMessage}")
|
val message = "WebDav上传失败\n${it.localizedMessage}"
|
||||||
}.isSuccess
|
AppLog.put(message)
|
||||||
|
throw NoStackTraceException(message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun upload(byteArray: ByteArray, contentType: String): Boolean {
|
@Throws(Exception::class)
|
||||||
|
suspend fun upload(byteArray: ByteArray, contentType: String) {
|
||||||
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
|
// 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
|
||||||
|
kotlin.runCatching {
|
||||||
val fileBody = byteArray.toRequestBody(contentType.toMediaType())
|
val fileBody = byteArray.toRequestBody(contentType.toMediaType())
|
||||||
val url = httpUrl ?: return false
|
val url = httpUrl ?: throw NoStackTraceException("url不能为空")
|
||||||
return kotlin.runCatching {
|
|
||||||
okHttpClient.newCallResponse {
|
okHttpClient.newCallResponse {
|
||||||
url(url)
|
url(url)
|
||||||
put(fileBody)
|
put(fileBody)
|
||||||
@@ -230,8 +234,10 @@ class WebDav(urlStr: String, val authorization: Authorization) {
|
|||||||
checkResult(it)
|
checkResult(it)
|
||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
AppLog.put("WebDav上传失败\n${it.localizedMessage}")
|
val message = "WebDav上传失败\n${it.localizedMessage}"
|
||||||
}.isSuccess
|
AppLog.put(message)
|
||||||
|
throw NoStackTraceException(message)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun getInputStream(): InputStream? {
|
private suspend fun getInputStream(): InputStream? {
|
||||||
@@ -246,7 +252,7 @@ class WebDav(urlStr: String, val authorization: Authorization) {
|
|||||||
|
|
||||||
private fun checkResult(response: Response) {
|
private fun checkResult(response: Response) {
|
||||||
if (!response.isSuccessful) {
|
if (!response.isSuccessful) {
|
||||||
throw NoStackTraceException(response.message)
|
throw NoStackTraceException("${url}\n${response.code}:${response.message}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user