Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Dec 17, 2024
1 parent 7db656a commit 5f392d4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
10 changes: 9 additions & 1 deletion app/src/main/java/io/legado/app/model/localBook/EpubFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.jsoup.Jsoup
import org.jsoup.nodes.Element
import org.jsoup.parser.Parser
import org.jsoup.select.Elements
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
Expand Down Expand Up @@ -95,6 +96,10 @@ class EpubFile(var book: Book) {
return field
}

init {
upBookCover(true)
}

/**
* 重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误
*/
Expand Down Expand Up @@ -249,12 +254,15 @@ class EpubFile(var book: Book) {
return epubBook?.resources?.getByHref(abHref)?.inputStream
}

private fun upBookCover() {
private fun upBookCover(fastCheck: Boolean = false) {
try {
epubBook?.let {
if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = LocalBook.getCoverPath(book)
}
if (fastCheck && File(book.coverUrl!!).exists()) {
return
}
/*部分书籍DRM处理后,封面获取异常,待优化*/
it.coverImage?.inputStream?.use { input ->
val cover = BitmapFactory.decodeStream(input)
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/io/legado/app/model/localBook/MobiFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.legado.app.utils.FileUtils
import io.legado.app.utils.HtmlFormatter
import io.legado.app.utils.printOnDebug
import org.jsoup.Jsoup
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream

Expand Down Expand Up @@ -70,6 +71,10 @@ class MobiFile(var book: Book) {
return field
}

init {
upBookCover(true)
}

private fun readMobi(): MobiBook? {
return kotlin.runCatching {
BookHelp.getBookPFD(book)?.let {
Expand Down Expand Up @@ -266,12 +271,15 @@ class MobiFile(var book: Book) {
return kf8Book.getResourceByHref(href)?.inputStream()
}

private fun upBookCover() {
private fun upBookCover(fastCheck: Boolean = false) {
try {
mobiBook?.let {
if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = LocalBook.getCoverPath(book)
}
if (fastCheck && File(book.coverUrl!!).exists()) {
return
}
it.getCover()?.let { bytes ->
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
val file = FileUtils.createFileIfNotExist(book.coverUrl!!)
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/io/legado/app/model/localBook/PdfFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class PdfFile(var book: Book) {
return field
}

init {
upBookCover(true)
}

/**
* 读取PDF文件
*
Expand Down Expand Up @@ -188,12 +192,15 @@ class PdfFile(var book: Book) {
return chapterList
}

private fun upBookCover() {
private fun upBookCover(fastCheck: Boolean = false) {
try {
pdfRenderer?.let { renderer ->
if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = LocalBook.getCoverPath(book)
}
if (fastCheck && File(book.coverUrl!!).exists()) {
return
}
FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!)).use { out ->
openPdfPage(renderer, 0)?.compress(Bitmap.CompressFormat.JPEG, 90, out)
out.flush()
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/io/legado/app/model/localBook/UmdFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.legado.app.utils.FileUtils
import io.legado.app.utils.printOnDebug
import me.ag2s.umdlib.domain.UmdBook
import me.ag2s.umdlib.umd.UmdReader
import java.io.File
import java.io.InputStream

class UmdFile(var book: Book) {
Expand Down Expand Up @@ -58,17 +59,24 @@ class UmdFile(var book: Book) {
return field
}

init {
upBookCover(true)
}

private fun readUmd(): UmdBook? {
val input = LocalBook.getBookInputStream(book)
return UmdReader().read(input)
}

private fun upBookCover() {
private fun upBookCover(fastCheck: Boolean = false) {
try {
umdBook?.let {
if (book.coverUrl.isNullOrEmpty()) {
book.coverUrl = LocalBook.getCoverPath(book)
}
if (fastCheck && File(book.coverUrl!!).exists()) {
return
}
FileUtils.writeBytes(book.coverUrl!!, it.cover.coverData)
}
} catch (e: Exception) {
Expand Down

0 comments on commit 5f392d4

Please sign in to comment.