Improve EPUB cover replacement for EPUB 2/3 and ensure covers display correctly on client devices (#1428)

This commit is contained in:
Aditya Chandel
2025-10-23 23:23:28 -06:00
committed by GitHub
parent 5fe62ac576
commit ee0d6b8e4b
@@ -364,16 +364,59 @@ public class EpubMetadataWriter implements MetadataWriter {
Element manifest = (Element) manifestList.item(0); Element manifest = (Element) manifestList.item(0);
Element existingCoverItem = null; Element existingCoverItem = null;
NodeList items = manifest.getElementsByTagNameNS(OPF_NS, "item"); // First, try to find cover via metadata reference (EPUB 3 style)
for (int i = 0; i < items.getLength(); i++) { NodeList metadataList = opfDoc.getElementsByTagNameNS(OPF_NS, "metadata");
Element item = (Element) items.item(i); if (metadataList.getLength() > 0) {
if ("cover-image".equals(item.getAttribute("id"))) { Element metadataElement = (Element) metadataList.item(0);
existingCoverItem = item; String coverItemId = getMetaContentByName(metadataElement, "cover");
break;
if (coverItemId != null && !coverItemId.isBlank()) {
// Find the item with this id
NodeList items = manifest.getElementsByTagNameNS(OPF_NS, "item");
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
if (coverItemId.equals(item.getAttribute("id"))) {
existingCoverItem = item;
break;
}
}
} }
} }
String coverHref = existingCoverItem != null ? existingCoverItem.getAttribute("href") : "images/cover.jpg"; // If not found, try looking for properties="cover-image" (EPUB 3)
if (existingCoverItem == null) {
NodeList items = manifest.getElementsByTagNameNS(OPF_NS, "item");
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
String properties = item.getAttribute("properties");
if (properties != null && properties.contains("cover-image")) {
existingCoverItem = item;
break;
}
}
}
// If still not found, try common id values (EPUB 2 fallback)
if (existingCoverItem == null) {
NodeList items = manifest.getElementsByTagNameNS(OPF_NS, "item");
for (int i = 0; i < items.getLength(); i++) {
Element item = (Element) items.item(i);
String itemId = item.getAttribute("id");
if ("cover-image".equals(itemId) || "cover".equals(itemId) || "coverimg".equals(itemId)) {
existingCoverItem = item;
break;
}
}
}
if (existingCoverItem == null) {
throw new IOException("No cover item found in manifest");
}
String coverHref = existingCoverItem.getAttribute("href");
if (coverHref == null || coverHref.isBlank()) {
throw new IOException("Cover item has no href attribute");
}
Path opfPath; Path opfPath;
try { try {
@@ -384,31 +427,9 @@ public class EpubMetadataWriter implements MetadataWriter {
Path opfDir = opfPath.getParent(); Path opfDir = opfPath.getParent();
Path coverFilePath = opfDir.resolve(coverHref).normalize(); Path coverFilePath = opfDir.resolve(coverHref).normalize();
Files.createDirectories(coverFilePath.getParent()); Files.createDirectories(coverFilePath.getParent());
Files.write(coverFilePath, coverData); Files.write(coverFilePath, coverData);
if (existingCoverItem != null) {
manifest.removeChild(existingCoverItem);
}
Element newItem = opfDoc.createElementNS(OPF_NS, "item");
newItem.setAttribute("id", "cover-image");
newItem.setAttribute("href", coverHref);
newItem.setAttribute("media-type", "image/jpeg");
manifest.appendChild(newItem);
NodeList metadataList = opfDoc.getElementsByTagNameNS(OPF_NS, "metadata");
if (metadataList.getLength() == 0) {
throw new IOException("No <metadata> element found in OPF document.");
}
Element metadataElement = (Element) metadataList.item(0);
removeMetaByName(metadataElement, "cover");
Element meta = opfDoc.createElementNS(OPF_NS, "meta");
meta.setAttribute("name", "cover");
meta.setAttribute("content", "cover-image");
metadataElement.appendChild(meta);
} }
private Path findOpfPath(Path tempDir) throws IOException, ParserConfigurationException, SAXException { private Path findOpfPath(Path tempDir) throws IOException, ParserConfigurationException, SAXException {