From f05350f4904f57b04215b236543e31acfce6a5b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E6=84=8F=E5=B8=86?= <1657098617@qq.com> Date: Tue, 17 May 2022 16:16:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BA=86=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=20content://=20=E8=B7=AF=E5=BE=84=E7=9A=84=E4=B9=A6?= =?UTF-8?q?=E7=B1=8D=E7=9A=84=E4=B8=8A=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/info/BookInfoActivity.kt | 3 +- .../app/ui/book/remote/RemoteBookManager.kt | 32 +------------------ .../book/remote/manager/RemoteBookWebDav.kt | 21 +++++++++--- 3 files changed, 18 insertions(+), 38 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index ce7562761..25a901c78 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -204,13 +204,12 @@ class BookInfoActivity : R.id.menu_upload -> { launch { val uri = Uri.parse(viewModel.bookData.value?.bookUrl.toString()) - RemoteBookWebDav.getFilePathFromContentUri(uri, contentResolver) // val doc = DocumentFile.fromTreeUri(appCtx,uri) // if (uri.isContentScheme()){ // uri.path // } -// RemoteBookWebDav.upload() + RemoteBookWebDav.upload(uri) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt index 2c4d428a5..363649c80 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt @@ -15,37 +15,7 @@ abstract class RemoteBookManager { protected val contentTypeList: ArrayList = arrayListOf("epub","txt") abstract suspend fun initRemoteContext() abstract suspend fun getRemoteBookList(): MutableList - abstract suspend fun upload(localBookUrl: String): Boolean + abstract suspend fun upload(localBookUri: Uri): Boolean abstract suspend fun delete(remoteBookUrl: String): Boolean abstract suspend fun getRemoteBook(remoteBookUrl: String): RemoteBook - - /** - * 把content uri转为 文件路径 - * - * @param contentUri 要转换的content uri - * @param contentResolver 解析器 - * @return - */ - - fun getFilePathFromContentUri( - contentUri: Uri, - contentResolver: ContentResolver - ): String? { - val filePath: String - if (contentUri.scheme == ContentResolver.SCHEME_FILE) - return File(requireNotNull(contentUri.path)).absolutePath - else if(contentUri.scheme == ContentResolver.SCHEME_CONTENT){ - val filePathColumn = arrayOf(MediaStore.MediaColumns.DATA) - val cursor: Cursor? = - contentResolver.query(contentUri, filePathColumn, null, null, null) - cursor!!.moveToFirst() - val columnIndex: Int = cursor.getColumnIndex(filePathColumn[0]) - filePath = cursor.getString(columnIndex) - cursor.close() - return filePath - } - return null - } - - } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt b/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt index 5bbe4e746..90e38914b 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt @@ -1,6 +1,7 @@ package io.legado.app.ui.book.remote.manager +import android.net.Uri import io.legado.app.constant.PreferKey import io.legado.app.exception.NoStackTraceException @@ -9,6 +10,7 @@ import io.legado.app.help.config.AppConfig import io.legado.app.lib.webdav.Authorization import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.WebDavFile +import io.legado.app.ui.book.info.BookInfoActivity import io.legado.app.ui.book.remote.RemoteBook import io.legado.app.ui.book.remote.RemoteBookManager @@ -98,12 +100,21 @@ object RemoteBookWebDav : RemoteBookManager() { /** * 上传本地导入的书籍到远程 */ - override suspend fun upload(localBookUrl: String): Boolean { + override suspend fun upload(localBookUri: Uri): Boolean { if (!NetworkUtils.isAvailable()) return false - val localBookName = localBookUrl.substringAfterLast(File.separator) - authorization?.let { - val putUrl = "${remoteBookUrl}${File.separator}${localBookName}" - WebDav(putUrl, it).upload(localBookUrl) + + val localBookName = localBookUri.path?.substringAfterLast(File.separator) + val putUrl = "${remoteBookUrl}${File.separator}${localBookName}" + kotlin.runCatching { + authorization?.let { + if (localBookUri.isContentScheme()){ + WebDav(putUrl, it).upload(byteArray = localBookUri.readBytes(appCtx),contentType = "application/octet-stream") + }else{ + WebDav(putUrl, it).upload(localBookUri.path!!) + } + } + }.onFailure { + return false } return true }