pull/2114/head
kunfei 2 years ago
parent 7222778aa3
commit 6e3bf6d736
  1. 25
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 24
      app/src/main/java/io/legado/app/model/localBook/EpubFile.kt

@ -1,5 +1,7 @@
package io.legado.app.help package io.legado.app.help
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import io.legado.app.constant.AppLog import io.legado.app.constant.AppLog
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.constant.EventBus import io.legado.app.constant.EventBus
@ -15,8 +17,11 @@ import kotlinx.coroutines.Dispatchers.IO
import org.apache.commons.text.similarity.JaccardSimilarity import org.apache.commons.text.similarity.JaccardSimilarity
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
import java.util.regex.Pattern import java.util.regex.Pattern
import java.util.zip.ZipFile
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@ -158,6 +163,26 @@ object BookHelp {
return suffix return suffix
} }
@Throws(IOException::class)
fun getEpubFile(book: Book): ZipFile {
val uri = Uri.parse(book.bookUrl)
if (uri.isContentScheme()) {
val path = FileUtils.getPath(downloadDir, cacheFolderName, book.originName)
val file = File(path)
val doc = DocumentFile.fromSingleUri(appCtx, uri)
?: throw IOException("文件不存在")
if (!file.exists() || doc.lastModified() > book.latestChapterTime) {
LocalBook.getBookInputStream(book).use { inputStream ->
FileOutputStream(file).use { outputStream ->
inputStream.copyTo(outputStream)
}
}
}
return ZipFile(file)
}
return ZipFile(uri.path)
}
fun getChapterFiles(book: Book): List<String> { fun getChapterFiles(book: Book): List<String> {
val fileNameList = arrayListOf<String>() val fileNameList = arrayListOf<String>()
if (book.isLocalTxt()) { if (book.isLocalTxt()) {

@ -2,10 +2,10 @@ package io.legado.app.model.localBook
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.net.Uri
import android.text.TextUtils import android.text.TextUtils
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.BookHelp
import io.legado.app.utils.* import io.legado.app.utils.*
import me.ag2s.epublib.domain.EpubBook import me.ag2s.epublib.domain.EpubBook
import me.ag2s.epublib.domain.Resource import me.ag2s.epublib.domain.Resource
@ -15,13 +15,11 @@ import org.jsoup.Jsoup
import org.jsoup.nodes.Element import org.jsoup.nodes.Element
import org.jsoup.select.Elements import org.jsoup.select.Elements
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.nio.charset.Charset import java.nio.charset.Charset
import java.util.zip.ZipFile
class EpubFile(var book: Book) { class EpubFile(var book: Book) {
@ -104,19 +102,13 @@ class EpubFile(var book: Book) {
* 重写epub文件解析代码直接读出压缩包文件生成Resources给epublib这样的好处是可以逐一修改某些文件的格式错误 * 重写epub文件解析代码直接读出压缩包文件生成Resources给epublib这样的好处是可以逐一修改某些文件的格式错误
*/ */
private fun readEpub(): EpubBook? { private fun readEpub(): EpubBook? {
try { return kotlin.runCatching {
val uri = Uri.parse(book.bookUrl) //ContentScheme拷贝到私有文件夹采用懒加载防止OOM
return if (uri.isContentScheme()) { val zipFile = BookHelp.getEpubFile(book)
//高版本的手机内存一般足够大,直接加载到内存中最快 EpubReader().readEpubLazy(zipFile, "utf-8")
EpubReader().readEpub(LocalBook.getBookInputStream(book), "utf-8") }.onFailure {
} else { it.printOnDebug()
//低版本的使用懒加载 }.getOrNull()
EpubReader().readEpubLazy(ZipFile(uri.path), "utf-8")
}
} catch (e: Exception) {
e.printOnDebug()
}
return null
} }
private fun getContent(chapter: BookChapter): String? { private fun getContent(chapter: BookChapter): String? {

Loading…
Cancel
Save