Fix(metadata): Improve handling of non-author creator roles (#1987)

* Fix(metadata): Don't import creators with a non-author role as author

Looks at either the meta tag for the creators or the opf role attribute
to determine the role of a creator. If no role is specified its assumed
to be an author.

Also only prunes handled created roles when writing the epub data back
out to prevent clobbering any extra data that might be present in the
epub already.

* Fix(metadata): Add testcases for non-author creator handling

Just adds testcases for the previous changes to the extraction and
writer logic.
This commit is contained in:
MightyHalfGinger
2025-12-26 16:47:24 -07:00
committed by GitHub
parent 87fe405455
commit 4d8092e5bc
4 changed files with 175 additions and 8 deletions
@@ -232,6 +232,21 @@ class EpubMetadataExtractorTest {
assertEquals(2, result.getAuthors().size());
}
@Test
@DisplayName("Should not extract non-authors from EPUB metadata")
void extractMetadata_withExtraCreators_returnsOnlyAuthors() throws IOException {
File epubFile = createEpubWithExtraCreators(DEFAULT_TITLE, DEFAULT_AUTHOR, "Jane Smith", "Alice", "Bob");
BookMetadata result = extractor.extractMetadata(epubFile);
assertNotNull(result);
assertTrue(result.getAuthors().contains(DEFAULT_AUTHOR));
assertTrue(result.getAuthors().contains("Jane Smith"));
assertFalse(result.getAuthors().contains("Alice"));
assertFalse(result.getAuthors().contains("Bob"));
assertEquals(2, result.getAuthors().size());
}
@Test
@DisplayName("Should extract publisher from EPUB metadata")
void extractMetadata_withPublisher_returnsPublisher() throws IOException {
@@ -533,6 +548,23 @@ class EpubMetadataExtractorTest {
return createEpubWithOpf(opfContent, "test-multiauthor-" + System.nanoTime() + ".epub");
}
private File createEpubWithExtraCreators(String title, String author1, String author2, String illustrator, String editor) throws IOException {
String opfContent = String.format("""
<?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>%s</dc:title>
<dc:creator>%s</dc:creator>
<dc:creator opf:role="aut">%s</dc:creator>
<dc:creator opf:role="ill">%s</dc:creator>
<dc:creator id="creator04">%s</dc:creator>
<meta property="role" refines="#creator04" scheme="marc:relators">edt</meta>
</metadata>
</package>
""", title, author1, author2, illustrator, editor);
return createEpubWithOpf(opfContent, "test-extracreator-" + System.nanoTime() + ".epub");
}
private File createEpubWithCalibreSeries(String title, String seriesName, String seriesIndex) throws IOException {
String opfContent = String.format("""
<?xml version="1.0" encoding="UTF-8"?>
@@ -15,11 +15,14 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -51,6 +54,40 @@ class EpubMetadataWriterTest {
bookEntity.setFileName("test.epub");
}
@Nested
@DisplayName("Metadata writing Tests")
class MetadataWritingTests {
@Test
@DisplayName("Should only overwrite authors of EPUB metadata")
void writeMetadata_withAuthor_onlyAuthor() throws IOException {
StringBuilder existingMetadata = new StringBuilder();
existingMetadata.append("<metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">");
existingMetadata.append("<dc:creator id=\"creator02\">Alice</dc:creator>");
existingMetadata.append("<meta property=\"role\" refines=\"#creator02\">ill</meta>");
existingMetadata.append("</metadata>");
String opfContent = String.format("""
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0">
%s
</package>
""", existingMetadata);
File epubFile = createEpubWithOpf(opfContent, "test-metadata-" + System.nanoTime() + ".epub");
assertDoesNotThrow(() -> writer.writeMetadataToFile(epubFile, metadata, null, new MetadataClearFlags()));
assertTrue(epubFile.exists());
assertTrue(epubFile.length() > 0);
try (ZipFile zf = new ZipFile(epubFile)) {
ZipEntry ze = zf.getEntry("OEBPS/content.opf");
try (InputStream is = zf.getInputStream(ze)) {
byte[] fileBytes = is.readAllBytes();
String fileString = new String(fileBytes);
assertTrue(fileString.contains("id=\"creator02\""));
}
}
}
}
@Nested
@DisplayName("URL Decoding Tests")
class UrlDecodingTests {
@@ -89,6 +126,35 @@ class EpubMetadataWriterTest {
}
}
private File createEpubWithOpf(String opfContent, String filename) throws IOException {
File epubFile = tempDir.resolve(filename).toFile();
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="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
""";
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(epubFile))) {
zos.putNextEntry(new ZipEntry("mimetype"));
zos.write("application/epub+zip".getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
zos.putNextEntry(new ZipEntry("META-INF/container.xml"));
zos.write(containerXml.getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
zos.putNextEntry(new ZipEntry("OEBPS/content.opf"));
zos.write(opfContent.getBytes(StandardCharsets.UTF_8));
zos.closeEntry();
}
return epubFile;
}
private byte[] createEpubWithUnicodeCoverHref() throws IOException {
String opfContent = """
<?xml version="1.0" encoding="UTF-8"?>