From 2649e1b6ba0dfa844cc3369c9b27904ea2bbd691 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Tue, 24 May 2022 13:58:47 +0800 Subject: [PATCH 1/2] feat(book/remote): implements isOnBookShelf and use regex to judge booktype --- .../legado/app/model/localBook/LocalBook.kt | 22 +++++++++++++++++++ .../legado/app/ui/book/remote/RemoteBook.kt | 3 ++- .../app/ui/book/remote/RemoteBookManager.kt | 3 --- .../book/remote/manager/RemoteBookWebDav.kt | 11 ++++++++-- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index a59b2db5d..3953dae5b 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -219,6 +219,8 @@ object LocalBook { fileName: String, source: BaseSource? = null, ): Uri { + AppConfig.defaultBookTreeUri + ?: throw NoStackTraceException("没有设置书籍保存位置!") val bytes = when { str.isAbsUrl() -> AnalyzeUrl(str, source = source).getByteArray() str.isDataUrl() -> Base64.decode(str.substringAfter("base64,"), Base64.DEFAULT) @@ -269,6 +271,26 @@ object LocalBook { } } + fun isOnBookShelf( + fileName: String + ): Boolean { + val defaultBookTreeUri = AppConfig.defaultBookTreeUri + if (defaultBookTreeUri.isNullOrBlank()) throw NoStackTraceException("没有设置书籍保存位置!") + val treeUri = Uri.parse(defaultBookTreeUri) + var bookUrl: String = "" + if (treeUri.isContentScheme()) { + val treeDoc = DocumentFile.fromTreeUri(appCtx, treeUri) + var doc = treeDoc!!.findFile(fileName) ?: return false + bookUrl = doc.uri.toString() + } else { + val treeFile = File(treeUri.path!!) + val file = treeFile.getFile(fileName) + if (!file.exists()) return false + bookUrl = file.absolutePath + } + return appDb.bookDao.getBook(bookUrl) != null + } + //文件类书源 合并在线书籍信息 在线 > 本地 fun mergeBook(localBook: Book, onLineBook: Book?): Book { onLineBook ?: return localBook diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBook.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBook.kt index c04098421..b0a45c332 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBook.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBook.kt @@ -5,5 +5,6 @@ data class RemoteBook( val urlName: String, val size: Long, val contentType: String, - val lastModify: Long + val lastModify: Long, + val isOnBookShelf: Boolean ) \ No newline at end of file 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 9fd1d1faf..84ce53428 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 @@ -2,11 +2,8 @@ package io.legado.app.ui.book.remote import android.net.Uri - - abstract class RemoteBookManager { protected val remoteBookFolder : String = "books" - protected val contentTypeList: ArrayList = arrayListOf("epub","txt") abstract suspend fun initRemoteContext() abstract suspend fun getRemoteBookList(): MutableList abstract suspend fun upload(localBookUri: Uri): Boolean 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 995463f58..77a45ad31 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 @@ -3,6 +3,7 @@ package io.legado.app.ui.book.remote.manager import android.net.Uri import io.legado.app.constant.PreferKey +import io.legado.app.constant.AppPattern.bookFileRegex import io.legado.app.exception.NoStackTraceException import io.legado.app.help.AppWebDav import io.legado.app.help.config.AppConfig @@ -68,8 +69,14 @@ object RemoteBookWebDav : RemoteBookManager() { val fileExtension = webDavFileName.substringAfterLast(".") //扩展名符合阅读的格式则认为是书籍 - if (contentTypeList.contains(fileExtension)) { - remoteBooks.add(RemoteBook(webDavFileName,webDavUrlName,webDavFile.size,fileExtension,webDavFile.lastModify)) + if (bookFileRegex.matches(webDavFileName)) { + val isOnBookShelf = LocalBook.isOnBookShelf(webDavFileName) + remoteBooks.add( + RemoteBook( + webDavFileName, webDavUrlName, webDavFile.size, + fileExtension, webDavFile.lastModify, isOnBookShelf + ) + ) } } } ?: throw NoStackTraceException("webDav没有配置") From 787393d83de692a3f5abf365526613982ac5e8e2 Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Tue, 24 May 2022 11:12:51 +0800 Subject: [PATCH 2/2] feat(book/remote): save remoteBookFile to defaultBookTreeUri --- .../legado/app/model/localBook/LocalBook.kt | 2 +- .../app/ui/book/remote/RemoteBookManager.kt | 2 +- .../app/ui/book/remote/RemoteBookViewModel.kt | 2 +- .../book/remote/manager/RemoteBookWebDav.kt | 24 +++++++++---------- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 3953dae5b..75ff48199 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -243,7 +243,7 @@ object LocalBook { return type ?: fileType } - private fun saveBookFile( + fun saveBookFile( bytes: ByteArray, fileName: String ): 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 84ce53428..132c6f354 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 @@ -12,5 +12,5 @@ abstract class RemoteBookManager { /** * @return String:下载到本地的路径 */ - abstract suspend fun getRemoteBook(remoteBook: RemoteBook): String? + abstract suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt index 87a7eff57..09ecfb18a 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookViewModel.kt @@ -73,7 +73,7 @@ class RemoteBookViewModel(application: Application): BaseViewModel(application){ execute { val downloadBookPath = RemoteBookWebDav.getRemoteBook(remoteBook) downloadBookPath?.let { - LocalBook.importFile(Uri.parse(it)) + LocalBook.importFile(it) } }.onFinally { finally.invoke() 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 77a45ad31..fc1293ce7 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 @@ -9,6 +9,7 @@ import io.legado.app.help.AppWebDav import io.legado.app.help.config.AppConfig import io.legado.app.lib.webdav.WebDav 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.RemoteBookManager import io.legado.app.utils.* @@ -18,7 +19,6 @@ import java.io.File object RemoteBookWebDav : RemoteBookManager() { private val remoteBookUrl get() = "${rootWebDavUrl}${remoteBookFolder}" - private val localSaveFolder get() = "${appCtx.externalFiles.absolutePath}${File.separator}${remoteBookFolder}" init { runBlocking { @@ -83,23 +83,21 @@ object RemoteBookWebDav : RemoteBookManager() { return remoteBooks } - override suspend fun getRemoteBook(remoteBook: RemoteBook): String? { - val saveFilePath= "${localSaveFolder}${File.separator}${remoteBook.filename}" - kotlin.runCatching { + override suspend fun getRemoteBook(remoteBook: RemoteBook): Uri? { + return kotlin.runCatching { AppWebDav.authorization?.let { - FileUtils.createFolderIfNotExist(localSaveFolder).run { - val webdav = WebDav( - remoteBook.urlName, - it - ) - webdav.downloadTo(saveFilePath, true) + val webdav = WebDav( + remoteBook.urlName, + it + ) + webdav.download().let { bytes -> + LocalBook.saveBookFile(bytes, remoteBook.filename) } } }.onFailure { it.printStackTrace() - return null - } - return saveFilePath + null + }.getOrNull() } /**