Skip to content

Commit

Permalink
fix(epub): epub not loading correctly on windows (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
gotson committed May 5, 2020
1 parent 6d40601 commit 8535f46
Showing 1 changed file with 7 additions and 5 deletions.
Expand Up @@ -22,7 +22,7 @@ class EpubExtractor(contentDetector: ContentDetector) : ZipExtractor(contentDete
val opfFile = getPackagePath(zip)

val opfDoc = zip.getInputStream(zip.getEntry(opfFile)).use { Jsoup.parse(it, null, "") }
val opfDir = Paths.get(opfFile).parentOrEmpty()
val opfDir = Paths.get(opfFile).parent

val manifest = opfDoc.select("manifest > item")
.associate { it.attr("id") to ManifestItem(it.attr("id"), it.attr("href"), it.attr("media-type")) }
Expand All @@ -32,16 +32,18 @@ class EpubExtractor(contentDetector: ContentDetector) : ZipExtractor(contentDete
.map { it.href }

val images = pages
.map { opfDir.resolve(it).normalize() }
.map { opfDir?.resolve(it)?.normalize() ?: Paths.get(it) }
.flatMap { pagePath ->
val doc = zip.getInputStream(zip.getEntry(pagePath.separatorsToUnix())).use { Jsoup.parse(it, null, "") }
doc.getElementsByTag("img")
.map { it.attr("src") }
.map { pagePath.parentOrEmpty().resolve(it).normalize() }
.map { it.attr("src") } //get the src, which can be a relative path
.map { pagePath.parentOrEmpty().resolve(it).normalize() } //resolve it against the page folder
}

return images.map { image ->
MediaContainerEntry(image.toString(), manifest.values.first { it.href == opfDir.relativize(image).separatorsToUnix() }.mediaType)
MediaContainerEntry(image.separatorsToUnix(), manifest.values.first {
it.href == (opfDir?.relativize(image) ?: image).separatorsToUnix()
}.mediaType)
}
} catch (e: Exception) {
logger.error(e) { "File is not a proper Epub, treating it as a zip file" }
Expand Down

0 comments on commit 8535f46

Please sign in to comment.