优化epub解析

pull/883/head
gedoor 4 years ago
parent 62fa68cfb3
commit 4102b12efd
  1. 18
      app/src/main/java/io/legado/app/model/localBook/EPUBFile.kt
  2. 1
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@ -59,8 +59,8 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
} }
@Synchronized @Synchronized
fun getBookInfo(book: io.legado.app.data.entities.Book) { fun upBookInfo(book: io.legado.app.data.entities.Book) {
return getEFile(book).getBookInfo() return getEFile(book).upBookInfo()
} }
} }
@ -111,7 +111,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
zipEntry = inZip.nextEntry zipEntry = inZip.nextEntry
if ((zipEntry == null) || zipEntry.isDirectory || zipEntry == ZipEntry("<error>")) continue if ((zipEntry == null) || zipEntry.isDirectory || zipEntry == ZipEntry("<error>")) continue
val resource = ResourceUtil.createResource(zipEntry, inZip) val resource = ResourceUtil.createResource(zipEntry, inZip)
if (resource.mediaType == MediatypeService.XHTML) resource.inputEncoding = "UTF-8"; if (resource.mediaType == MediatypeService.XHTML) resource.inputEncoding = "UTF-8"
if (zipEntry.name.endsWith(".opf")) { if (zipEntry.name.endsWith(".opf")) {
/*掌上书苑有很多自制书OPF的nameSpace格式不标准,强制修复成正确的格式*/ /*掌上书苑有很多自制书OPF的nameSpace格式不标准,强制修复成正确的格式*/
val newS = String(resource.data).replace( val newS = String(resource.data).replace(
@ -133,11 +133,8 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
/*获取当前章节文本*/ /*获取当前章节文本*/
var string = getChildChapter(chapter, chapter.url) var string = getChildChapter(chapter, chapter.url)
val childContends = appDb.epubChapterDao.get(book.bookUrl, chapter.url) val childContends = appDb.epubChapterDao.get(book.bookUrl, chapter.url)
if (childContends != null) { childContends?.forEach {
/*如果书籍当前章节有多个html文件,追加文本*/ string += "\n" + getChildChapter(chapter, it.href)
for (child in childContends) {
string += "\n" + getChildChapter(chapter, child.href)
}
} }
return string return string
} }
@ -194,7 +191,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
return epubBook?.resources?.getByHref(abHref)?.inputStream return epubBook?.resources?.getByHref(abHref)?.inputStream
} }
private fun getBookInfo() { private fun upBookInfo() {
if (epubBook == null) { if (epubBook == null) {
eFile = null eFile = null
book.intro = "书籍导入异常" book.intro = "书籍导入异常"
@ -343,8 +340,7 @@ class EPUBFile(var book: io.legado.app.data.entities.Book) {
refs: List<TOCReference>?, refs: List<TOCReference>?,
level: Int level: Int
) { ) {
if (refs == null) return refs?.forEach { ref ->
for (ref in refs) {
if (ref.resource != null) { if (ref.resource != null) {
val chapter = BookChapter() val chapter = BookChapter()
chapter.bookUrl = book.bookUrl chapter.bookUrl = book.bookUrl

@ -83,6 +83,7 @@ object LocalBook {
"${MD5Utils.md5Encode16(path)}.jpg" "${MD5Utils.md5Encode16(path)}.jpg"
) )
) )
if (book.isEpub()) EPUBFile.upBookInfo(book)
appDb.bookDao.insert(book) appDb.bookDao.insert(book)
return book return book
} }

Loading…
Cancel
Save