fix: 修复了本地 content:// 路径的书籍的上传

pull/1894/head
谢意帆 3 years ago
parent 05951a98ea
commit f05350f490
  1. 3
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  2. 32
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt
  3. 19
      app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt

@ -204,13 +204,12 @@ class BookInfoActivity :
R.id.menu_upload -> { R.id.menu_upload -> {
launch { launch {
val uri = Uri.parse(viewModel.bookData.value?.bookUrl.toString()) val uri = Uri.parse(viewModel.bookData.value?.bookUrl.toString())
RemoteBookWebDav.getFilePathFromContentUri(uri, contentResolver)
// val doc = DocumentFile.fromTreeUri(appCtx,uri) // val doc = DocumentFile.fromTreeUri(appCtx,uri)
// if (uri.isContentScheme()){ // if (uri.isContentScheme()){
// uri.path // uri.path
// } // }
// RemoteBookWebDav.upload() RemoteBookWebDav.upload(uri)
} }
} }
} }

@ -15,37 +15,7 @@ abstract class RemoteBookManager {
protected val contentTypeList: ArrayList<String> = arrayListOf("epub","txt") protected val contentTypeList: ArrayList<String> = arrayListOf("epub","txt")
abstract suspend fun initRemoteContext() abstract suspend fun initRemoteContext()
abstract suspend fun getRemoteBookList(): MutableList<RemoteBook> abstract suspend fun getRemoteBookList(): MutableList<RemoteBook>
abstract suspend fun upload(localBookUrl: String): Boolean abstract suspend fun upload(localBookUri: Uri): Boolean
abstract suspend fun delete(remoteBookUrl: String): Boolean abstract suspend fun delete(remoteBookUrl: String): Boolean
abstract suspend fun getRemoteBook(remoteBookUrl: String): RemoteBook 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
}
} }

@ -1,6 +1,7 @@
package io.legado.app.ui.book.remote.manager package io.legado.app.ui.book.remote.manager
import android.net.Uri
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.exception.NoStackTraceException 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.Authorization
import io.legado.app.lib.webdav.WebDav import io.legado.app.lib.webdav.WebDav
import io.legado.app.lib.webdav.WebDavFile 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.RemoteBook
import io.legado.app.ui.book.remote.RemoteBookManager 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 if (!NetworkUtils.isAvailable()) return false
val localBookName = localBookUrl.substringAfterLast(File.separator)
authorization?.let { val localBookName = localBookUri.path?.substringAfterLast(File.separator)
val putUrl = "${remoteBookUrl}${File.separator}${localBookName}" val putUrl = "${remoteBookUrl}${File.separator}${localBookName}"
WebDav(putUrl, it).upload(localBookUrl) 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 return true
} }

Loading…
Cancel
Save