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