From 4cc2a73a58cc0e9b1294938fbac759c6f1a83c78 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 5 Oct 2022 20:53: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/help/book/BookExtensions.kt | 14 ++++++++++++++ .../io/legado/app/model/localBook/LocalBook.kt | 11 ++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt index 2411fcb03..5f0bccd23 100644 --- a/app/src/main/java/io/legado/app/help/book/BookExtensions.kt +++ b/app/src/main/java/io/legado/app/help/book/BookExtensions.kt @@ -1,9 +1,13 @@ package io.legado.app.help.book +import android.net.Uri import io.legado.app.constant.BookSourceType import io.legado.app.constant.BookType import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookSource +import io.legado.app.exception.NoStackTraceException +import io.legado.app.utils.isContentScheme +import java.io.File val Book.isAudio: Boolean @@ -44,6 +48,16 @@ val Book.isOnLineTxt: Boolean return !isLocal && type and BookType.text > 0 } +fun Book.getLocalUri(): Uri { + if (isLocal) { + if (bookUrl.isContentScheme()) { + return Uri.parse(bookUrl) + } + return Uri.fromFile(File(bookUrl)) + } + throw NoStackTraceException("不是本地书籍") +} + fun Book.getRemoteUrl(): String? { if (origin.startsWith(BookType.webDavTag)) { return origin.substring(8) 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 bbf116e9d..c5c05d3ec 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 @@ -15,10 +15,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.TocEmptyException import io.legado.app.help.AppWebDav -import io.legado.app.help.book.BookHelp -import io.legado.app.help.book.getRemoteUrl -import io.legado.app.help.book.isEpub -import io.legado.app.help.book.isUmd +import io.legado.app.help.book.* import io.legado.app.help.config.AppConfig import io.legado.app.lib.webdav.WebDav import io.legado.app.model.analyzeRule.AnalyzeUrl @@ -44,11 +41,7 @@ object LocalBook { @Throws(FileNotFoundException::class, SecurityException::class) fun getBookInputStream(book: Book): InputStream { - val uri = if (book.bookUrl.isContentScheme()) { - Uri.parse(book.bookUrl) - } else { - Uri.fromFile(File(book.bookUrl)) - } + val uri = book.getLocalUri() //文件不存在 尝试下载webDav文件 book.remoteUrl val inputStream = uri.inputStream(appCtx) ?: downloadRemoteBook(book) if (inputStream != null) return inputStream