Fix EPUB Cover Image Detection (#301)

Fix EPUB Cover Image Detection

---------

Co-authored-by: Aditya Chandel <8075870+adityachandelgit@users.noreply.github.com>
This commit is contained in:
Alex Fair
2025-05-18 13:20:53 -04:00
committed by GitHub
parent c2960ad830
commit 1309c0f6d7
2 changed files with 25 additions and 3 deletions

2
.gitignore vendored
View File

@@ -37,4 +37,4 @@ out/
.vscode/
local/
booklore-api/src/main/resources/application-local.yaml
booklore-api/src/main/resources/application-local.yaml

View File

@@ -76,14 +76,36 @@ public class EpubProcessor implements FileProcessor {
public boolean generateCover(BookEntity bookEntity) {
try {
io.documentnode.epub4j.domain.Book epub = new EpubReader().readEpub(new FileInputStream(FileUtils.getBookFullPath(bookEntity)));
File epubFile = new File(FileUtils.getBookFullPath(bookEntity));
io.documentnode.epub4j.domain.Book epub = new EpubReader().readEpub(new FileInputStream(epubFile));
// Try the default method
Resource coverImage = epub.getCoverImage();
// Fallback: look for a manifest resource with common "cover" keywords
if (coverImage == null) {
for (Resource res : epub.getResources().getAll()) {
String id = res.getId();
String href = res.getHref();
if ((id != null && id.toLowerCase().contains("cover")) ||
(href != null && href.toLowerCase().contains("cover"))) {
if (res.getMediaType() != null && res.getMediaType().getName().startsWith("image")) {
coverImage = res;
break;
}
}
}
}
boolean saved = saveCoverImage(coverImage, bookEntity.getId());
bookEntity.getMetadata().setCoverUpdatedOn(Instant.now());
bookMetadataRepository.save(bookEntity.getMetadata());
return saved;
} catch (Exception e) {
log.error("Error generating cover for epub file {}, error: {}", bookEntity.getFileName(), e.getMessage());
log.error("Error generating cover for epub file {}, error: {}", bookEntity.getFileName(), e.getMessage(), e);
}
return false;
}