From 77e06b868ea587f62860319543fc52df4ace81f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Thu, 25 Dec 2025 18:05:24 +0100 Subject: [PATCH] fix(epub-metadata): properly decode URL-encoded cover image hrefs in EPUB files (#1983) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ensure cover image paths in EPUB manifest are URL-decoded before file access - Update EpubMetadataExtractor and EpubMetadataWriter to handle encoded hrefs - Add comprehensive tests for unicode and URL-encoded cover image filenames Signed-off-by: Balázs Szücs --- .../extractor/EpubMetadataExtractor.java | 5 +- .../metadata/writer/EpubMetadataWriter.java | 7 +- .../extractor/EpubMetadataExtractorTest.java | 91 ++++++++ .../writer/EpubMetadataWriterTest.java | 210 ++++++++++++++++++ 4 files changed, 310 insertions(+), 3 deletions(-) create mode 100644 booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java 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 01a7da24a..72f40fdf4 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 @@ -21,6 +21,8 @@ import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.OffsetDateTime; import java.util.HashMap; @@ -332,7 +334,8 @@ public class EpubMetadataExtractor implements FileMetadataExtractor { String properties = item.getAttribute("properties"); if (properties != null && properties.contains("cover-image")) { String href = item.getAttribute("href"); - return resolvePath(opfPath, href); + String decodedHref = URLDecoder.decode(href, StandardCharsets.UTF_8); + return resolvePath(opfPath, decodedHref); } } } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java index ef5995c7f..30d439931 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriter.java @@ -29,6 +29,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; @@ -406,7 +408,8 @@ public class EpubMetadataWriter implements MetadataWriter { } String coverHref = existingCoverItem.getAttribute("href"); - if (coverHref == null || coverHref.isBlank()) { + String decodedCoverHref = URLDecoder.decode(coverHref, StandardCharsets.UTF_8); + if (decodedCoverHref == null || decodedCoverHref.isBlank()) { throw new IOException("Cover item has no href attribute"); } @@ -418,7 +421,7 @@ public class EpubMetadataWriter implements MetadataWriter { } Path opfDir = opfPath.getParent(); - Path coverFilePath = opfDir.resolve(coverHref).normalize(); + Path coverFilePath = opfDir.resolve(decodedCoverHref).normalize(); Files.createDirectories(coverFilePath.getParent()); Files.write(coverFilePath, coverData); 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 07dfa0df9..255de003a 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 @@ -721,5 +721,96 @@ class EpubMetadataExtractorTest { return epubFile; } + + @Nested + @DisplayName("URL Decoding Tests") + class UrlDecodingTests { + + @Test + @DisplayName("Should properly decode unicode characters in cover href") + void extractCover_withUnicodeHref_decodesCorrectly() throws IOException { + byte[] pngImage = createMinimalPngImage(); + String encodedHref = "cover%C3%A1.png"; // coverá.png URL-encoded + File epubFile = createEpubWithUnicodeCover(pngImage, "cover_image", encodedHref); + + byte[] cover = extractor.extractCover(epubFile); + + assertNotNull(cover, "Cover should be extracted from EPUB with URL-encoded href"); + assertTrue(cover.length > 0); + } + + @Test + @DisplayName("Should extract cover with URL-encoded characters in manifest") + void findCoverImageHrefInOpf_withEncodedHref_returnsDecodedPath() throws IOException { + byte[] pngImage = createMinimalPngImage(); + String encodedHref = "images%2Fcover%C3%A1.jpg"; // images/coverá.jpg URL-encoded + File epubFile = createEpubWithUnicodeCover(pngImage, "cover_image", encodedHref); + + byte[] cover = extractor.extractCover(epubFile); + + assertNotNull(cover, "Cover should be extracted even with encoded path"); + assertArrayEquals(pngImage, cover, "Extracted cover should match original image"); + } + + @Test + @DisplayName("Should handle multiple encoded unicode characters") + void extractCover_withMultipleEncodedChars_handlesCorrectly() throws IOException { + byte[] pngImage = createMinimalPngImage(); + String encodedHref = "c%C3%B3ver%20t%C3%ADtle%20%C3%A1nd%20%C3%B1ame.png"; + File epubFile = createEpubWithUnicodeCover(pngImage, "multi_unicode_cover", encodedHref); + + byte[] cover = extractor.extractCover(epubFile); + + assertNotNull(cover, "Cover should be extracted from filename with multiple encoded chars"); + assertTrue(cover.length > 0); + } + } + + private File createEpubWithUnicodeCover(byte[] coverImageData, String id, String encodedHref) throws IOException { + String opfContent = String.format(""" + + + + Book with Unicode Cover + + + + + + """, id, encodedHref); + + File epubFile = tempDir.resolve("test-unicode-cover-" + System.nanoTime() + ".epub").toFile(); + + String containerXml = """ + + + + + + + """; + + 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(); + + // The actual file path in the zip should match the decoded href + String decodedPath = java.net.URLDecoder.decode(encodedHref, java.nio.charset.StandardCharsets.UTF_8); + zos.putNextEntry(new ZipEntry("OEBPS/" + decodedPath)); + zos.write(coverImageData); + zos.closeEntry(); + } + + return epubFile; + } } diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java new file mode 100644 index 000000000..9a04018e2 --- /dev/null +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/metadata/writer/EpubMetadataWriterTest.java @@ -0,0 +1,210 @@ +package com.adityachandel.booklore.service.metadata.writer; + +import com.adityachandel.booklore.model.MetadataClearFlags; +import com.adityachandel.booklore.model.entity.AuthorEntity; +import com.adityachandel.booklore.model.entity.BookEntity; +import com.adityachandel.booklore.model.entity.BookMetadataEntity; +import com.adityachandel.booklore.model.entity.LibraryPathEntity; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.springframework.mock.web.MockMultipartFile; +import org.springframework.web.multipart.MultipartFile; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collections; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static org.junit.jupiter.api.Assertions.*; + +class EpubMetadataWriterTest { + + private EpubMetadataWriter writer; + private BookMetadataEntity metadata; + private BookEntity bookEntity; + + @TempDir + Path tempDir; + + @BeforeEach + void setUp() { + writer = new EpubMetadataWriter(); + metadata = new BookMetadataEntity(); + metadata.setTitle("Test Book"); + AuthorEntity author = new AuthorEntity(); + author.setName("Test Author"); + metadata.setAuthors(Collections.singleton(author)); + + bookEntity = new BookEntity(); + LibraryPathEntity libraryPath = new LibraryPathEntity(); + libraryPath.setPath(tempDir.toString()); + bookEntity.setLibraryPath(libraryPath); + bookEntity.setFileSubPath(""); + bookEntity.setFileName("test.epub"); + } + + @Nested + @DisplayName("URL Decoding Tests") + class UrlDecodingTests { + + @Test + @DisplayName("Should properly handle URL-encoded href values in manifest") + void writeMetadataToFile_withUnicodeHref_handlesDecoding() throws IOException { + byte[] epubContent = createEpubWithUnicodeCoverHref(); + File epubFile = tempDir.resolve("test_unicode.epub").toFile(); + Files.write(epubFile.toPath(), epubContent); + + assertDoesNotThrow(() -> writer.writeMetadataToFile(epubFile, metadata, null, new MetadataClearFlags())); + + assertTrue(epubFile.exists()); + assertTrue(epubFile.length() > 0); + } + + @Test + @DisplayName("Should handle URL-encoded cover href during cover replacement") + void replaceCoverImageFromUpload_withUnicodeHref_handlesDecoding() throws IOException { + byte[] epubContent = createEpubWithUnicodeCoverHref(); + File epubFile = tempDir.resolve("test_cover_unicode.epub").toFile(); + Files.write(epubFile.toPath(), epubContent); + + byte[] imageBytes = createMinimalPngImage(); + MultipartFile coverFile = new MockMultipartFile( + "cover.png", + "cover.png", + "image/png", + imageBytes + ); + + assertDoesNotThrow(() -> { + writer.replaceCoverImageFromUpload(bookEntity, coverFile); + }); + } + } + + private byte[] createEpubWithUnicodeCoverHref() throws IOException { + String opfContent = """ + + + + Test Book with Unicode Cover + Test Author + + + + + + + + + + + + """; + + String containerXml = """ + + + + + + + """; + + String htmlContent = """ + + + + + Test + + +

Test Content

+ + + """; + + String ncxContent = """ + + + + + + + + Test Book + + + + + Test + + + + + + """; + + byte[] coverImage = createMinimalPngImage(); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (ZipOutputStream zos = new ZipOutputStream(baos)) { + + 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/index.html")); + zos.write(htmlContent.getBytes(StandardCharsets.UTF_8)); + zos.closeEntry(); + + zos.putNextEntry(new ZipEntry("OEBPS/toc.ncx")); + zos.write(ncxContent.getBytes(StandardCharsets.UTF_8)); + zos.closeEntry(); + + String decodedCoverPath = java.net.URLDecoder.decode("cover%C3%A1.png", java.nio.charset.StandardCharsets.UTF_8); + zos.putNextEntry(new ZipEntry("OEBPS/" + decodedCoverPath)); + zos.write(coverImage); + zos.closeEntry(); + } + + return baos.toByteArray(); + } + + private byte[] createMinimalPngImage() { + return new byte[]{ + (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, + 0x00, 0x00, 0x00, 0x0D, + 0x49, 0x48, 0x44, 0x52, + 0x00, 0x00, 0x00, 0x01, + 0x00, 0x00, 0x00, 0x01, + 0x08, 0x06, + 0x00, 0x00, 0x00, + (byte) 0x90, (byte) 0x77, (byte) 0x53, (byte) 0xDE, + 0x00, 0x00, 0x00, 0x0A, + 0x49, 0x44, 0x41, 0x54, + 0x78, (byte) 0x9C, 0x63, 0x00, 0x01, 0x00, 0x00, 0x05, + 0x00, 0x01, + 0x0D, (byte) 0x0A, 0x2D, (byte) 0xB4, + 0x00, 0x00, 0x00, 0x00, + 0x49, 0x45, 0x4E, 0x44, + (byte) 0xAE, 0x42, 0x60, (byte) 0x82 + }; + } +} \ No newline at end of file