fix: 修复了本地 content:// 路径的书籍的上传
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,37 +15,7 @@ abstract class RemoteBookManager {
|
||||
protected val contentTypeList: ArrayList<String> = arrayListOf("epub","txt")
|
||||
abstract suspend fun initRemoteContext()
|
||||
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 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
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user