kunfei %!s(int64=4) %!d(string=hai) anos
pai
achega
63f48c53ee

+ 10 - 13
app/src/main/java/io/legado/app/help/storage/AppWebDav.kt

@@ -135,22 +135,19 @@ object AppWebDav {
         return false
     }
 
+    @Throws(Exception::class)
     suspend fun backUpWebDav(path: String) {
         if (!NetworkUtils.isAvailable()) return
-        try {
-            authorization?.let {
-                val paths = arrayListOf(*Backup.backupFileNames)
-                for (i in 0 until paths.size) {
-                    paths[i] = path + File.separator + paths[i]
-                }
-                FileUtils.delete(zipFilePath)
-                if (ZipUtils.zipFiles(paths, zipFilePath)) {
-                    val putUrl = "${rootWebDavUrl}${backupFileName}"
-                    WebDav(putUrl, it).upload(zipFilePath)
-                }
+        authorization?.let {
+            val paths = arrayListOf(*Backup.backupFileNames)
+            for (i in 0 until paths.size) {
+                paths[i] = path + File.separator + paths[i]
+            }
+            FileUtils.delete(zipFilePath)
+            if (ZipUtils.zipFiles(paths, zipFilePath)) {
+                val putUrl = "${rootWebDavUrl}${backupFileName}"
+                WebDav(putUrl, it).upload(zipFilePath)
             }
-        } catch (e: Exception) {
-            appCtx.toastOnUi("WebDav\n${e.localizedMessage}")
         }
     }
 

+ 1 - 6
app/src/main/java/io/legado/app/help/storage/Backup.kt

@@ -3,8 +3,6 @@ package io.legado.app.help.storage
 import android.content.Context
 import android.net.Uri
 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.data.appDb
 import io.legado.app.help.config.ReadBookConfig
@@ -56,9 +54,6 @@ object Backup {
                 } else {
                     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()
             }
-            AppWebDav.backUpWebDav(backupPath)
             when {
                 path.isNullOrBlank() -> {
                     copyBackup(context.getExternalFilesDir(null)!!, false)
@@ -119,6 +113,7 @@ object Backup {
                     copyBackup(File(path), isAuto)
                 }
             }
+            AppWebDav.backUpWebDav(backupPath)
         }
     }
 

+ 22 - 16
app/src/main/java/io/legado/app/lib/webdav/WebDav.kt

@@ -195,16 +195,17 @@ class WebDav(urlStr: String, val authorization: Authorization) {
     /**
      * 上传文件
      */
+    @Throws(Exception::class)
     suspend fun upload(
         localPath: String,
         contentType: String = "application/octet-stream"
-    ): Boolean {
-        val file = File(localPath)
-        if (!file.exists()) return false
-        // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
-        val fileBody = file.asRequestBody(contentType.toMediaType())
-        val url = httpUrl ?: return false
-        return kotlin.runCatching {
+    ) {
+        kotlin.runCatching {
+            val file = File(localPath)
+            if (!file.exists()) throw NoStackTraceException("文件不存在")
+            // 务必注意RequestBody不要嵌套,不然上传时内容可能会被追加多余的文件信息
+            val fileBody = file.asRequestBody(contentType.toMediaType())
+            val url = httpUrl ?: throw NoStackTraceException("url不能为空")
             okHttpClient.newCallResponse {
                 url(url)
                 put(fileBody)
@@ -213,15 +214,18 @@ class WebDav(urlStr: String, val authorization: Authorization) {
                 checkResult(it)
             }
         }.onFailure {
-            AppLog.put("WebDav上传失败\n${it.localizedMessage}")
-        }.isSuccess
+            val message = "WebDav上传失败\n${it.localizedMessage}"
+            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不要嵌套,不然上传时内容可能会被追加多余的文件信息
-        val fileBody = byteArray.toRequestBody(contentType.toMediaType())
-        val url = httpUrl ?: return false
-        return kotlin.runCatching {
+        kotlin.runCatching {
+            val fileBody = byteArray.toRequestBody(contentType.toMediaType())
+            val url = httpUrl ?: throw NoStackTraceException("url不能为空")
             okHttpClient.newCallResponse {
                 url(url)
                 put(fileBody)
@@ -230,8 +234,10 @@ class WebDav(urlStr: String, val authorization: Authorization) {
                 checkResult(it)
             }
         }.onFailure {
-            AppLog.put("WebDav上传失败\n${it.localizedMessage}")
-        }.isSuccess
+            val message = "WebDav上传失败\n${it.localizedMessage}"
+            AppLog.put(message)
+            throw NoStackTraceException(message)
+        }
     }
 
     private suspend fun getInputStream(): InputStream? {
@@ -246,7 +252,7 @@ class WebDav(urlStr: String, val authorization: Authorization) {
 
     private fun checkResult(response: Response) {
         if (!response.isSuccessful) {
-            throw NoStackTraceException(response.message)
+            throw NoStackTraceException("${url}\n${response.code}:${response.message}")
         }
     }