fix(epub-metadata): properly decode URL-encoded cover image hrefs in EPUB files (#1983)

- 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 <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2025-12-25 10:05:24 -07:00
committed by GitHub
parent b4b4d8f492
commit 77e06b868e
4 changed files with 310 additions and 3 deletions
@@ -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);
}
}
}
@@ -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);