feat(custom-covers): add functionality to generate custom covers for selected books (#2355)

* feat(custom-covers): add functionality to generate custom covers for selected books

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

* feat(book-menu): add option to generate custom covers in metadata menu

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>

---------

Signed-off-by: Balázs Szücs <bszucs1209@gmail.com>
This commit is contained in:
Balázs Szücs
2026-01-20 08:56:18 -07:00
committed by GitHub
parent b7c0d7050d
commit 691845adb8
8 changed files with 149 additions and 2 deletions
@@ -85,6 +85,14 @@ public class BookCoverController {
bookCoverService.regenerateCoversForBooks(request.getBookIds());
}
@Operation(summary = "Generate custom covers for selected books", description = "Generate custom covers for a list of books. Requires metadata edit permission or admin.")
@ApiResponse(responseCode = "204", description = "Custom cover generation started successfully")
@PostMapping("/bulk-generate-custom-covers")
@PreAuthorize("@securityUtil.canBulkRegenerateCover() or @securityUtil.isAdmin()")
public void generateCustomCoversForBooks(@Parameter(description = "List of book IDs") @Validated @RequestBody BulkBookIdsRequest request) {
bookCoverService.generateCustomCoversForBooks(request.getBookIds());
}
@Operation(summary = "Upload cover image for multiple books", description = "Upload a cover image to apply to multiple books. Requires metadata edit permission or admin.")
@ApiResponse(responseCode = "204", description = "Cover upload started successfully")
@PostMapping("/bulk-upload-cover")
@@ -157,6 +157,14 @@ public class BookCoverService {
SecurityContextVirtualThread.runWithSecurityContext(() -> processBulkCoverRegeneration(unlockedBooks));
}
/**
* Generate custom covers for a set of books.
*/
public void generateCustomCoversForBooks(Set<Long> bookIds) {
List<BookCoverInfo> unlockedBooks = getUnlockedBookCoverInfos(bookIds);
SecurityContextVirtualThread.runWithSecurityContext(() -> processBulkCustomCoverGeneration(unlockedBooks));
}
/**
* Regenerate covers for all books.
*/
@@ -294,6 +302,49 @@ public class BookCoverService {
}
}
private void processBulkCustomCoverGeneration(List<BookCoverInfo> books) {
try {
int total = books.size();
notificationService.sendMessage(Topic.LOG, LogNotification.info("Started generating custom covers for " + total + " selected book(s)"));
int current = 1;
List<Long> refreshedIds = new ArrayList<>();
for (BookCoverInfo bookInfo : books) {
try {
String progress = "(" + current + "/" + total + ") ";
notificationService.sendMessage(Topic.LOG, LogNotification.info(progress + "Generating custom cover for: " + bookInfo.title()));
transactionTemplate.execute(status -> {
bookRepository.findById(bookInfo.id()).ifPresent(book -> {
String title = book.getMetadata().getTitle();
String author = getAuthorNames(book);
byte[] coverBytes = coverImageGenerator.generateCover(title, author);
fileService.createThumbnailFromBytes(book.getId(), coverBytes);
writeCoverToBookFile(book, (writer, b) -> writer.replaceCoverImageFromBytes(b, coverBytes));
updateBookCoverMetadata(book);
bookRepository.save(book);
refreshedIds.add(book.getId());
});
return null;
});
log.info("{}Successfully generated custom cover for book ID {} ({})", progress, bookInfo.id(), bookInfo.title());
} catch (Exception e) {
log.error("Failed to generate custom cover for book ID {}: {}", bookInfo.id(), e.getMessage(), e);
}
current++;
}
notifyBulkCoverUpdate(refreshedIds);
notificationService.sendMessage(Topic.LOG, LogNotification.info("Finished generating custom covers for selected books"));
} catch (Exception e) {
log.error("Error during custom cover generation: {}", e.getMessage(), e);
notificationService.sendMessage(Topic.LOG, LogNotification.error("Error occurred during custom cover generation"));
}
}
// =========================
// SECTION: INTERNAL HELPERS
// =========================