fix(epub): handle literal + when decoding URLs (#2618)
This commit is contained in:
@@ -494,6 +494,12 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
|
||||
private Path getManifestItemPath(Path tempDir, Path opfDir, Element element) throws IOException {
|
||||
String href = element.getAttribute("href");
|
||||
|
||||
// Technically, epub specification only refers to `href` as supporting percent-encoding.
|
||||
// Unfortunately, the Java URLDecoder.decode method will also do `+` -> space decoding.
|
||||
// To work around this, we can replace `+` with the percent-encoded version of a plus.
|
||||
href = href.replaceAll("\\+", "%2b");
|
||||
|
||||
String decodedHref = URLDecoder.decode(href, StandardCharsets.UTF_8);
|
||||
if (decodedHref == null || decodedHref.isBlank()) {
|
||||
throw new IOException("Manifest item has no href attribute");
|
||||
|
||||
+62
@@ -518,6 +518,68 @@ class EpubMetadataWriterTest {
|
||||
assertThat(mimetypeCount).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should handle file names with a literal plus sign")
|
||||
void saveMetadata_handlesPlus() throws Exception {
|
||||
String opfContent = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
|
||||
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||
<dc:title>Original Title</dc:title>
|
||||
<meta name="cover" content="cover-image"/>
|
||||
</metadata>
|
||||
<manifest>
|
||||
<item id="cover-image" href="cover+example.bin" media-type="application/octet-stream" properties="cover-image"/>
|
||||
</manifest>
|
||||
</package>""";
|
||||
|
||||
byte[] coverImageA = new byte[]{0x0A};
|
||||
byte[] coverImageB = new byte[]{0x0B};
|
||||
|
||||
File coverImageFile = tempDir.resolve("new-cover-" + System.nanoTime() + ".bin").toFile();
|
||||
Files.write(coverImageFile.toPath(), coverImageB);
|
||||
|
||||
File epubFile = tempDir.resolve("test-duplicate-" + System.nanoTime() + ".epub").toFile();
|
||||
|
||||
try (var zos = new ZipArchiveOutputStream(new FileOutputStream(epubFile))) {
|
||||
String containerXml = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
||||
<rootfiles>
|
||||
<rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
|
||||
</rootfiles>
|
||||
</container>
|
||||
""";
|
||||
|
||||
zos.putArchiveEntry(new ZipArchiveEntry("mimetype"));
|
||||
zos.write("application/epub+zip".getBytes(StandardCharsets.UTF_8));
|
||||
zos.closeArchiveEntry();
|
||||
|
||||
zos.putArchiveEntry(new ZipArchiveEntry("META-INF/container.xml"));
|
||||
zos.write(containerXml.getBytes(StandardCharsets.UTF_8));
|
||||
zos.closeArchiveEntry();
|
||||
|
||||
zos.putArchiveEntry(new ZipArchiveEntry("content.opf"));
|
||||
zos.write(opfContent.getBytes(StandardCharsets.UTF_8));
|
||||
zos.closeArchiveEntry();
|
||||
|
||||
zos.putArchiveEntry(new ZipArchiveEntry("cover+example.bin"));
|
||||
zos.write(coverImageA);
|
||||
zos.closeArchiveEntry();
|
||||
}
|
||||
|
||||
writer.saveMetadataToFile(epubFile, metadata, coverImageFile.toString(), new MetadataClearFlags());
|
||||
|
||||
try (ZipFile zf = new ZipFile(epubFile)) {
|
||||
var entry = zf.getEntry("cover+example.bin");
|
||||
|
||||
try (InputStream is = zf.getInputStream(entry)) {
|
||||
assertThat(is.readAllBytes()).isEqualTo(coverImageB);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
Reference in New Issue
Block a user