fix(epub): support cover edits when no previous cover (#2278)
This commit is contained in:
+114
@@ -36,6 +36,7 @@ import java.nio.file.Path;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
@@ -151,6 +152,33 @@ class EpubMetadataWriterTest {
|
||||
assertThat(content).doesNotContain("refines=\"#example2\"");
|
||||
assertThat(content).contains("refines=\"#example\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Should sort package children in EPUB3")
|
||||
void writeMetadata_shouldSortPackageChildren() throws Exception {
|
||||
String opfContent = """
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
|
||||
<manifest></manifest>
|
||||
<collection></collection>
|
||||
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||
</metadata>
|
||||
</package>""";
|
||||
|
||||
File epubFile = createEpubWithOpf(opfContent, "test-sort-package-" + System.nanoTime() + ".epub");
|
||||
writer.saveMetadataToFile(epubFile, metadata, null, new MetadataClearFlags());
|
||||
|
||||
String content = readOpfContent(epubFile);
|
||||
|
||||
int manifestIndex = content.indexOf("<manifest");
|
||||
int metadataIndex = content.indexOf("<metadata");
|
||||
int spineIndex = content.indexOf("<spine");
|
||||
int collectionIndex = content.indexOf("<collection");
|
||||
|
||||
assertThat(metadataIndex).isLessThan(manifestIndex);
|
||||
assertThat(manifestIndex).isLessThan(spineIndex);
|
||||
assertThat(spineIndex).isLessThan(collectionIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -694,6 +722,92 @@ class EpubMetadataWriterTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("Cover Tests")
|
||||
class CoverTests {
|
||||
@Test
|
||||
void shouldCreateManifestItemIfNoneExist() 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="2.0">
|
||||
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
|
||||
<dc:title>Test Book</dc:title>
|
||||
</metadata>
|
||||
</package>
|
||||
""",
|
||||
"no-cover-" + System.nanoTime() + ".epub"
|
||||
);
|
||||
|
||||
writer.saveMetadataToFile(epubFile, metadata, thumbnailPath.toString(), new MetadataClearFlags());
|
||||
|
||||
|
||||
String content = readOpfContent(epubFile);
|
||||
assertThat(content).contains("href=\"cover.");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddEpub3CoverImageTag() 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">
|
||||
<dc:title>Test Book</dc:title>
|
||||
</metadata>
|
||||
</package>
|
||||
""",
|
||||
"no-cover-" + System.nanoTime() + ".epub"
|
||||
);
|
||||
|
||||
writer.saveMetadataToFile(epubFile, metadata, thumbnailPath.toString(), new MetadataClearFlags());
|
||||
|
||||
|
||||
String content = readOpfContent(epubFile);
|
||||
assertThat(content).contains("properties=\"cover-image\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAllowOnlyOneCoverImagePropertiesEpub3() 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">
|
||||
<dc:title>Test Book</dc:title>
|
||||
</metadata>
|
||||
<manifest>
|
||||
<item href="foo" properties="cover-image" />
|
||||
<item href="bar" properties="cover-image" />
|
||||
</manifest>
|
||||
</package>
|
||||
""",
|
||||
"cover-" + System.nanoTime() + ".epub"
|
||||
);
|
||||
|
||||
writer.saveMetadataToFile(epubFile, metadata, thumbnailPath.toString(), new MetadataClearFlags());
|
||||
|
||||
|
||||
String content = readOpfContent(epubFile);
|
||||
var pattern = Pattern.compile("properties=\"cover-image\"");
|
||||
var matcher = pattern.matcher(content);
|
||||
int count = 0;
|
||||
while (matcher.find()) {
|
||||
count++;
|
||||
}
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
private Document parseOpf(File epubFile) throws Exception {
|
||||
try (ZipFile zf = new ZipFile(epubFile)) {
|
||||
ZipEntry ze = zf.getEntry("OEBPS/content.opf");
|
||||
|
||||
Reference in New Issue
Block a user