Fix: correctly update book cover hash on metadata refresh (#2149)
Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
+61
-28
@@ -57,6 +57,13 @@ public class BookCoverService {
|
||||
private record BookRegenerationInfo(Long id, String title, BookFileType bookType) {
|
||||
}
|
||||
|
||||
// =========================
|
||||
// SECTION: COVER UPDATES
|
||||
// =========================
|
||||
|
||||
/**
|
||||
* Generate a custom cover for a single book.
|
||||
*/
|
||||
public void generateCustomCover(long bookId) {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
if (isCoverLocked(bookEntity)) {
|
||||
@@ -71,18 +78,27 @@ public class BookCoverService {
|
||||
notifyBookCoverUpdate(bookEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update cover image from uploaded file for a single book.
|
||||
*/
|
||||
@Transactional
|
||||
public BookMetadata updateCoverImageFromFile(Long bookId, MultipartFile file) {
|
||||
fileService.createThumbnailFromFile(bookId, file);
|
||||
return updateCover(bookId, (writer, book) -> writer.replaceCoverImageFromUpload(book, file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update cover image from a URL for a single book.
|
||||
*/
|
||||
@Transactional
|
||||
public BookMetadata updateCoverImageFromUrl(Long bookId, String url) {
|
||||
fileService.createThumbnailFromUrl(bookId, url);
|
||||
return updateCover(bookId, (writer, book) -> writer.replaceCoverImageFromUrl(book, url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk update cover images from a file for multiple books.
|
||||
*/
|
||||
public void updateCoverImageFromFileForBooks(Set<Long> bookIds, MultipartFile file) {
|
||||
validateCoverFile(file);
|
||||
byte[] coverImageBytes = extractBytesFromMultipartFile(file);
|
||||
@@ -90,24 +106,34 @@ public class BookCoverService {
|
||||
SecurityContextVirtualThread.runWithSecurityContext(() -> processBulkCoverUpdate(unlockedBooks, coverImageBytes));
|
||||
}
|
||||
|
||||
public void regenerateCover(long bookId) {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId)
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
// =========================
|
||||
// SECTION: COVER REGENERATION
|
||||
// =========================
|
||||
|
||||
/**
|
||||
* Regenerate cover for a single book.
|
||||
*/
|
||||
public void regenerateCover(long bookId) {
|
||||
BookEntity bookEntity = bookRepository.findById(bookId).orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
if (isCoverLocked(bookEntity)) {
|
||||
throw ApiError.METADATA_LOCKED.createException();
|
||||
}
|
||||
|
||||
regenerateCoverForBook(bookEntity, "");
|
||||
updateBookCoverMetadata(bookEntity);
|
||||
bookRepository.save(bookEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate covers for a set of books.
|
||||
*/
|
||||
public void regenerateCoversForBooks(Set<Long> bookIds) {
|
||||
List<BookRegenerationInfo> unlockedBooks = getUnlockedBookRegenerationInfos(bookIds);
|
||||
SecurityContextVirtualThread.runWithSecurityContext(() -> processBulkCoverRegeneration(unlockedBooks));
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate covers for all books.
|
||||
*/
|
||||
public void regenerateCovers() {
|
||||
SecurityContextVirtualThread.runWithSecurityContext(() -> {
|
||||
try {
|
||||
@@ -135,30 +161,9 @@ public class BookCoverService {
|
||||
});
|
||||
}
|
||||
|
||||
private BookMetadata updateCover(Long bookId, BiConsumer<MetadataWriter, BookEntity> writerAction) {
|
||||
BookEntity bookEntity = bookRepository.findAllWithMetadataByIds(Collections.singleton(bookId)).stream()
|
||||
.findFirst()
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
|
||||
updateBookCoverMetadata(bookEntity);
|
||||
|
||||
MetadataPersistenceSettings settings = appSettingService.getAppSettings().getMetadataPersistenceSettings();
|
||||
boolean saveToOriginalFile = settings.isSaveToOriginalFile();
|
||||
boolean convertCbrCb7ToCbz = settings.isConvertCbrCb7ToCbz();
|
||||
|
||||
if (saveToOriginalFile && (bookEntity.getBookType() != BookFileType.CBX || convertCbrCb7ToCbz)) {
|
||||
metadataWriterFactory.getWriter(bookEntity.getBookType())
|
||||
.ifPresent(writer -> {
|
||||
writerAction.accept(writer, bookEntity);
|
||||
String newHash = FileFingerprint.generateHash(bookEntity.getFullFilePath());
|
||||
bookEntity.setCurrentHash(newHash);
|
||||
});
|
||||
}
|
||||
|
||||
bookEntity.setMetadataUpdatedAt(Instant.now());
|
||||
bookRepository.save(bookEntity);
|
||||
return bookMetadataMapper.toBookMetadata(bookEntity.getMetadata(), true);
|
||||
}
|
||||
// =========================
|
||||
// SECTION: BULK OPERATIONS
|
||||
// =========================
|
||||
|
||||
private void processBulkCoverUpdate(List<BookCoverInfo> books, byte[] coverImageBytes) {
|
||||
try {
|
||||
@@ -221,6 +226,34 @@ public class BookCoverService {
|
||||
}
|
||||
}
|
||||
|
||||
// =========================
|
||||
// SECTION: INTERNAL HELPERS
|
||||
// =========================
|
||||
|
||||
private BookMetadata updateCover(Long bookId, BiConsumer<MetadataWriter, BookEntity> writerAction) {
|
||||
BookEntity bookEntity = bookRepository.findAllWithMetadataByIds(Collections.singleton(bookId)).stream()
|
||||
.findFirst()
|
||||
.orElseThrow(() -> ApiError.BOOK_NOT_FOUND.createException(bookId));
|
||||
|
||||
MetadataPersistenceSettings settings = appSettingService.getAppSettings().getMetadataPersistenceSettings();
|
||||
boolean saveToOriginalFile = settings.isSaveToOriginalFile();
|
||||
boolean convertCbrCb7ToCbz = settings.isConvertCbrCb7ToCbz();
|
||||
|
||||
if (saveToOriginalFile && (bookEntity.getBookType() != BookFileType.CBX || convertCbrCb7ToCbz)) {
|
||||
metadataWriterFactory.getWriter(bookEntity.getBookType())
|
||||
.ifPresent(writer -> {
|
||||
writerAction.accept(writer, bookEntity);
|
||||
String newHash = FileFingerprint.generateHash(bookEntity.getFullFilePath());
|
||||
bookEntity.setCurrentHash(newHash);
|
||||
});
|
||||
}
|
||||
|
||||
bookEntity.setMetadataUpdatedAt(Instant.now());
|
||||
updateBookCoverMetadata(bookEntity);
|
||||
bookRepository.save(bookEntity);
|
||||
return bookMetadataMapper.toBookMetadata(bookEntity.getMetadata(), true);
|
||||
}
|
||||
|
||||
private void regenerateCoverForBook(BookEntity book, String progress) {
|
||||
String title = book.getMetadata().getTitle();
|
||||
notificationService.sendMessage(Topic.LOG, LogNotification.info(progress + "Regenerating cover for: " + title));
|
||||
|
||||
+11
-14
@@ -9,15 +9,12 @@ import com.adityachandel.booklore.model.dto.settings.MetadataPersistenceSettings
|
||||
import com.adityachandel.booklore.model.entity.*;
|
||||
import com.adityachandel.booklore.model.enums.BookFileType;
|
||||
import com.adityachandel.booklore.model.enums.MetadataReplaceMode;
|
||||
import com.adityachandel.booklore.repository.AuthorRepository;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.repository.CategoryRepository;
|
||||
import com.adityachandel.booklore.repository.MoodRepository;
|
||||
import com.adityachandel.booklore.repository.TagRepository;
|
||||
import com.adityachandel.booklore.service.file.FileFingerprint;
|
||||
import com.adityachandel.booklore.repository.*;
|
||||
import com.adityachandel.booklore.service.appsettings.AppSettingService;
|
||||
import com.adityachandel.booklore.service.file.FileFingerprint;
|
||||
import com.adityachandel.booklore.service.file.FileMoveService;
|
||||
import com.adityachandel.booklore.service.metadata.writer.MetadataWriterFactory;
|
||||
import com.adityachandel.booklore.util.BookCoverUtils;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import com.adityachandel.booklore.util.MetadataChangeDetector;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -82,7 +79,6 @@ public class BookMetadataUpdater {
|
||||
return;
|
||||
}
|
||||
|
||||
// If all fields are locked we must allow unlocking, hasValueChanges will be false
|
||||
if (metadata.areAllFieldsLocked() && hasValueChanges) {
|
||||
log.warn("All fields are locked for book ID {}. Skipping update.", bookId);
|
||||
return;
|
||||
@@ -100,7 +96,7 @@ public class BookMetadataUpdater {
|
||||
updateMoodsIfNeeded(newMetadata, metadata, clearFlags, mergeMoods, replaceMode);
|
||||
updateTagsIfNeeded(newMetadata, metadata, clearFlags, mergeTags, replaceMode);
|
||||
bookReviewUpdateService.updateBookReviews(newMetadata, metadata, clearFlags, mergeCategories);
|
||||
updateThumbnailIfNeeded(bookId, newMetadata, metadata, updateThumbnail);
|
||||
updateThumbnailIfNeeded(bookId, bookEntity, newMetadata, metadata, updateThumbnail);
|
||||
updateLocks(newMetadata, metadata);
|
||||
|
||||
bookEntity.setMetadataUpdatedAt(Instant.now());
|
||||
@@ -229,14 +225,14 @@ public class BookMetadataUpdater {
|
||||
if (replaceAll) {
|
||||
if (!merge) e.getAuthors().clear();
|
||||
e.getAuthors().addAll(newAuthors);
|
||||
e.updateSearchText(); // Manually trigger search text update since collection modification doesn't trigger @PreUpdate
|
||||
e.updateSearchText();
|
||||
} else if (replaceMissing && e.getAuthors().isEmpty()) {
|
||||
e.getAuthors().addAll(newAuthors);
|
||||
e.updateSearchText(); // Manually trigger search text update since collection modification doesn't trigger @PreUpdate
|
||||
e.updateSearchText();
|
||||
} else if (replaceMode == null) {
|
||||
if (!merge) e.getAuthors().clear();
|
||||
e.getAuthors().addAll(newAuthors);
|
||||
e.updateSearchText(); // Manually trigger search text update since collection modification doesn't trigger @PreUpdate
|
||||
e.updateSearchText();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,17 +350,18 @@ public class BookMetadataUpdater {
|
||||
}
|
||||
}
|
||||
|
||||
private void updateThumbnailIfNeeded(long bookId, BookMetadata m, BookMetadataEntity e, boolean set) {
|
||||
private void updateThumbnailIfNeeded(long bookId, BookEntity bookEntity, BookMetadata m, BookMetadataEntity e, boolean set) {
|
||||
if (Boolean.TRUE.equals(e.getCoverLocked())) {
|
||||
return; // Locked — do nothing
|
||||
return;
|
||||
}
|
||||
if (!set) return;
|
||||
if (!StringUtils.hasText(m.getThumbnailUrl()) || isLocalOrPrivateUrl(m.getThumbnailUrl())) return;
|
||||
try {
|
||||
fileService.createThumbnailFromUrl(bookId, m.getThumbnailUrl());
|
||||
bookEntity.setBookCoverHash(BookCoverUtils.generateCoverHash());
|
||||
bookEntity.getMetadata().setCoverUpdatedOn(Instant.now());
|
||||
} catch (Exception ex) {
|
||||
log.warn("Failed to download cover for book {}: {}", bookId, ex.getMessage());
|
||||
// Don't rethrow - cover failures shouldn't roll back metadata updates
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user