🛠️ Fix cannot add books without cover in EPUB

Fix, cannot add books that do not contain cover image in them. Problem was caused by returning function with null before getting to return parsed book.
This commit is contained in:
Acclorite 2024-08-12 20:28:59 +03:00
parent 3b69b89819
commit 739833ed44
2 changed files with 10 additions and 11 deletions

View file

@ -61,12 +61,6 @@ class EpubFileParser @Inject constructor() : FileParser {
.firstOrNull()?.attr("href") .firstOrNull()?.attr("href")
} }
if (coverImagePath == null) {
return@withContext
}
val coverImage = extractCoverImageBitmap(file, coverImagePath)
book = Book( book = Book(
title = title, title = title,
author = author, author = author,
@ -79,7 +73,7 @@ class EpubFileParser @Inject constructor() : FileParser {
lastOpened = null, lastOpened = null,
category = Category.entries[0], category = Category.entries[0],
coverImage = null coverImage = null
) to coverImage ) to extractCoverImageBitmap(file, coverImagePath)
} }
} }
return book return book
@ -90,7 +84,11 @@ class EpubFileParser @Inject constructor() : FileParser {
} }
} }
private fun extractCoverImageBitmap(file: File, coverImagePath: String): Bitmap? { private fun extractCoverImageBitmap(file: File, coverImagePath: String?): Bitmap? {
if (coverImagePath.isNullOrBlank()) {
return null
}
ZipFile(file).use { zip -> ZipFile(file).use { zip ->
zip.entries().asSequence().forEach { entry -> zip.entries().asSequence().forEach { entry ->
if (entry.name.endsWith(coverImagePath)) { if (entry.name.endsWith(coverImagePath)) {

View file

@ -37,12 +37,13 @@ class EpubTextParser @Inject constructor() : TextParser {
} }
val document = Jsoup.parse(content) val document = Jsoup.parse(content)
document.select("p").append("\n")
document document
.wholeText() .wholeText()
.lines() .lines()
.forEach { element -> .forEach { line ->
if (element.isNotBlank()) { if (line.isNotBlank()) {
lines.add(element.trim()) lines.add(line.trim())
} }
} }
} }