@@ -95,8 +95,8 @@ public class BookCoverController {
|
||||
@ApiResponse(responseCode = "204", description = "Covers regenerated successfully")
|
||||
@PostMapping("/regenerate-covers")
|
||||
@PreAuthorize("@securityUtil.canBulkRegenerateCover() or @securityUtil.isAdmin()")
|
||||
public void regenerateCovers() {
|
||||
bookCoverService.regenerateCovers();
|
||||
public void regenerateCovers(@RequestParam(defaultValue = "false") boolean missingOnly) {
|
||||
bookCoverService.regenerateCovers(missingOnly);
|
||||
}
|
||||
|
||||
@Operation(summary = "Regenerate cover for a book", description = "Regenerate cover for a specific book. Requires metadata edit permission or admin.")
|
||||
|
||||
@@ -293,18 +293,20 @@ public class BookCoverService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate covers for all books.
|
||||
* Regenerate covers for all books, optionally only for books with missing covers.
|
||||
*/
|
||||
public void regenerateCovers() {
|
||||
public void regenerateCovers(boolean missingOnly) {
|
||||
SecurityContextVirtualThread.runWithSecurityContext(() -> {
|
||||
try {
|
||||
List<BookRegenerationInfo> books = bookQueryService.getAllFullBookEntities().stream()
|
||||
.filter(book -> !isCoverLocked(book))
|
||||
.filter(book -> book.getPrimaryBookFile() != null)
|
||||
.filter(book -> !missingOnly || book.getBookCoverHash() == null)
|
||||
.map(book -> new BookRegenerationInfo(book.getId(), book.getMetadata().getTitle(), book.getPrimaryBookFile().getBookType(), false))
|
||||
.toList();
|
||||
int total = books.size();
|
||||
notificationService.sendMessage(Topic.LOG, LogNotification.info("Started regenerating covers for " + total + " books"));
|
||||
String label = missingOnly ? "missing" : "all";
|
||||
notificationService.sendMessage(Topic.LOG, LogNotification.info("Started regenerating covers for " + total + " books (" + label + ")"));
|
||||
|
||||
int current = 1;
|
||||
List<Long> refreshedIds = new ArrayList<>();
|
||||
|
||||
@@ -755,7 +755,7 @@ class BookCoverServiceTest {
|
||||
return null;
|
||||
});
|
||||
|
||||
service.regenerateCovers();
|
||||
service.regenerateCovers(false);
|
||||
|
||||
verify(bookRepository).save(book);
|
||||
assertThat(book.getMetadata().getCoverUpdatedOn()).isNotNull();
|
||||
@@ -779,12 +779,55 @@ class BookCoverServiceTest {
|
||||
return null;
|
||||
});
|
||||
|
||||
service.regenerateCovers();
|
||||
service.regenerateCovers(false);
|
||||
|
||||
verify(bookRepository, never()).save(any());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void missingOnlySkipsBooksWithExistingCover() {
|
||||
BookEntity withCover = buildBook(1L, false);
|
||||
withCover.setBookCoverHash("existingHash");
|
||||
BookFileEntity ebookFile1 = BookFileEntity.builder()
|
||||
.bookType(BookFileType.EPUB).isBookFormat(true).build();
|
||||
withCover.setBookFiles(List.of(ebookFile1));
|
||||
withCover.setLibrary(LibraryEntity.builder().build());
|
||||
|
||||
BookEntity withoutCover = buildBook(2L, false);
|
||||
withoutCover.setBookCoverHash(null);
|
||||
BookFileEntity ebookFile2 = BookFileEntity.builder()
|
||||
.bookType(BookFileType.EPUB).isBookFormat(true).build();
|
||||
withoutCover.setBookFiles(List.of(ebookFile2));
|
||||
withoutCover.setLibrary(LibraryEntity.builder().build());
|
||||
|
||||
when(bookQueryService.getAllFullBookEntities()).thenReturn(List.of(withCover, withoutCover));
|
||||
|
||||
BookFileProcessor processor = mock(BookFileProcessor.class);
|
||||
when(transactionTemplate.execute(any())).thenAnswer(inv -> {
|
||||
var callback = inv.getArgument(0, org.springframework.transaction.support.TransactionCallback.class);
|
||||
return callback.doInTransaction(null);
|
||||
});
|
||||
when(bookRepository.findById(2L)).thenReturn(Optional.of(withoutCover));
|
||||
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(processor);
|
||||
when(processor.generateCover(withoutCover)).thenReturn(true);
|
||||
when(bookRepository.findCoverUpdateInfoByIds(any())).thenReturn(List.of());
|
||||
|
||||
try (MockedStatic<SecurityContextVirtualThread> secMock = mockStatic(SecurityContextVirtualThread.class)) {
|
||||
secMock.when(() -> SecurityContextVirtualThread.runWithSecurityContext(any(Runnable.class)))
|
||||
.thenAnswer(inv -> {
|
||||
inv.<Runnable>getArgument(0).run();
|
||||
return null;
|
||||
});
|
||||
|
||||
service.regenerateCovers(true);
|
||||
|
||||
verify(transactionTemplate, times(1)).execute(any());
|
||||
verify(bookRepository).save(withoutCover);
|
||||
verify(bookRepository, never()).findById(1L);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void skipsBooksWithNoPrimaryFile() {
|
||||
BookEntity book = buildBook(1L, false);
|
||||
@@ -799,7 +842,7 @@ class BookCoverServiceTest {
|
||||
return null;
|
||||
});
|
||||
|
||||
service.regenerateCovers();
|
||||
service.regenerateCovers(false);
|
||||
|
||||
verify(transactionTemplate, never()).execute(any());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user