From 61aee76c53cca02d5d7ab2292e056f7760abf820 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 20 Jun 2022 21:45:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/lib/webdav/WebDav.kt | 34 ++++++++++--------- .../main/java/io/legado/app/model/ReadBook.kt | 3 +- .../legado/app/ui/book/remote/RemoteBook.kt | 8 ++++- .../app/ui/book/remote/RemoteBookAdapter.kt | 13 +++++-- .../book/remote/manager/RemoteBookWebDav.kt | 25 +++++++++----- 5 files changed, 54 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index fcd5ac6e4..cf24d7e99 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -6,6 +6,7 @@ import io.legado.app.exception.NoStackTraceException import io.legado.app.help.http.newCallResponse import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.text +import io.legado.app.utils.NetworkUtils import io.legado.app.utils.printOnDebug import okhttp3.MediaType.Companion.toMediaType import okhttp3.RequestBody.Companion.asRequestBody @@ -61,11 +62,14 @@ open class WebDav(urlStr: String, val authorization: Authorization) { /** - * 填充文件信息。实例化WebDAVFile对象时,并没有将远程文件的信息填充到实例中。需要手动填充! - * @return 远程文件是否存在 + * 获取当前url文件信息 */ - suspend fun indexFileInfo(): Boolean { - return !propFindResponse(ArrayList()).isNullOrEmpty() + suspend fun getWebDavFile(): WebDavFile? { + return kotlin.runCatching { + propFindResponse(depth = 0)?.let { + return parseBody(it).firstOrNull() + } + }.getOrNull() } /** @@ -76,9 +80,11 @@ open class WebDav(urlStr: String, val authorization: Authorization) { @Throws(WebDavException::class) suspend fun listFiles(): List { propFindResponse()?.let { body -> - return parseDir(body) + return parseBody(body).filter { + it.path != path + } } - return ArrayList() + return emptyList() } /** @@ -112,12 +118,12 @@ open class WebDav(urlStr: String, val authorization: Authorization) { }.body?.text() } - private fun parseDir(s: String): List { + private fun parseBody(s: String): List { val list = ArrayList() val document = Jsoup.parse(s) val elements = document.getElementsByTag("d:response") httpUrl?.let { urlStr -> - val baseUrl = if (urlStr.endsWith("/")) urlStr else "$urlStr/" + val baseUrl = NetworkUtils.getBaseUrl(urlStr) for (element in elements) { val href = element.getElementsByTag("d:href")[0].text() if (!href.endsWith("/")) { @@ -137,11 +143,12 @@ open class WebDav(urlStr: String, val authorization: Authorization) { val lastModify: Long = kotlin.runCatching { element.getElementsByTag("d:getlastmodified") .firstOrNull()?.text()?.let { - LocalDateTime.parse(it, dateTimeFormatter).toInstant(ZoneOffset.of("+8")).toEpochMilli() + LocalDateTime.parse(it, dateTimeFormatter) + .toInstant(ZoneOffset.of("+8")).toEpochMilli() } }.getOrNull() ?: 0 webDavFile = WebDavFile( - baseUrl + fileName, + "$baseUrl$href", authorization, displayName = fileName, urlName = urlName, @@ -163,12 +170,7 @@ open class WebDav(urlStr: String, val authorization: Authorization) { * 文件是否存在 */ suspend fun exists(): Boolean { - return kotlin.runCatching { - val response = propFindResponse(depth = 0) ?: return false - val document = Jsoup.parse(response) - val elements = document.getElementsByTag("d:response") - return elements.isNotEmpty() - }.getOrDefault(false) + return getWebDavFile() != null } /** diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index 614d71fc5..db820a572 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -21,6 +21,7 @@ import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.MainScope import kotlinx.coroutines.delay import splitties.init.appCtx +import kotlin.math.min @Suppress("MemberVisibilityCanBePrivate") @@ -437,7 +438,7 @@ object ReadBook : CoroutineScope by MainScope() { delay(1000) download(i) } - val minChapterIndex = durChapterIndex - 5 + val minChapterIndex = durChapterIndex - min(5, AppConfig.preDownloadNum) for (i in durChapterIndex.minus(2) downTo minChapterIndex) { delay(1000) download(i) 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 b0a45c332..65de6ac2d 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 @@ -7,4 +7,10 @@ data class RemoteBook( val contentType: String, val lastModify: Long, val isOnBookShelf: Boolean -) \ No newline at end of file +) { + + val isDir by lazy { + contentType == "folder" + } + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt index c02094d0e..b31f56bec 100644 --- a/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/remote/RemoteBookAdapter.kt @@ -2,6 +2,7 @@ package io.legado.app.ui.book.remote import android.content.Context import android.view.ViewGroup +import androidx.core.view.isGone import cn.hutool.core.date.LocalDateTimeUtil import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.RecyclerAdapter @@ -37,7 +38,11 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : tvName.text = item.filename.substringBeforeLast(".") tvContentType.text = item.contentType tvSize.text = ConvertUtils.formatFileSize(item.size) - tvDate.text = LocalDateTimeUtil.format(LocalDateTimeUtil.of(item.lastModify), "yyyy-MM-dd") + tvDate.text = + LocalDateTimeUtil.format(LocalDateTimeUtil.of(item.lastModify), "yyyy-MM-dd") + llInfo.isGone = item.isDir + tvContentType.isGone = item.isDir + btnDownload.isGone = item.isDir } } @@ -45,7 +50,11 @@ class RemoteBookAdapter (context: Context, val callBack: CallBack) : binding.btnDownload.setOnClickListener { getItem(holder.layoutPosition)?.let { - callBack.addToBookshelf(it) + if (it.isDir) { + + } else { + callBack.addToBookshelf(it) + } } } 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 70378138e..0a512b76f 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 @@ -49,20 +49,27 @@ object RemoteBookWebDav : RemoteBookManager() { webDavFileName = URLDecoder.decode(webDavFileName, "utf-8") webDavUrlName = URLDecoder.decode(webDavUrlName, "utf-8") - webDavFile.isDir - - //分割后缀 - val fileExtension = webDavFileName.substringAfterLast(".") - - //扩展名符合阅读的格式则认为是书籍 - if (bookFileRegex.matches(webDavFileName)) { - val isOnBookShelf = LocalBook.isOnBookShelf(webDavFileName) + if (webDavFile.isDir) { remoteBooks.add( RemoteBook( webDavFileName, webDavUrlName, webDavFile.size, - fileExtension, webDavFile.lastModify, isOnBookShelf + "folder", webDavFile.lastModify, false ) ) + } else { + //分割后缀 + val fileExtension = webDavFileName.substringAfterLast(".") + + //扩展名符合阅读的格式则认为是书籍 + if (bookFileRegex.matches(webDavFileName)) { + val isOnBookShelf = LocalBook.isOnBookShelf(webDavFileName) + remoteBooks.add( + RemoteBook( + webDavFileName, webDavUrlName, webDavFile.size, + fileExtension, webDavFile.lastModify, isOnBookShelf + ) + ) + } } } } ?: throw NoStackTraceException("webDav没有配置")