mirror of
https://github.com/gedoor/legado.git
synced 2024-07-17 00:58:29 +08:00
优化
This commit is contained in:
parent
ccd4726ae4
commit
1824d24267
@ -21,7 +21,6 @@ import me.ag2s.epublib.util.zip.AndroidZipFile
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Element
|
||||
import org.jsoup.select.Elements
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
@ -95,29 +94,6 @@ class EpubFile(var book: Book) {
|
||||
return field
|
||||
}
|
||||
|
||||
init {
|
||||
try {
|
||||
epubBook?.let {
|
||||
if (book.coverUrl.isNullOrEmpty()) {
|
||||
book.coverUrl = LocalBook.getCoverPath(book)
|
||||
}
|
||||
if (!File(book.coverUrl!!).exists()) {
|
||||
/*部分书籍DRM处理后,封面获取异常,待优化*/
|
||||
it.coverImage?.inputStream?.use { input ->
|
||||
val cover = BitmapFactory.decodeStream(input)
|
||||
val out = FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
|
||||
cover.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
||||
out.flush()
|
||||
out.close()
|
||||
} ?: AppLog.putDebug("Epub: 封面获取为空. path: ${book.bookUrl}")
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("加载书籍封面失败\n${e.localizedMessage}", e)
|
||||
e.printOnDebug()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误
|
||||
*/
|
||||
@ -267,11 +243,33 @@ class EpubFile(var book: Book) {
|
||||
return epubBook?.resources?.getByHref(abHref)?.inputStream
|
||||
}
|
||||
|
||||
private fun upBookCover() {
|
||||
try {
|
||||
epubBook?.let {
|
||||
if (book.coverUrl.isNullOrEmpty()) {
|
||||
book.coverUrl = LocalBook.getCoverPath(book)
|
||||
}
|
||||
/*部分书籍DRM处理后,封面获取异常,待优化*/
|
||||
it.coverImage?.inputStream?.use { input ->
|
||||
val cover = BitmapFactory.decodeStream(input)
|
||||
val out = FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!))
|
||||
cover.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
||||
out.flush()
|
||||
out.close()
|
||||
} ?: AppLog.putDebug("Epub: 封面获取为空. path: ${book.bookUrl}")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("加载书籍封面失败\n${e.localizedMessage}", e)
|
||||
e.printOnDebug()
|
||||
}
|
||||
}
|
||||
|
||||
private fun upBookInfo() {
|
||||
if (epubBook == null) {
|
||||
eFile = null
|
||||
book.intro = "书籍导入异常"
|
||||
} else {
|
||||
upBookCover()
|
||||
val metadata = epubBook!!.metadata
|
||||
book.name = metadata.firstTitle
|
||||
if (book.name.isEmpty()) {
|
||||
|
@ -192,17 +192,25 @@ object LocalBook {
|
||||
latestChapterTime = updateTime,
|
||||
order = appDb.bookDao.minOrder - 1
|
||||
)
|
||||
if (book.isEpub) EpubFile.upBookInfo(book)
|
||||
if (book.isUmd) UmdFile.upBookInfo(book)
|
||||
if (book.isPdf) PdfFile.upBookInfo(book)
|
||||
upBookInfo(book)
|
||||
appDb.bookDao.insert(book)
|
||||
} else {
|
||||
deleteBook(book, false)
|
||||
upBookInfo(book)
|
||||
//已有书籍说明是更新,删除原有目录
|
||||
appDb.bookChapterDao.delByBook(bookUrl)
|
||||
}
|
||||
return book
|
||||
}
|
||||
|
||||
fun upBookInfo(book: Book) {
|
||||
when {
|
||||
book.isEpub -> EpubFile.upBookInfo(book)
|
||||
book.isUmd -> UmdFile.upBookInfo(book)
|
||||
book.isPdf -> UmdFile.upBookInfo(book)
|
||||
}
|
||||
}
|
||||
|
||||
/* 导入压缩包内的书籍 */
|
||||
fun importArchiveFile(
|
||||
archiveFileUri: Uri,
|
||||
@ -306,6 +314,9 @@ object LocalBook {
|
||||
fun deleteBook(book: Book, deleteOriginal: Boolean) {
|
||||
kotlin.runCatching {
|
||||
BookHelp.clearCache(book)
|
||||
if (!book.coverUrl.isNullOrEmpty()) {
|
||||
FileUtils.delete(book.coverUrl!!)
|
||||
}
|
||||
if (deleteOriginal) {
|
||||
if (book.bookUrl.isContentScheme()) {
|
||||
val uri = Uri.parse(book.bookUrl)
|
||||
|
@ -8,7 +8,11 @@ import io.legado.app.constant.AppLog
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.BookChapter
|
||||
import io.legado.app.help.book.getLocalUri
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.BitmapUtils
|
||||
import io.legado.app.utils.FileUtils
|
||||
import io.legado.app.utils.SystemUtils
|
||||
import io.legado.app.utils.isContentScheme
|
||||
import io.legado.app.utils.printOnDebug
|
||||
import splitties.init.appCtx
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
@ -70,28 +74,6 @@ class PdfFile(var book: Book) {
|
||||
return field
|
||||
}
|
||||
|
||||
|
||||
init {
|
||||
try {
|
||||
pdfRenderer?.let { renderer ->
|
||||
if (book.coverUrl.isNullOrEmpty()) {
|
||||
book.coverUrl = LocalBook.getCoverPath(book)
|
||||
}
|
||||
if (!File(book.coverUrl!!).exists()) {
|
||||
|
||||
FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!)).use { out ->
|
||||
openPdfPage(renderer, 0)?.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
||||
out.flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("加载书籍封面失败\n${e.localizedMessage}", e)
|
||||
e.printOnDebug()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取PDF文件
|
||||
*
|
||||
@ -206,11 +188,29 @@ class PdfFile(var book: Book) {
|
||||
return chapterList
|
||||
}
|
||||
|
||||
private fun upBookCover() {
|
||||
try {
|
||||
pdfRenderer?.let { renderer ->
|
||||
if (book.coverUrl.isNullOrEmpty()) {
|
||||
book.coverUrl = LocalBook.getCoverPath(book)
|
||||
}
|
||||
FileOutputStream(FileUtils.createFileIfNotExist(book.coverUrl!!)).use { out ->
|
||||
openPdfPage(renderer, 0)?.compress(Bitmap.CompressFormat.JPEG, 90, out)
|
||||
out.flush()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("加载书籍封面失败\n${e.localizedMessage}", e)
|
||||
e.printOnDebug()
|
||||
}
|
||||
}
|
||||
|
||||
private fun upBookInfo() {
|
||||
if (pdfRenderer == null) {
|
||||
pFile = null
|
||||
book.intro = "书籍导入异常"
|
||||
} else {
|
||||
upBookCover()
|
||||
if (book.name.isEmpty()) {
|
||||
book.name = book.originName.replace(".pdf", "")
|
||||
}
|
||||
|
@ -2,10 +2,11 @@ package io.legado.app.model.localBook
|
||||
|
||||
import io.legado.app.data.entities.Book
|
||||
import io.legado.app.data.entities.BookChapter
|
||||
import io.legado.app.utils.*
|
||||
import io.legado.app.utils.DebugLog
|
||||
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) {
|
||||
@ -57,32 +58,30 @@ class UmdFile(var book: Book) {
|
||||
return field
|
||||
}
|
||||
|
||||
private fun readUmd(): UmdBook? {
|
||||
val input = LocalBook.getBookInputStream(book)
|
||||
return UmdReader().read(input)
|
||||
}
|
||||
|
||||
init {
|
||||
private fun upBookCover() {
|
||||
try {
|
||||
umdBook?.let {
|
||||
if (book.coverUrl.isNullOrEmpty()) {
|
||||
book.coverUrl = LocalBook.getCoverPath(book)
|
||||
}
|
||||
if (!File(book.coverUrl!!).exists()) {
|
||||
FileUtils.writeBytes(book.coverUrl!!, it.cover.coverData)
|
||||
}
|
||||
FileUtils.writeBytes(book.coverUrl!!, it.cover.coverData)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printOnDebug()
|
||||
}
|
||||
}
|
||||
|
||||
private fun readUmd(): UmdBook? {
|
||||
val input = LocalBook.getBookInputStream(book)
|
||||
return UmdReader().read(input)
|
||||
}
|
||||
|
||||
private fun upBookInfo() {
|
||||
if (umdBook == null) {
|
||||
uFile = null
|
||||
book.intro = "书籍导入异常"
|
||||
} else {
|
||||
upBookCover()
|
||||
val hd = umdBook!!.header
|
||||
book.name = hd.title
|
||||
book.author = hd.author
|
||||
|
@ -165,6 +165,8 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
||||
scope: CoroutineScope = viewModelScope
|
||||
) {
|
||||
if (book.isLocal) {
|
||||
LocalBook.upBookInfo(book)
|
||||
bookData.postValue(book)
|
||||
loadChapter(book, scope)
|
||||
} else {
|
||||
val bookSource = bookSource ?: let {
|
||||
|
Loading…
Reference in New Issue
Block a user