fix(epub): add cover extraction fallback using meta cover attribute (#2310)

Add fallback mechanism to extract EPUB cover images when the primary
epub4j getCoverImage() method returns null. The new fallback reads
the 'cover' meta attribute from EPUB metadata and uses it to look up
the cover resource by ID.

This improves cover detection for EPUBs that use the traditional
<meta name="cover" content="image-id"/> and the cover image href
uses a URL-encoded special character which epub4j can't resolve.

Changes:
- Add cover extraction fallback via getMetaAttribute("cover")
- Add null safety check for coverId before resource lookup
- Add test with URL-encoded href to verify proper path handling
This commit is contained in:
AJ Banck
2026-01-19 08:47:06 -07:00
committed by GitHub
parent 69c3c88375
commit c7fa64c045
2 changed files with 80 additions and 0 deletions
@@ -58,6 +58,18 @@ public class EpubMetadataExtractor implements FileMetadataExtractor {
return image;
}
// First fallback to reading the cover image based on the cover
String coverId = epub.getMetadata().getMetaAttribute("cover");
if (coverId != null) {
Resource coverResource = epub.getResources().getById(coverId);
if (coverResource != null) {
image = getImageFromEpubResource(coverResource);
if (image != null) {
return image;
}
}
}
// We fall back to reading the image based on the cover-image property.
String coverHref = findCoverImageHrefInOpf(epubFile);
if (coverHref != null) {