diff --git a/.gitignore b/.gitignore index 661c47499..262501023 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,4 @@ out/ .vscode/ local/ -booklore-api/src/main/resources/application-local.yaml +booklore-api/src/main/resources/application-local.yaml \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/EpubProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/EpubProcessor.java index 6cb313fef..25c60af2d 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/EpubProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/EpubProcessor.java @@ -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; }