fix(fb2): prioritize explicit cover binary elements (#2230)
This commit is contained in:
+61
-37
@@ -35,50 +35,74 @@ public class Fb2MetadataExtractor implements FileMetadataExtractor {
|
|||||||
private static final Pattern ISBN_CLEANER_PATTERN = Pattern.compile("[^0-9Xx]");
|
private static final Pattern ISBN_CLEANER_PATTERN = Pattern.compile("[^0-9Xx]");
|
||||||
private static final Pattern ISO_DATE_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
|
private static final Pattern ISO_DATE_PATTERN = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
|
||||||
|
|
||||||
|
private String getCoverId(Document doc) {
|
||||||
|
Element titleInfo = getFirstElementByTagNameNS(doc, FB2_NAMESPACE, "title-info");
|
||||||
|
if (titleInfo == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Element coverPage = getFirstElementByTagNameNS(titleInfo, FB2_NAMESPACE, "coverpage");
|
||||||
|
if (coverPage == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Element image = getFirstElementByTagNameNS(coverPage, FB2_NAMESPACE, "image");
|
||||||
|
if (image == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String href = image.getAttributeNS("http://www.w3.org/1999/xlink", "href");
|
||||||
|
if (!href.startsWith("#")) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return href.substring(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Element getCoverBinaryElement(Document doc) {
|
||||||
|
String coverId = getCoverId(doc);
|
||||||
|
|
||||||
|
// Look for cover image in binary elements
|
||||||
|
NodeList binaries = doc.getElementsByTagNameNS(FB2_NAMESPACE, "binary");
|
||||||
|
|
||||||
|
Element coverLike = null;
|
||||||
|
|
||||||
|
for (int i = 0; i < binaries.getLength(); i++) {
|
||||||
|
if (binaries.item(i) instanceof Element binary) {
|
||||||
|
String id = binary.getAttribute("id");
|
||||||
|
|
||||||
|
if (coverId != null && coverId.equals(id)) {
|
||||||
|
return binary;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!id.toLowerCase().contains("cover")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
String contentType = binary.getAttribute("content-type");
|
||||||
|
if (!contentType.startsWith("image/")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
coverLike = binary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fall back to the cover-like image if it was found;
|
||||||
|
return coverLike;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public byte[] extractCover(File file) {
|
public byte[] extractCover(File file) {
|
||||||
try (InputStream inputStream = getInputStream(file)) {
|
try (InputStream inputStream = getInputStream(file)) {
|
||||||
DocumentBuilder builder = SecureXmlUtils.createSecureDocumentBuilder(true);
|
DocumentBuilder builder = SecureXmlUtils.createSecureDocumentBuilder(true);
|
||||||
Document doc = builder.parse(inputStream);
|
Document doc = builder.parse(inputStream);
|
||||||
|
|
||||||
// Look for cover image in binary elements
|
Element binary = getCoverBinaryElement(doc);
|
||||||
NodeList binaries = doc.getElementsByTagNameNS(FB2_NAMESPACE, "binary");
|
|
||||||
for (int i = 0; i < binaries.getLength(); i++) {
|
|
||||||
Element binary = (Element) binaries.item(i);
|
|
||||||
String id = binary.getAttribute("id");
|
|
||||||
|
|
||||||
if (id != null && id.toLowerCase().contains("cover")) {
|
if (binary != null) {
|
||||||
String contentType = binary.getAttribute("content-type");
|
String base64Data = binary.getTextContent().trim();
|
||||||
if (contentType != null && contentType.startsWith("image/")) {
|
return Base64.getMimeDecoder().decode(base64Data);
|
||||||
String base64Data = binary.getTextContent().trim();
|
|
||||||
return Base64.getMimeDecoder().decode(base64Data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no cover found by name, try to find the first referenced image in title-info
|
|
||||||
Element titleInfo = getFirstElementByTagNameNS(doc, FB2_NAMESPACE, "title-info");
|
|
||||||
if (titleInfo != null) {
|
|
||||||
NodeList coverPages = titleInfo.getElementsByTagNameNS(FB2_NAMESPACE, "coverpage");
|
|
||||||
if (coverPages.getLength() > 0) {
|
|
||||||
Element coverPage = (Element) coverPages.item(0);
|
|
||||||
NodeList images = coverPage.getElementsByTagNameNS(FB2_NAMESPACE, "image");
|
|
||||||
if (images.getLength() > 0) {
|
|
||||||
Element image = (Element) images.item(0);
|
|
||||||
String href = image.getAttributeNS("http://www.w3.org/1999/xlink", "href");
|
|
||||||
if (href != null && href.startsWith("#")) {
|
|
||||||
String imageId = href.substring(1);
|
|
||||||
// Find the binary with this ID
|
|
||||||
for (int i = 0; i < binaries.getLength(); i++) {
|
|
||||||
Element binary = (Element) binaries.item(i);
|
|
||||||
if (imageId.equals(binary.getAttribute("id"))) {
|
|
||||||
String base64Data = binary.getTextContent().trim();
|
|
||||||
return Base64.getMimeDecoder().decode(base64Data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
+47
-1
@@ -516,7 +516,7 @@ class Fb2MetadataExtractorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void extractCover_fallbackToCoverpageReference() throws IOException {
|
void extractCover_readCoverpageReference() throws IOException {
|
||||||
byte[] imageData = {0x01, 0x02, 0x03, 0x04};
|
byte[] imageData = {0x01, 0x02, 0x03, 0x04};
|
||||||
String base64 = Base64.getEncoder().encodeToString(imageData);
|
String base64 = Base64.getEncoder().encodeToString(imageData);
|
||||||
File file = writeFb2("""
|
File file = writeFb2("""
|
||||||
@@ -535,6 +535,52 @@ class Fb2MetadataExtractorTest {
|
|||||||
assertThat(cover).isEqualTo(imageData);
|
assertThat(cover).isEqualTo(imageData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void extractCover_prioritizeCoverPageReference() throws IOException {
|
||||||
|
byte[] imageDataA = {0x01, 0x02, 0x03, 0x04};
|
||||||
|
byte[] imageDataB = {0x05, 0x06, 0x07, 0x08};
|
||||||
|
byte[] imageDataC = {0x09, 0x10, 0x11, 0x12};
|
||||||
|
String base64A = Base64.getEncoder().encodeToString(imageDataA);
|
||||||
|
String base64B = Base64.getEncoder().encodeToString(imageDataB);
|
||||||
|
String base64C = Base64.getEncoder().encodeToString(imageDataC);
|
||||||
|
File file = writeFb2("""
|
||||||
|
<description>
|
||||||
|
<title-info>
|
||||||
|
<coverpage>
|
||||||
|
<image l:href="#img1"/>
|
||||||
|
</coverpage>
|
||||||
|
</title-info>
|
||||||
|
</description>
|
||||||
|
<binary id="img1" content-type="image/png">%s</binary>
|
||||||
|
<binary id="not-cover.jpg" content-type="image/png">%s</binary>
|
||||||
|
<binary id="also-not-cover.jpg" content-type="image/png">%s</binary>
|
||||||
|
""".formatted(base64A, base64B, base64C));
|
||||||
|
|
||||||
|
byte[] cover = extractor.extractCover(file);
|
||||||
|
|
||||||
|
assertThat(cover).isEqualTo(imageDataA);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void extractCover_fallsBackWhenCoverPageRefIsMissing() throws IOException {
|
||||||
|
byte[] imageData = {0x01, 0x02, 0x03, 0x04};
|
||||||
|
String base64 = Base64.getEncoder().encodeToString(imageData);
|
||||||
|
File file = writeFb2("""
|
||||||
|
<description>
|
||||||
|
<title-info>
|
||||||
|
<coverpage>
|
||||||
|
<image l:href="#img1"/>
|
||||||
|
</coverpage>
|
||||||
|
</title-info>
|
||||||
|
</description>
|
||||||
|
<binary id="cover.jpg" content-type="image/png">%s</binary>
|
||||||
|
""".formatted(base64));
|
||||||
|
|
||||||
|
byte[] cover = extractor.extractCover(file);
|
||||||
|
|
||||||
|
assertThat(cover).isEqualTo(imageData);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void extractCover_noBinaryReturnsNull() throws IOException {
|
void extractCover_noBinaryReturnsNull() throws IOException {
|
||||||
File file = writeFb2("""
|
File file = writeFb2("""
|
||||||
|
|||||||
Reference in New Issue
Block a user