Improve EPUB cover replacement for EPUB 2/3 and ensure covers display correctly on client devices (#1428)
This commit is contained in:
+51
-30
@@ -364,16 +364,59 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
Element manifest = (Element) manifestList.item(0);
|
||||
Element existingCoverItem = null;
|
||||
|
||||
NodeList items = manifest.getElementsByTagNameNS(OPF_NS, "item");
|
||||
for (int i = 0; i < items.getLength(); i++) {
|
||||
Element item = (Element) items.item(i);
|
||||
if ("cover-image".equals(item.getAttribute("id"))) {
|
||||
existingCoverItem = item;
|
||||
break;
|
||||
// First, try to find cover via metadata reference (EPUB 3 style)
|
||||
NodeList metadataList = opfDoc.getElementsByTagNameNS(OPF_NS, "metadata");
|
||||
if (metadataList.getLength() > 0) {
|
||||
Element metadataElement = (Element) metadataList.item(0);
|
||||
String coverItemId = getMetaContentByName(metadataElement, "cover");
|
||||
|
||||
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;
|
||||
try {
|
||||
@@ -384,31 +427,9 @@ public class EpubMetadataWriter implements MetadataWriter {
|
||||
|
||||
Path opfDir = opfPath.getParent();
|
||||
Path coverFilePath = opfDir.resolve(coverHref).normalize();
|
||||
|
||||
Files.createDirectories(coverFilePath.getParent());
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user