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("""