feat(book/remote): save remoteBookFile to defaultBookTreeUri

pull/1903/head
Xwite 3 years ago
parent 2649e1b6ba
commit 787393d83d
  1. 2
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookManager.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt
  4. 16
      app/src/main/java/io/legado/app/ui/book/remote/manager/RemoteBookWebDav.kt

@ -243,7 +243,7 @@ object LocalBook {
return type ?: fileType return type ?: fileType
} }
private fun saveBookFile( fun saveBookFile(
bytes: ByteArray, bytes: ByteArray,
fileName: String fileName: String
): Uri { ): Uri {

@ -12,5 +12,5 @@ abstract class RemoteBookManager {
/** /**
* @return String下载到本地的路径 * @return String下载到本地的路径
*/ */
abstract suspend fun getRemoteBook(remoteBook: RemoteBook): String? abstract suspend fun getRemoteBook(remoteBook: RemoteBook): Uri?
} }

@ -73,7 +73,7 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){
execute { execute {
val downloadBookPath = RemoteBookWebDav.getRemoteBook(remoteBook) val downloadBookPath = RemoteBookWebDav.getRemoteBook(remoteBook)
downloadBookPath?.let { downloadBookPath?.let {
LocalBook.importFile(Uri.parse(it)) LocalBook.importFile(it)
} }
}.onFinally { }.onFinally {
finally.invoke() finally.invoke()

@ -9,6 +9,7 @@ import io.legado.app.help.AppWebDav
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
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.model.localBook.LocalBook
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
import io.legado.app.utils.* import io.legado.app.utils.*
@ -18,7 +19,6 @@ import java.io.File
object RemoteBookWebDav : RemoteBookManager() { object RemoteBookWebDav : RemoteBookManager() {
private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}" private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}"
private val localSaveFolder get() = "${appCtx.externalFiles.absolutePath}${File.separator}${remoteBookFolder}"
init { init {
runBlocking { runBlocking {
@ -83,23 +83,21 @@ object RemoteBookWebDav : RemoteBookManager() {
return remoteBooks return remoteBooks
} }
override suspend fun getRemoteBook(remoteBook: RemoteBook): String? { override suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? {
val saveFilePath= "${localSaveFolder}${File.separator}${remoteBook.filename}" return kotlin.runCatching {
kotlin.runCatching {
AppWebDav.authorization?.let { AppWebDav.authorization?.let {
FileUtils.createFolderIfNotExist(localSaveFolder).run {
val webdav = WebDav( val webdav = WebDav(
remoteBook.urlName, remoteBook.urlName,
it it
) )
webdav.downloadTo(saveFilePath, true) webdav.download().let { bytes ->
LocalBook.saveBookFile(bytes, remoteBook.filename)
} }
} }
}.onFailure { }.onFailure {
it.printStackTrace() it.printStackTrace()
return null null
} }.getOrNull()
return saveFilePath
} }
/** /**

Loading…
Cancel
Save