fix(epub): support cover edits when media type changes (#2281)

This commit is contained in:
James Ward
2026-09-12 13:03:49 -04:00
committed by GitHub
parent d75c1282f4
commit 828806a4aa
2 changed files with 47 additions and 3 deletions
@@ -550,6 +550,18 @@ public class EpubMetadataWriter implements MetadataWriter {
throw new IOException("Failed to parse container.xml to locate OPF path", e);
}
if (existingCoverItem != null) {
String existingMediaType = existingCoverItem.getAttribute("media-type");
if (!existingMediaType.equals(mediaType)) {
// If the existing cover item exists but has a different media type we can't
// use it. However, because it's _possible_ that the cover MAY be used elsewhere
// in the epub (such as referenced in HTML) we cannot delete the manifest item
// or the file. All we can do is discard it as the existing cover manifest item,
// and treat the epub as if it has no cover, and place a new cover manifest item.
existingCoverItem = null;
}
}
Path opfDir = opfPath.getParent();
if (existingCoverItem != null) {
@@ -387,7 +387,7 @@ class EpubMetadataWriterTest {
<meta name="cover" content="cover-image"/>
</metadata>
<manifest>
<item id="cover-image" href="cover.png" media-type="image/png" properties="cover-image"/>
<item id="cover-image" href="cover.png" media-type="text/plain" properties="cover-image"/>
</manifest>
</package>""";
@@ -786,8 +786,8 @@ class EpubMetadataWriterTest {
<dc:title>Test Book</dc:title>
</metadata>
<manifest>
<item href="foo" properties="cover-image" />
<item href="bar" properties="cover-image" />
<item href="foo" properties="cover-image" media-type="application/octet-stream" />
<item href="bar" properties="cover-image" media-type="application/octet-stream" />
</manifest>
</package>
""",
@@ -806,6 +806,38 @@ class EpubMetadataWriterTest {
}
assertThat(count).isEqualTo(1);
}
@Test
void shouldMitigateMediaTypeMismatch() throws Exception {
Path thumbnailPath = tempDir.resolve("thumbnail-" + System.nanoTime()).toAbsolutePath();
Files.write(thumbnailPath, new byte[]{0x01, 0x02, 0x03});
File epubFile = createEpubWithOpf(
"""
<?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/" xmlns:opf="http://www.idpf.org/2007/opf">
</metadata>
<manifest>
<item href="cover.bin" id="cover" properties="cover-image" media-type="image/png" />
</manifest>
</package>
""",
"no-cover-" + System.nanoTime() + ".epub"
);
writer.saveMetadataToFile(epubFile, metadata, thumbnailPath.toString(), new MetadataClearFlags());
String content = readOpfContent(epubFile);
// Removes properties from existing
assertThat(content).contains("<item href=\"cover.bin\" id=\"cover\" media-type=\"image/png\"/>");
// And creates a new item with the prop
assertThat(content).containsPattern("<item href=\"cover-[^\"]+\\.bin\" id=\"cover-[^\"]+\" media-type=\"application/octet-stream\" properties=\"cover-image\"/>.*");
}
}
private Document parseOpf(File epubFile) throws Exception {