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) {
@@ -454,6 +454,21 @@ class EpubMetadataExtractorTest {
assertTrue(cover.length > 0);
assertEquals(pngImage.length, cover.length);
}
@Test
@DisplayName("Should extract cover using meta name='cover' attribute fallback with URL-encoded href")
void extractCover_metaCoverAttribute_returnsCoverBytes() throws IOException {
byte[] pngImage = createMinimalPngImage();
// Create EPUB where cover is only discoverable via <meta name="cover" content="img-id"/>
// Use URL-encoded href (cover+.png -> cover%2B.png) to verify proper decoding
File epubFile = createEpubWithMetaCoverAttribute(pngImage, "image-id", "images/img001%2B.png");
byte[] cover = extractor.extractCover(epubFile);
assertNotNull(cover, "Cover should be extracted via meta cover attribute with encoded URL");
assertTrue(cover.length > 0);
assertArrayEquals(pngImage, cover);
}
}
@Nested
@@ -811,6 +826,59 @@ class EpubMetadataExtractorTest {
}
}
private File createEpubWithMetaCoverAttribute(byte[] coverImageData, String id, String encodedHref) throws IOException {
// This EPUB uses <meta name="cover" content="id"/> to reference the cover image
// The ID and href do NOT contain "cover" and there's no properties="cover-image"
// This tests the fallback path: getMetaAttribute("cover") -> getById(coverId)
// The href may be URL-encoded (e.g., image%2B.png for image+.png)
String opfContent = String.format("""
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Book with Meta Cover Attribute</dc:title>
<meta name="cover" content="%s"/>
</metadata>
<manifest>
<item id="%s" href="%s" media-type="image/png"/>
</manifest>
</package>
""", id, id, encodedHref);
File epubFile = tempDir.resolve("test-meta-cover-" + System.nanoTime() + ".epub").toFile();
String containerXml = """
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
""";
// Decode the href for the actual file path in the ZIP
String decodedHref = java.net.URLDecoder.decode(encodedHref, StandardCharsets.UTF_8);
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(epubFile))) {
zos.putNextEntry(new ZipEntry("mimetype"));
zos.write("application/epub+zip".getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
zos.putNextEntry(new ZipEntry("META-INF/container.xml"));
zos.write(containerXml.getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
zos.putNextEntry(new ZipEntry("OEBPS/content.opf"));
zos.write(opfContent.getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
zos.putNextEntry(new ZipEntry("OEBPS/" + decodedHref));
zos.write(coverImageData);
zos.closeEntry();
}
return epubFile;
}
private File createEpubWithUnicodeCover(byte[] coverImageData, String id, String encodedHref) throws IOException {
String opfContent = String.format("""
<?xml version="1.0" encoding="UTF-8"?>