From c7fa64c045a4b0142a69351cd41244f92cc7b551 Mon Sep 17 00:00:00 2001 From: AJ Banck Date: Mon, 19 Jan 2026 16:47:06 +0100 Subject: [PATCH] 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 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 --- .../extractor/EpubMetadataExtractor.java | 12 ++++ .../extractor/EpubMetadataExtractorTest.java | 68 +++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractor.java index 76f964d3d..7227d6b17 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractor.java @@ -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) { diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java index 2aac03f17..1ad73cb01 100644 --- a/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/extractor/EpubMetadataExtractorTest.java @@ -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 + // 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 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(""" + + + + Book with Meta Cover Attribute + + + + + + + """, id, id, encodedHref); + + File epubFile = tempDir.resolve("test-meta-cover-" + System.nanoTime() + ".epub").toFile(); + + String containerXml = """ + + + + + + + """; + + // 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("""