优化
This commit is contained in:
@@ -21,7 +21,6 @@ import me.ag2s.epublib.util.zip.AndroidZipFile
|
|||||||
import org.jsoup.Jsoup
|
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 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
|
||||||
@@ -95,29 +94,6 @@ class EpubFile(var book: Book) {
|
|||||||
return field
|
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,这样的好处是可以逐一修改某些文件的格式错误
|
* 重写epub文件解析代码,直接读出压缩包文件生成Resources给epublib,这样的好处是可以逐一修改某些文件的格式错误
|
||||||
*/
|
*/
|
||||||
@@ -267,11 +243,33 @@ class EpubFile(var book: Book) {
|
|||||||
return epubBook?.resources?.getByHref(abHref)?.inputStream
|
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() {
|
private fun upBookInfo() {
|
||||||
if (epubBook == null) {
|
if (epubBook == null) {
|
||||||
eFile = null
|
eFile = null
|
||||||
book.intro = "书籍导入异常"
|
book.intro = "书籍导入异常"
|
||||||
} else {
|
} else {
|
||||||
|
upBookCover()
|
||||||
val metadata = epubBook!!.metadata
|
val metadata = epubBook!!.metadata
|
||||||
book.name = metadata.firstTitle
|
book.name = metadata.firstTitle
|
||||||
if (book.name.isEmpty()) {
|
if (book.name.isEmpty()) {
|
||||||
|
|||||||
@@ -192,17 +192,25 @@ object LocalBook {
|
|||||||
latestChapterTime = updateTime,
|
latestChapterTime = updateTime,
|
||||||
order = appDb.bookDao.minOrder - 1
|
order = appDb.bookDao.minOrder - 1
|
||||||
)
|
)
|
||||||
if (book.isEpub) EpubFile.upBookInfo(book)
|
upBookInfo(book)
|
||||||
if (book.isUmd) UmdFile.upBookInfo(book)
|
|
||||||
if (book.isPdf) PdfFile.upBookInfo(book)
|
|
||||||
appDb.bookDao.insert(book)
|
appDb.bookDao.insert(book)
|
||||||
} else {
|
} else {
|
||||||
|
deleteBook(book, false)
|
||||||
|
upBookInfo(book)
|
||||||
//已有书籍说明是更新,删除原有目录
|
//已有书籍说明是更新,删除原有目录
|
||||||
appDb.bookChapterDao.delByBook(bookUrl)
|
appDb.bookChapterDao.delByBook(bookUrl)
|
||||||
}
|
}
|
||||||
return book
|
return book
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun upBookInfo(book: Book) {
|
||||||
|
when {
|
||||||
|
book.isEpub -> EpubFile.upBookInfo(book)
|
||||||
|
book.isUmd -> UmdFile.upBookInfo(book)
|
||||||
|
book.isPdf -> UmdFile.upBookInfo(book)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* 导入压缩包内的书籍 */
|
/* 导入压缩包内的书籍 */
|
||||||
fun importArchiveFile(
|
fun importArchiveFile(
|
||||||
archiveFileUri: Uri,
|
archiveFileUri: Uri,
|
||||||
@@ -306,6 +314,9 @@ object LocalBook {
|
|||||||
fun deleteBook(book: Book, deleteOriginal: Boolean) {
|
fun deleteBook(book: Book, deleteOriginal: Boolean) {
|
||||||
kotlin.runCatching {
|
kotlin.runCatching {
|
||||||
BookHelp.clearCache(book)
|
BookHelp.clearCache(book)
|
||||||
|
if (!book.coverUrl.isNullOrEmpty()) {
|
||||||
|
FileUtils.delete(book.coverUrl!!)
|
||||||
|
}
|
||||||
if (deleteOriginal) {
|
if (deleteOriginal) {
|
||||||
if (book.bookUrl.isContentScheme()) {
|
if (book.bookUrl.isContentScheme()) {
|
||||||
val uri = Uri.parse(book.bookUrl)
|
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.Book
|
||||||
import io.legado.app.data.entities.BookChapter
|
import io.legado.app.data.entities.BookChapter
|
||||||
import io.legado.app.help.book.getLocalUri
|
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 splitties.init.appCtx
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
@@ -70,28 +74,6 @@ class PdfFile(var book: Book) {
|
|||||||
return field
|
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文件
|
* 读取PDF文件
|
||||||
*
|
*
|
||||||
@@ -206,11 +188,29 @@ class PdfFile(var book: Book) {
|
|||||||
return chapterList
|
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() {
|
private fun upBookInfo() {
|
||||||
if (pdfRenderer == null) {
|
if (pdfRenderer == null) {
|
||||||
pFile = null
|
pFile = null
|
||||||
book.intro = "书籍导入异常"
|
book.intro = "书籍导入异常"
|
||||||
} else {
|
} else {
|
||||||
|
upBookCover()
|
||||||
if (book.name.isEmpty()) {
|
if (book.name.isEmpty()) {
|
||||||
book.name = book.originName.replace(".pdf", "")
|
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.Book
|
||||||
import io.legado.app.data.entities.BookChapter
|
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.domain.UmdBook
|
||||||
import me.ag2s.umdlib.umd.UmdReader
|
import me.ag2s.umdlib.umd.UmdReader
|
||||||
import java.io.File
|
|
||||||
import java.io.InputStream
|
import java.io.InputStream
|
||||||
|
|
||||||
class UmdFile(var book: Book) {
|
class UmdFile(var book: Book) {
|
||||||
@@ -57,32 +58,30 @@ class UmdFile(var book: Book) {
|
|||||||
return field
|
return field
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun readUmd(): UmdBook? {
|
||||||
|
val input = LocalBook.getBookInputStream(book)
|
||||||
|
return UmdReader().read(input)
|
||||||
|
}
|
||||||
|
|
||||||
init {
|
private fun upBookCover() {
|
||||||
try {
|
try {
|
||||||
umdBook?.let {
|
umdBook?.let {
|
||||||
if (book.coverUrl.isNullOrEmpty()) {
|
if (book.coverUrl.isNullOrEmpty()) {
|
||||||
book.coverUrl = LocalBook.getCoverPath(book)
|
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) {
|
} catch (e: Exception) {
|
||||||
e.printOnDebug()
|
e.printOnDebug()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun readUmd(): UmdBook? {
|
|
||||||
val input = LocalBook.getBookInputStream(book)
|
|
||||||
return UmdReader().read(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun upBookInfo() {
|
private fun upBookInfo() {
|
||||||
if (umdBook == null) {
|
if (umdBook == null) {
|
||||||
uFile = null
|
uFile = null
|
||||||
book.intro = "书籍导入异常"
|
book.intro = "书籍导入异常"
|
||||||
} else {
|
} else {
|
||||||
|
upBookCover()
|
||||||
val hd = umdBook!!.header
|
val hd = umdBook!!.header
|
||||||
book.name = hd.title
|
book.name = hd.title
|
||||||
book.author = hd.author
|
book.author = hd.author
|
||||||
|
|||||||
@@ -165,6 +165,8 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
|
|||||||
scope: CoroutineScope = viewModelScope
|
scope: CoroutineScope = viewModelScope
|
||||||
) {
|
) {
|
||||||
if (book.isLocal) {
|
if (book.isLocal) {
|
||||||
|
LocalBook.upBookInfo(book)
|
||||||
|
bookData.postValue(book)
|
||||||
loadChapter(book, scope)
|
loadChapter(book, scope)
|
||||||
} else {
|
} else {
|
||||||
val bookSource = bookSource ?: let {
|
val bookSource = bookSource ?: let {
|
||||||
|
|||||||
Reference in New Issue
Block a user