From 395fcca80d0d171e70782dc8d3f8aae1782a8b51 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Mon, 14 Jul 2025 08:21:10 -0700 Subject: [PATCH 01/12] refactor(api/services): don't scan library files twice on rescan --- .../service/library/LibraryProcessingService.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java index 7f8c028ec..6c6c20e04 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java @@ -57,13 +57,14 @@ public class LibraryProcessingService { public void rescanLibrary(long libraryId) throws IOException { LibraryEntity libraryEntity = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); notificationService.sendMessage(Topic.LOG, createLogNotification("Started refreshing library: " + libraryEntity.getName())); - deleteRemovedBooks(detectDeletedBookIds(libraryEntity)); - processLibraryFiles(detectNewBookPaths(libraryEntity)); + List libraryFiles = getLibraryFiles(libraryEntity); + deleteRemovedBooks(detectDeletedBookIds(libraryFiles, libraryEntity)); + processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity)); notificationService.sendMessage(Topic.LOG, createLogNotification("Finished refreshing library: " + libraryEntity.getName())); } - public List detectDeletedBookIds(LibraryEntity libraryEntity) throws IOException { - Set currentFileNames = getLibraryFiles(libraryEntity).stream() + public static List detectDeletedBookIds(List libraryFiles, LibraryEntity libraryEntity) throws IOException { + Set currentFileNames = libraryFiles.stream() .map(LibraryFile::getFileName) .collect(Collectors.toSet()); @@ -74,11 +75,11 @@ public class LibraryProcessingService { .collect(Collectors.toList()); } - public List detectNewBookPaths(LibraryEntity libraryEntity) throws IOException { + public static List detectNewBookPaths(List libraryFiles, LibraryEntity libraryEntity) throws IOException { Set existingFileNames = libraryEntity.getBookEntities().stream() .map(BookEntity::getFileName) .collect(Collectors.toSet()); - return getLibraryFiles(libraryEntity).stream() + return libraryFiles.stream() .filter(file -> !existingFileNames.contains(file.getFileName())) .collect(Collectors.toList()); } From f084bb1da6c18896af8acf887a0a1d3a9a0893cb Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 16 Jul 2025 00:48:28 -0700 Subject: [PATCH 02/12] refactor(library): remove checked IOException --- .../booklore/service/library/LibraryProcessingService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java index 6c6c20e04..632faaa44 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java @@ -63,7 +63,7 @@ public class LibraryProcessingService { notificationService.sendMessage(Topic.LOG, createLogNotification("Finished refreshing library: " + libraryEntity.getName())); } - public static List detectDeletedBookIds(List libraryFiles, LibraryEntity libraryEntity) throws IOException { + public static List detectDeletedBookIds(List libraryFiles, LibraryEntity libraryEntity) { Set currentFileNames = libraryFiles.stream() .map(LibraryFile::getFileName) .collect(Collectors.toSet()); @@ -75,7 +75,7 @@ public class LibraryProcessingService { .collect(Collectors.toList()); } - public static List detectNewBookPaths(List libraryFiles, LibraryEntity libraryEntity) throws IOException { + public static List detectNewBookPaths(List libraryFiles, LibraryEntity libraryEntity) { Set existingFileNames = libraryEntity.getBookEntities().stream() .map(BookEntity::getFileName) .collect(Collectors.toSet()); From c9e05b1c282d40630bcc463295e1f068b4b7d5e5 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 16 Jul 2025 00:49:28 -0700 Subject: [PATCH 03/12] refactor(library): extract processLibraryFiles into separate FileAsBookProcessor class --- .../service/library/FileAsBookProcessor.java | 52 +++++++++++++++++++ .../service/library/LibraryFileProcessor.java | 10 ++++ .../library/LibraryProcessingService.java | 37 +++---------- .../watcher/BookFileTransactionalHandler.java | 2 +- 4 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java create mode 100644 booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessor.java diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java new file mode 100644 index 000000000..49c9e2e87 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java @@ -0,0 +1,52 @@ +package com.adityachandel.booklore.service.library; + +import com.adityachandel.booklore.model.dto.Book; +import com.adityachandel.booklore.model.dto.settings.LibraryFile; +import com.adityachandel.booklore.model.entity.LibraryEntity; +import com.adityachandel.booklore.model.websocket.Topic; +import com.adityachandel.booklore.service.NotificationService; +import com.adityachandel.booklore.service.fileprocessor.CbxProcessor; +import com.adityachandel.booklore.service.fileprocessor.EpubProcessor; +import com.adityachandel.booklore.service.fileprocessor.PdfProcessor; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +import static com.adityachandel.booklore.model.websocket.LogNotification.createLogNotification; + +@AllArgsConstructor +@Component +@Slf4j +public class FileAsBookProcessor implements LibraryFileProcessor { + + private final NotificationService notificationService; + private final PdfProcessor pdfProcessor; + private final EpubProcessor epubProcessor; + private final CbxProcessor cbxProcessor; + + @Override + @Transactional + public void processLibraryFiles(List libraryFiles, LibraryEntity libraryEntity) { + for (LibraryFile libraryFile : libraryFiles) { + log.info("Processing file: {}", libraryFile.getFileName()); + Book book = processLibraryFile(libraryFile); + if (book != null) { + notificationService.sendMessage(Topic.BOOK_ADD, book); + notificationService.sendMessage(Topic.LOG, createLogNotification("Book added: " + book.getFileName())); + log.info("Processed file: {}", libraryFile.getFileName()); + } + } + } + + @Transactional + protected Book processLibraryFile(LibraryFile libraryFile) { + return switch (libraryFile.getBookFileType()) { + case PDF -> pdfProcessor.processFile(libraryFile); + case EPUB -> epubProcessor.processFile(libraryFile); + case CBX -> cbxProcessor.processFile(libraryFile); + }; + } +} \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessor.java new file mode 100644 index 000000000..ee7fbdb17 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessor.java @@ -0,0 +1,10 @@ +package com.adityachandel.booklore.service.library; + +import com.adityachandel.booklore.model.dto.settings.LibraryFile; +import com.adityachandel.booklore.model.entity.LibraryEntity; + +import java.util.List; + +public interface LibraryFileProcessor { + void processLibraryFiles(List libraryFiles, LibraryEntity libraryEntity); +} \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java index 632faaa44..c1263ea83 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java @@ -1,7 +1,6 @@ package com.adityachandel.booklore.service.library; import com.adityachandel.booklore.exception.ApiError; -import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; import com.adityachandel.booklore.model.entity.LibraryEntity; @@ -11,9 +10,6 @@ import com.adityachandel.booklore.model.websocket.Topic; import com.adityachandel.booklore.repository.BookRepository; import com.adityachandel.booklore.repository.LibraryRepository; import com.adityachandel.booklore.service.NotificationService; -import com.adityachandel.booklore.service.fileprocessor.CbxProcessor; -import com.adityachandel.booklore.service.fileprocessor.EpubProcessor; -import com.adityachandel.booklore.service.fileprocessor.PdfProcessor; import com.adityachandel.booklore.util.FileService; import com.adityachandel.booklore.util.FileUtils; import lombok.AllArgsConstructor; @@ -38,18 +34,16 @@ public class LibraryProcessingService { private final LibraryRepository libraryRepository; private final NotificationService notificationService; - private final PdfProcessor pdfProcessor; - private final EpubProcessor epubProcessor; - private final CbxProcessor cbxProcessor; private final BookRepository bookRepository; private final FileService fileService; + private final FileAsBookProcessor fileAsBookProcessor; @Transactional public void processLibrary(long libraryId) throws IOException { LibraryEntity libraryEntity = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); notificationService.sendMessage(Topic.LOG, createLogNotification("Started processing library: " + libraryEntity.getName())); List libraryFiles = getLibraryFiles(libraryEntity); - processLibraryFiles(libraryFiles); + fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); notificationService.sendMessage(Topic.LOG, createLogNotification("Finished processing library: " + libraryEntity.getName())); } @@ -59,10 +53,14 @@ public class LibraryProcessingService { notificationService.sendMessage(Topic.LOG, createLogNotification("Started refreshing library: " + libraryEntity.getName())); List libraryFiles = getLibraryFiles(libraryEntity); deleteRemovedBooks(detectDeletedBookIds(libraryFiles, libraryEntity)); - processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity)); + fileAsBookProcessor.processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity), libraryEntity); notificationService.sendMessage(Topic.LOG, createLogNotification("Finished refreshing library: " + libraryEntity.getName())); } + public void processLibraryFiles(List libraryFiles, LibraryEntity libraryEntity) { + fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + } + public static List detectDeletedBookIds(List libraryFiles, LibraryEntity libraryEntity) { Set currentFileNames = libraryFiles.stream() .map(LibraryFile::getFileName) @@ -119,27 +117,6 @@ public class LibraryProcessingService { } } - @Transactional - public void processLibraryFiles(List libraryFiles) { - for (LibraryFile libraryFile : libraryFiles) { - log.info("Processing file: {}", libraryFile.getFileName()); - Book book = processLibraryFile(libraryFile); - if (book != null) { - notificationService.sendMessage(Topic.BOOK_ADD, book); - notificationService.sendMessage(Topic.LOG, createLogNotification("Book added: " + book.getFileName())); - log.info("Processed file: {}", libraryFile.getFileName()); - } - } - } - - @Transactional - protected Book processLibraryFile(LibraryFile libraryFile) { - return switch (libraryFile.getBookFileType()) { - case PDF -> pdfProcessor.processFile(libraryFile); - case EPUB -> epubProcessor.processFile(libraryFile); - case CBX -> cbxProcessor.processFile(libraryFile); - }; - } private List getLibraryFiles(LibraryEntity libraryEntity) throws IOException { List allFiles = new ArrayList<>(); diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/watcher/BookFileTransactionalHandler.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/watcher/BookFileTransactionalHandler.java index 023ddc127..57401aef5 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/watcher/BookFileTransactionalHandler.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/watcher/BookFileTransactionalHandler.java @@ -62,7 +62,7 @@ public class BookFileTransactionalHandler { .orElseThrow(() -> new IllegalArgumentException("Unsupported book file type: " + fileName))) .build(); - libraryProcessingService.processLibraryFiles(List.of(libraryFile)); + libraryProcessingService.processLibraryFiles(List.of(libraryFile), libraryEntity); notificationService.sendMessage(Topic.LOG, createLogNotification("Finished processing file: " + filePath)); log.info("[CREATE] Completed processing for file '{}'", filePath); From b7a17e1405514420cce6d4f908297ef35e934474 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 16 Jul 2025 01:45:24 -0700 Subject: [PATCH 04/12] refactor(service): use full path instead of just name to sync book files --- .../service/library/LibraryProcessingService.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java index c1263ea83..85baec5c6 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java @@ -62,23 +62,23 @@ public class LibraryProcessingService { } public static List detectDeletedBookIds(List libraryFiles, LibraryEntity libraryEntity) { - Set currentFileNames = libraryFiles.stream() - .map(LibraryFile::getFileName) + Set currentFullPaths = libraryFiles.stream() + .map(LibraryFile::getFullPath) .collect(Collectors.toSet()); return libraryEntity.getBookEntities().stream() .filter(book -> (book.getDeleted() == null || !book.getDeleted())) - .filter(book -> !currentFileNames.contains(book.getFileName())) + .filter(book -> !currentFullPaths.contains(book.getFullFilePath())) .map(BookEntity::getId) .collect(Collectors.toList()); } public static List detectNewBookPaths(List libraryFiles, LibraryEntity libraryEntity) { - Set existingFileNames = libraryEntity.getBookEntities().stream() - .map(BookEntity::getFileName) + Set existingFullPaths = libraryEntity.getBookEntities().stream() + .map(BookEntity::getFullFilePath) .collect(Collectors.toSet()); return libraryFiles.stream() - .filter(file -> !existingFileNames.contains(file.getFileName())) + .filter(file -> !existingFullPaths.contains(file.getFullPath())) .collect(Collectors.toList()); } From 3d17159c59625e20e5413c16ae6f701193821d7f Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 16 Jul 2025 08:34:36 -0700 Subject: [PATCH 05/12] refactor(services): add common BookFileProcessorRegistry for all current and future book file processors --- .../fileprocessor/BookFileProcessor.java | 4 ++ .../BookFileProcessorRegistry.java | 44 +++++++++++++++++++ .../service/fileprocessor/CbxProcessor.java | 6 +++ .../service/fileprocessor/EpubProcessor.java | 7 +++ .../service/fileprocessor/PdfProcessor.java | 7 +++ 5 files changed, 68 insertions(+) create mode 100644 booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java index 7a2cb394a..b68bf480c 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java @@ -3,8 +3,12 @@ package com.adityachandel.booklore.service.fileprocessor; import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; +import com.adityachandel.booklore.model.enums.BookFileExtension; + +import java.util.List; public interface BookFileProcessor { + List getSupportedExtensions(); Book processFile(LibraryFile libraryFile); boolean generateCover(BookEntity bookEntity); } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java new file mode 100644 index 000000000..6be568ed8 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java @@ -0,0 +1,44 @@ +package com.adityachandel.booklore.service.fileprocessor; + +import com.adityachandel.booklore.model.enums.BookFileExtension; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Component +@Slf4j +public class BookFileProcessorRegistry { + + private final Map processorMap; + + public BookFileProcessorRegistry(List processors) { + this.processorMap = new EnumMap<>(BookFileExtension.class); + initializeProcessorMap(processors); + } + + private void initializeProcessorMap(List processors) { + for (BookFileProcessor processor : processors) { + List supportedExtensions = processor.getSupportedExtensions(); + for (BookFileExtension extension : supportedExtensions) { + processorMap.put(extension, processor); + log.debug("Registered {} for extension: {}", processor.getClass().getSimpleName(), extension); + } + } + log.info("Initialized BookFileProcessorRegistry with {} processors for {} extensions", + processors.size(), processorMap.size()); + } + + public Optional getProcessor(BookFileExtension extension) { + return Optional.ofNullable(processorMap.get(extension)); + } + + public BookFileProcessor getProcessorOrThrow(BookFileExtension extension) { + return getProcessor(extension) + .orElseThrow(() -> new IllegalArgumentException( + "No processor found for file extension: " + extension)); + } +} \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java index 542c519a0..bcdf65d8e 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java @@ -4,6 +4,7 @@ import com.adityachandel.booklore.mapper.BookMapper; import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; +import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.repository.BookMetadataRepository; import com.adityachandel.booklore.repository.BookRepository; @@ -70,6 +71,11 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce return false; } + @Override + public List getSupportedExtensions() { + return List.of(BookFileExtension.CBZ, BookFileExtension.CBR, BookFileExtension.CB7); + } + private Optional extractImagesFromArchive(File file) { String name = file.getName().toLowerCase(); if (name.endsWith(".cbz")) { 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 fc13a8c22..961e56129 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 @@ -5,6 +5,7 @@ import com.adityachandel.booklore.model.dto.BookMetadata; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; import com.adityachandel.booklore.model.entity.BookMetadataEntity; +import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.repository.BookMetadataRepository; import com.adityachandel.booklore.repository.BookRepository; @@ -21,6 +22,7 @@ import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.*; import java.time.Instant; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; @@ -92,6 +94,11 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc } } + @Override + public List getSupportedExtensions() { + return List.of(BookFileExtension.EPUB); + } + private void setBookMetadata(BookEntity bookEntity) { File bookFile = new File(bookEntity.getFullFilePath().toUri()); BookMetadata epubMetadata = epubMetadataExtractor.extractMetadata(bookFile); diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java index d33908378..9d5896f47 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java @@ -4,6 +4,7 @@ import com.adityachandel.booklore.mapper.BookMapper; import com.adityachandel.booklore.model.dto.BookMetadata; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; +import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.repository.BookMetadataRepository; import com.adityachandel.booklore.repository.BookRepository; @@ -23,6 +24,7 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.time.Instant; +import java.util.List; import static com.adityachandel.booklore.service.fileprocessor.FileProcessingUtils.truncate; @@ -68,6 +70,11 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce } } + @Override + public List getSupportedExtensions() { + return List.of(BookFileExtension.PDF); + } + private void extractAndSetMetadata(BookEntity bookEntity) { try { BookMetadata extracted = pdfMetadataExtractor.extractMetadata(new File(FileUtils.getBookFullPath(bookEntity))); From 31e4ae29726e2839c775a70f90c41c87257e2232 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 16 Jul 2025 11:10:25 -0700 Subject: [PATCH 06/12] refactor(service): integrate BookFileProcessorRegistry for handling multiple book file formats --- .../service/bookdrop/BookDropService.java | 14 +++++------ .../service/library/FileAsBookProcessor.java | 24 ++++++++++--------- .../service/metadata/BookMetadataService.java | 23 +++++++++--------- .../service/upload/FileUploadService.java | 19 +++++++-------- .../booklore/service/BookDropServiceTest.java | 16 ++++--------- 5 files changed, 43 insertions(+), 53 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java index 0d9edeef0..3b4d40770 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java @@ -45,9 +45,7 @@ public class BookDropService { private final NotificationService notificationService; private final MetadataRefreshService metadataRefreshService; private final BookdropNotificationService bookdropNotificationService; - private final PdfProcessor pdfProcessor; - private final EpubProcessor epubProcessor; - private final CbxProcessor cbxProcessor; + private final BookFileProcessorRegistry processorRegistry; private final AppProperties appProperties; public BookdropFinalizeResult finalizeImport(BookdropFinalizeRequest request) { @@ -169,11 +167,11 @@ public class BookDropService { .fileName(fileName) .build(); - return switch (type) { - case PDF -> pdfProcessor.processFile(libraryFile); - case EPUB -> epubProcessor.processFile(libraryFile); - case CBX -> cbxProcessor.processFile(libraryFile); - }; + BookFileExtension extension = BookFileExtension.fromFileName(fileName) + .orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension")); + + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + return processor.processFile(libraryFile); } public void discardAllFiles() { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java index 49c9e2e87..03b8fa247 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java @@ -3,11 +3,11 @@ package com.adityachandel.booklore.service.library; import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.LibraryEntity; +import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.websocket.Topic; import com.adityachandel.booklore.service.NotificationService; -import com.adityachandel.booklore.service.fileprocessor.CbxProcessor; -import com.adityachandel.booklore.service.fileprocessor.EpubProcessor; -import com.adityachandel.booklore.service.fileprocessor.PdfProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -23,9 +23,7 @@ import static com.adityachandel.booklore.model.websocket.LogNotification.createL public class FileAsBookProcessor implements LibraryFileProcessor { private final NotificationService notificationService; - private final PdfProcessor pdfProcessor; - private final EpubProcessor epubProcessor; - private final CbxProcessor cbxProcessor; + private final BookFileProcessorRegistry processorRegistry; @Override @Transactional @@ -43,10 +41,14 @@ public class FileAsBookProcessor implements LibraryFileProcessor { @Transactional protected Book processLibraryFile(LibraryFile libraryFile) { - return switch (libraryFile.getBookFileType()) { - case PDF -> pdfProcessor.processFile(libraryFile); - case EPUB -> epubProcessor.processFile(libraryFile); - case CBX -> cbxProcessor.processFile(libraryFile); - }; + BookFileExtension extension = BookFileExtension.fromFileName(libraryFile.getFileName()).orElse(null); + if (extension == null) { + log.warn("Unsupported file type for file: {}", libraryFile.getFileName()); + return null; + } + + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + return processor.processFile(libraryFile); } + } \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java index 22bb6340c..26d64ead0 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java @@ -13,6 +13,7 @@ import com.adityachandel.booklore.model.dto.request.FetchMetadataRequest; import com.adityachandel.booklore.model.dto.request.ToggleAllLockRequest; import com.adityachandel.booklore.model.entity.BookEntity; import com.adityachandel.booklore.model.entity.BookMetadataEntity; +import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.Lock; import com.adityachandel.booklore.model.enums.MetadataProvider; import com.adityachandel.booklore.model.websocket.Topic; @@ -21,9 +22,8 @@ import com.adityachandel.booklore.repository.BookRepository; import com.adityachandel.booklore.service.BookQueryService; import com.adityachandel.booklore.service.NotificationService; import com.adityachandel.booklore.service.appsettings.AppSettingService; -import com.adityachandel.booklore.service.fileprocessor.CbxProcessor; -import com.adityachandel.booklore.service.fileprocessor.EpubProcessor; -import com.adityachandel.booklore.service.fileprocessor.PdfProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry; import com.adityachandel.booklore.service.metadata.backuprestore.MetadataBackupRestore; import com.adityachandel.booklore.service.metadata.backuprestore.MetadataBackupRestoreFactory; import com.adityachandel.booklore.service.metadata.parser.BookParser; @@ -61,9 +61,7 @@ public class BookMetadataService { private final AppSettingService appSettingService; private final BookMetadataRepository bookMetadataRepository; private final FileService fileService; - private final PdfProcessor pdfProcessor; - private final EpubProcessor epubProcessor; - private final CbxProcessor cbxProcessor; + private final BookFileProcessorRegistry processorRegistry; private final BookQueryService bookQueryService; private final Map parserMap; private final MetadataBackupRestoreFactory metadataBackupRestoreFactory; @@ -196,12 +194,13 @@ public class BookMetadataService { String title = book.getMetadata().getTitle(); String message = progress + "Regenerating cover for: " + title; notificationService.sendMessage(Topic.LOG, createLogNotification(message)); - switch (book.getBookType()) { - case PDF -> pdfProcessor.generateCover(book); - case EPUB -> epubProcessor.generateCover(book); - case CBX -> cbxProcessor.generateCover(book); - default -> throw ApiError.UNSUPPORTED_BOOK_TYPE.createException(book.getBookType()); - } + + BookFileExtension extension = BookFileExtension.fromFileName(book.getFileName()) + .orElseThrow(() -> ApiError.UNSUPPORTED_BOOK_TYPE.createException(book.getBookType())); + + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + processor.generateCover(book); + log.info("{}Successfully regenerated cover for book ID {} ({})", progress, book.getId(), title); } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java index 853752730..eaa8759a4 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java @@ -12,9 +12,8 @@ import com.adityachandel.booklore.model.websocket.Topic; import com.adityachandel.booklore.repository.LibraryRepository; import com.adityachandel.booklore.service.NotificationService; import com.adityachandel.booklore.service.appsettings.AppSettingService; -import com.adityachandel.booklore.service.fileprocessor.CbxProcessor; -import com.adityachandel.booklore.service.fileprocessor.EpubProcessor; -import com.adityachandel.booklore.service.fileprocessor.PdfProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry; import com.adityachandel.booklore.service.metadata.extractor.EpubMetadataExtractor; import com.adityachandel.booklore.service.metadata.extractor.PdfMetadataExtractor; import com.adityachandel.booklore.service.monitoring.MonitoringService; @@ -46,9 +45,7 @@ import java.util.Objects; public class FileUploadService { private final LibraryRepository libraryRepository; - private final PdfProcessor pdfProcessor; - private final EpubProcessor epubProcessor; - private final CbxProcessor cbxProcessor; + private final BookFileProcessorRegistry processorRegistry; private final NotificationService notificationService; private final AppSettingService appSettingService; private final PdfMetadataExtractor pdfMetadataExtractor; @@ -178,11 +175,11 @@ public class FileUploadService { .fileName(fileName) .build(); - return switch (fileType) { - case PDF -> pdfProcessor.processFile(libraryFile); - case EPUB -> epubProcessor.processFile(libraryFile); - case CBX -> cbxProcessor.processFile(libraryFile); - }; + BookFileExtension extension = BookFileExtension.fromFileName(fileName) + .orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension")); + + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + return processor.processFile(libraryFile); } } \ No newline at end of file diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/BookDropServiceTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/BookDropServiceTest.java index a9c480284..007a860b0 100644 --- a/booklore-api/src/test/java/com/adityachandel/booklore/service/BookDropServiceTest.java +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/BookDropServiceTest.java @@ -6,12 +6,11 @@ import com.adityachandel.booklore.repository.BookdropFileRepository; import com.adityachandel.booklore.repository.LibraryRepository; import com.adityachandel.booklore.service.bookdrop.BookDropService; import com.adityachandel.booklore.service.bookdrop.BookdropNotificationService; -import com.adityachandel.booklore.service.fileprocessor.CbxProcessor; -import com.adityachandel.booklore.service.fileprocessor.EpubProcessor; -import com.adityachandel.booklore.service.fileprocessor.PdfProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry; import com.adityachandel.booklore.service.metadata.MetadataRefreshService; import com.adityachandel.booklore.service.bookdrop.BookdropMonitoringService; import com.adityachandel.booklore.service.monitoring.MonitoringService; +import com.adityachandel.booklore.service.NotificationService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.core.io.PathResource; @@ -35,9 +34,6 @@ class BookDropServiceTest { private NotificationService notificationService; private MetadataRefreshService metadataRefreshService; private BookdropNotificationService bookdropNotificationService; - private PdfProcessor pdfProcessor; - private EpubProcessor epubProcessor; - private CbxProcessor cbxProcessor; private AppProperties appProperties; private BookDropService bookDropService; @@ -52,9 +48,7 @@ class BookDropServiceTest { notificationService = mock(NotificationService.class); metadataRefreshService = mock(MetadataRefreshService.class); bookdropNotificationService = mock(BookdropNotificationService.class); - pdfProcessor = mock(PdfProcessor.class); - epubProcessor = mock(EpubProcessor.class); - cbxProcessor = mock(CbxProcessor.class); + BookFileProcessorRegistry processorRegistry = mock(BookFileProcessorRegistry.class); appProperties = mock(AppProperties.class); bookDropService = new BookDropService( @@ -62,7 +56,7 @@ class BookDropServiceTest { monitoringService, bookdropMonitoringService, notificationService, metadataRefreshService, bookdropNotificationService, - pdfProcessor, epubProcessor, cbxProcessor, + processorRegistry, appProperties ); } @@ -110,7 +104,7 @@ class BookDropServiceTest { Resource resource = bookDropService.getBookdropCover(bookdropId); assertThat(resource).isInstanceOf(PathResource.class); - assertThat(((PathResource) resource).getFile().getName()).isEqualTo(bookdropId + ".jpg"); + assertThat(resource.getFilename()).isEqualTo(bookdropId + ".jpg"); coverFile.delete(); } From d6c3c8e4c6069ef81105526c51e9d5cbfa4369e1 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 16 Jul 2025 19:30:37 -0700 Subject: [PATCH 07/12] test(service): add unit tests for FileAsBookProcessor to validate library file processing --- .../library/FileAsBookProcessorTest.java | 364 ++++++++++++++++++ 1 file changed, 364 insertions(+) create mode 100644 booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java new file mode 100644 index 000000000..d0e5fe191 --- /dev/null +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java @@ -0,0 +1,364 @@ +package com.adityachandel.booklore.service.library; + +import com.adityachandel.booklore.model.dto.Book; +import com.adityachandel.booklore.model.dto.settings.LibraryFile; +import com.adityachandel.booklore.model.entity.LibraryEntity; +import com.adityachandel.booklore.model.entity.LibraryPathEntity; +import com.adityachandel.booklore.model.enums.BookFileExtension; +import com.adityachandel.booklore.model.enums.BookFileType; +import com.adityachandel.booklore.model.websocket.LogNotification; +import com.adityachandel.booklore.model.websocket.Topic; +import com.adityachandel.booklore.service.NotificationService; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor; +import com.adityachandel.booklore.service.fileprocessor.BookFileProcessorRegistry; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.*; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; + +class FileAsBookProcessorTest { + + @Mock + private NotificationService notificationService; + + @Mock + private BookFileProcessorRegistry processorRegistry; + + @Mock + private BookFileProcessor bookFileProcessor; + + @InjectMocks + private FileAsBookProcessor fileAsBookProcessor; + + @Captor + private ArgumentCaptor bookCaptor; + + private AutoCloseable mocks; + + @BeforeEach + void setUp() { + mocks = MockitoAnnotations.openMocks(this); + } + + @AfterEach + void tearDown() throws Exception { + if (mocks != null) { + mocks.close(); + } + } + + @Test + void processLibraryFiles_shouldProcessAllValidFiles() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + LibraryPathEntity libraryPathEntity = new LibraryPathEntity(); + libraryPathEntity.setPath("/library/path"); + List libraryFiles = new ArrayList<>(); + + LibraryFile file1 = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book1.epub") + .fileSubPath("books") + .bookFileType(BookFileType.EPUB) + .build(); + + LibraryFile file2 = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book2.pdf") + .fileSubPath("books") + .bookFileType(BookFileType.PDF) + .build(); + + libraryFiles.add(file1); + libraryFiles.add(file2); + + Book book1 = Book.builder() + .fileName("book1.epub") + .title("Book 1") + .bookType(BookFileType.EPUB) + .build(); + + Book book2 = Book.builder() + .fileName("book2.pdf") + .title("Book 2") + .bookType(BookFileType.PDF) + .build(); + + when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor); + when(bookFileProcessor.processFile(file1)).thenReturn(book1); + when(bookFileProcessor.processFile(file2)).thenReturn(book2); + + // When + fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + + // Then + verify(notificationService, times(2)).sendMessage(eq(Topic.BOOK_ADD), bookCaptor.capture()); + verify(notificationService, times(2)).sendMessage(eq(Topic.LOG), any(LogNotification.class)); + + List capturedBooks = bookCaptor.getAllValues(); + assertThat(capturedBooks).hasSize(2); + assertThat(capturedBooks).containsExactly(book1, book2); + } + + @Test + void processLibraryFiles_shouldSkipFilesWithUnsupportedExtensions() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + LibraryPathEntity libraryPathEntity = new LibraryPathEntity(); + libraryPathEntity.setPath("/library/path"); + List libraryFiles = new ArrayList<>(); + + LibraryFile validFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book.epub") + .fileSubPath("books") + .bookFileType(BookFileType.EPUB) + .build(); + + LibraryFile invalidFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("document.txt") + .fileSubPath("docs") + .bookFileType(null) + .build(); + + libraryFiles.add(validFile); + libraryFiles.add(invalidFile); + + Book book = Book.builder() + .fileName("book.epub") + .title("Valid Book") + .bookType(BookFileType.EPUB) + .build(); + + when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(bookFileProcessor.processFile(validFile)).thenReturn(book); + + // When + fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + + // Then + verify(notificationService, times(1)).sendMessage(eq(Topic.BOOK_ADD), eq(book)); + verify(notificationService, times(1)).sendMessage(eq(Topic.LOG), any(LogNotification.class)); + verify(processorRegistry, times(1)).getProcessorOrThrow(any()); + } + + @Test + void processLibraryFiles_shouldHandleEmptyList() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + List libraryFiles = new ArrayList<>(); + + // When + fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + + // Then + verify(notificationService, never()).sendMessage(any(Topic.class), any()); + verify(processorRegistry, never()).getProcessorOrThrow(any()); + } + + @Test + void processLibraryFile_shouldReturnNullForUnsupportedFileType() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + LibraryPathEntity libraryPathEntity = new LibraryPathEntity(); + libraryPathEntity.setPath("/library/path"); + + LibraryFile libraryFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("document.txt") + .fileSubPath("docs") + .bookFileType(null) + .build(); + + // When + Book result = fileAsBookProcessor.processLibraryFile(libraryFile); + + // Then + assertThat(result).isNull(); + verify(processorRegistry, never()).getProcessorOrThrow(any()); + } + + @Test + void processLibraryFile_shouldProcessSupportedFileTypes() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + LibraryPathEntity libraryPathEntity = new LibraryPathEntity(); + libraryPathEntity.setPath("/library/path"); + + LibraryFile libraryFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book.epub") + .fileSubPath("books") + .bookFileType(BookFileType.EPUB) + .build(); + + Book expectedBook = Book.builder() + .fileName("book.epub") + .title("Test Book") + .bookType(BookFileType.EPUB) + .build(); + + when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(bookFileProcessor.processFile(libraryFile)).thenReturn(expectedBook); + + // When + Book result = fileAsBookProcessor.processLibraryFile(libraryFile); + + // Then + assertThat(result).isEqualTo(expectedBook); + verify(processorRegistry).getProcessorOrThrow(BookFileExtension.EPUB); + verify(bookFileProcessor).processFile(libraryFile); + } + + @Test + void processLibraryFile_shouldHandleNullReturnFromProcessor() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + LibraryPathEntity libraryPathEntity = new LibraryPathEntity(); + libraryPathEntity.setPath("/library/path"); + + LibraryFile libraryFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book.pdf") + .fileSubPath("books") + .bookFileType(BookFileType.PDF) + .build(); + + when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor); + when(bookFileProcessor.processFile(libraryFile)).thenReturn(null); + + // When + Book result = fileAsBookProcessor.processLibraryFile(libraryFile); + + // Then + assertThat(result).isNull(); + } + + @Test + void processLibraryFiles_shouldNotSendNotificationWhenProcessorReturnsNull() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + LibraryPathEntity libraryPathEntity = new LibraryPathEntity(); + libraryPathEntity.setPath("/library/path"); + List libraryFiles = new ArrayList<>(); + + LibraryFile file = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book.epub") + .fileSubPath("books") + .bookFileType(BookFileType.EPUB) + .build(); + + libraryFiles.add(file); + + when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(bookFileProcessor.processFile(file)).thenReturn(null); + + // When + fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + + // Then + verify(notificationService, never()).sendMessage(any(Topic.class), any()); + } + + @Test + void processLibraryFiles_shouldProcessAllSupportedFileExtensions() { + // Given + LibraryEntity libraryEntity = new LibraryEntity(); + LibraryPathEntity libraryPathEntity = new LibraryPathEntity(); + libraryPathEntity.setPath("/library/path"); + List libraryFiles = new ArrayList<>(); + + LibraryFile epubFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book.epub") + .fileSubPath("books") + .bookFileType(BookFileType.EPUB) + .build(); + + LibraryFile pdfFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("book.pdf") + .fileSubPath("books") + .bookFileType(BookFileType.PDF) + .build(); + + LibraryFile cbzFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("comic.cbz") + .fileSubPath("comics") + .bookFileType(BookFileType.CBX) + .build(); + + LibraryFile cbrFile = LibraryFile.builder() + .libraryEntity(libraryEntity) + .libraryPathEntity(libraryPathEntity) + .fileName("comic.cbr") + .fileSubPath("comics") + .bookFileType(BookFileType.CBX) + .build(); + + libraryFiles.add(epubFile); + libraryFiles.add(pdfFile); + libraryFiles.add(cbzFile); + libraryFiles.add(cbrFile); + + Book epubBook = Book.builder() + .fileName("book.epub") + .bookType(BookFileType.EPUB) + .build(); + + Book pdfBook = Book.builder() + .fileName("book.pdf") + .bookType(BookFileType.PDF) + .build(); + + Book cbzBook = Book.builder() + .fileName("comic.cbz") + .bookType(BookFileType.CBX) + .build(); + + Book cbrBook = Book.builder() + .fileName("comic.cbr") + .bookType(BookFileType.CBX) + .build(); + + when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileExtension.CBZ)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileExtension.CBR)).thenReturn(bookFileProcessor); + + when(bookFileProcessor.processFile(epubFile)).thenReturn(epubBook); + when(bookFileProcessor.processFile(pdfFile)).thenReturn(pdfBook); + when(bookFileProcessor.processFile(cbzFile)).thenReturn(cbzBook); + when(bookFileProcessor.processFile(cbrFile)).thenReturn(cbrBook); + + // When + fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + + // Then + verify(notificationService, times(4)).sendMessage(eq(Topic.BOOK_ADD), any(Book.class)); + verify(notificationService, times(4)).sendMessage(eq(Topic.LOG), any(LogNotification.class)); + } +} \ No newline at end of file From 3f23e4a3eb45e545714abf1558a54bb6df3cc376 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 16 Jul 2025 22:47:17 -0700 Subject: [PATCH 08/12] refactor(services): add LibraryFileProcessorRegistry to get processor based on scan mode --- .../library/LibraryFileProcessorRegistry.java | 21 +++++++++++++++++++ .../library/LibraryProcessingService.java | 11 ++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java new file mode 100644 index 000000000..94591a186 --- /dev/null +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java @@ -0,0 +1,21 @@ +package com.adityachandel.booklore.service.library; + +import com.adityachandel.booklore.model.entity.LibraryEntity; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +@AllArgsConstructor +@Component +public class LibraryFileProcessorRegistry { + + private final FileAsBookProcessor fileAsBookProcessor; + + public LibraryFileProcessor getProcessor(LibraryEntity library) { + return fileAsBookProcessor; + } +} diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java index 85baec5c6..a5f7a5cce 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryProcessingService.java @@ -36,14 +36,15 @@ public class LibraryProcessingService { private final NotificationService notificationService; private final BookRepository bookRepository; private final FileService fileService; - private final FileAsBookProcessor fileAsBookProcessor; + private final LibraryFileProcessorRegistry fileProcessorRegistry; @Transactional public void processLibrary(long libraryId) throws IOException { LibraryEntity libraryEntity = libraryRepository.findById(libraryId).orElseThrow(() -> ApiError.LIBRARY_NOT_FOUND.createException(libraryId)); notificationService.sendMessage(Topic.LOG, createLogNotification("Started processing library: " + libraryEntity.getName())); List libraryFiles = getLibraryFiles(libraryEntity); - fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + LibraryFileProcessor processor = fileProcessorRegistry.getProcessor(libraryEntity); + processor.processLibraryFiles(libraryFiles, libraryEntity); notificationService.sendMessage(Topic.LOG, createLogNotification("Finished processing library: " + libraryEntity.getName())); } @@ -53,12 +54,14 @@ public class LibraryProcessingService { notificationService.sendMessage(Topic.LOG, createLogNotification("Started refreshing library: " + libraryEntity.getName())); List libraryFiles = getLibraryFiles(libraryEntity); deleteRemovedBooks(detectDeletedBookIds(libraryFiles, libraryEntity)); - fileAsBookProcessor.processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity), libraryEntity); + LibraryFileProcessor processor = fileProcessorRegistry.getProcessor(libraryEntity); + processor.processLibraryFiles(detectNewBookPaths(libraryFiles, libraryEntity), libraryEntity); notificationService.sendMessage(Topic.LOG, createLogNotification("Finished refreshing library: " + libraryEntity.getName())); } public void processLibraryFiles(List libraryFiles, LibraryEntity libraryEntity) { - fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity); + LibraryFileProcessor processor = fileProcessorRegistry.getProcessor(libraryEntity); + processor.processLibraryFiles(libraryFiles, libraryEntity); } public static List detectDeletedBookIds(List libraryFiles, LibraryEntity libraryEntity) { From f4207cf9b3d12ba1498f93ff83cbd593fe63cff7 Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Mon, 21 Jul 2025 20:03:33 -0700 Subject: [PATCH 09/12] refactor(services): update BookFileProcessor to use BookFileType for processor retrieval --- .../service/bookdrop/BookDropService.java | 5 +--- .../fileprocessor/BookFileProcessor.java | 4 +-- .../BookFileProcessorRegistry.java | 26 +++++++++---------- .../service/fileprocessor/CbxProcessor.java | 6 ++--- .../service/fileprocessor/EpubProcessor.java | 5 ++-- .../service/fileprocessor/PdfProcessor.java | 5 ++-- .../service/library/FileAsBookProcessor.java | 7 ++--- .../service/metadata/BookMetadataService.java | 5 +--- .../service/upload/FileUploadService.java | 11 +++----- .../library/FileAsBookProcessorTest.java | 24 ++++++++--------- 10 files changed, 42 insertions(+), 56 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java index 3b4d40770..505e842a7 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/bookdrop/BookDropService.java @@ -167,10 +167,7 @@ public class BookDropService { .fileName(fileName) .build(); - BookFileExtension extension = BookFileExtension.fromFileName(fileName) - .orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension")); - - BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(type); return processor.processFile(libraryFile); } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java index b68bf480c..fb6dfa094 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessor.java @@ -3,12 +3,12 @@ package com.adityachandel.booklore.service.fileprocessor; import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; -import com.adityachandel.booklore.model.enums.BookFileExtension; +import com.adityachandel.booklore.model.enums.BookFileType; import java.util.List; public interface BookFileProcessor { - List getSupportedExtensions(); + List getSupportedTypes(); Book processFile(LibraryFile libraryFile); boolean generateCover(BookEntity bookEntity); } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java index 6be568ed8..027fbb997 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/BookFileProcessorRegistry.java @@ -1,6 +1,6 @@ package com.adityachandel.booklore.service.fileprocessor; -import com.adityachandel.booklore.model.enums.BookFileExtension; +import com.adityachandel.booklore.model.enums.BookFileType; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -13,32 +13,32 @@ import java.util.Optional; @Slf4j public class BookFileProcessorRegistry { - private final Map processorMap; + private final Map processorMap; public BookFileProcessorRegistry(List processors) { - this.processorMap = new EnumMap<>(BookFileExtension.class); + this.processorMap = new EnumMap<>(BookFileType.class); initializeProcessorMap(processors); } private void initializeProcessorMap(List processors) { for (BookFileProcessor processor : processors) { - List supportedExtensions = processor.getSupportedExtensions(); - for (BookFileExtension extension : supportedExtensions) { - processorMap.put(extension, processor); - log.debug("Registered {} for extension: {}", processor.getClass().getSimpleName(), extension); + List supportedTypes = processor.getSupportedTypes(); + for (BookFileType type : supportedTypes) { + processorMap.put(type, processor); + log.debug("Registered {} for type: {}", processor.getClass().getSimpleName(), type); } } - log.info("Initialized BookFileProcessorRegistry with {} processors for {} extensions", + log.info("Initialized BookFileProcessorRegistry with {} processors for {} types", processors.size(), processorMap.size()); } - public Optional getProcessor(BookFileExtension extension) { - return Optional.ofNullable(processorMap.get(extension)); + public Optional getProcessor(BookFileType type) { + return Optional.ofNullable(processorMap.get(type)); } - public BookFileProcessor getProcessorOrThrow(BookFileExtension extension) { - return getProcessor(extension) + public BookFileProcessor getProcessorOrThrow(BookFileType type) { + return getProcessor(type) .orElseThrow(() -> new IllegalArgumentException( - "No processor found for file extension: " + extension)); + "No processor found for file type: " + type)); } } \ No newline at end of file diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java index bcdf65d8e..3e0b743dd 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/CbxProcessor.java @@ -1,10 +1,8 @@ package com.adityachandel.booklore.service.fileprocessor; import com.adityachandel.booklore.mapper.BookMapper; -import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; -import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.repository.BookMetadataRepository; import com.adityachandel.booklore.repository.BookRepository; @@ -72,8 +70,8 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce } @Override - public List getSupportedExtensions() { - return List.of(BookFileExtension.CBZ, BookFileExtension.CBR, BookFileExtension.CB7); + public List getSupportedTypes() { + return List.of(BookFileType.CBX); } private Optional extractImagesFromArchive(File 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 961e56129..7664952c0 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 @@ -5,7 +5,6 @@ import com.adityachandel.booklore.model.dto.BookMetadata; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; import com.adityachandel.booklore.model.entity.BookMetadataEntity; -import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.repository.BookMetadataRepository; import com.adityachandel.booklore.repository.BookRepository; @@ -95,8 +94,8 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc } @Override - public List getSupportedExtensions() { - return List.of(BookFileExtension.EPUB); + public List getSupportedTypes() { + return List.of(BookFileType.EPUB); } private void setBookMetadata(BookEntity bookEntity) { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java index 9d5896f47..5a6a98892 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/PdfProcessor.java @@ -4,7 +4,6 @@ import com.adityachandel.booklore.mapper.BookMapper; import com.adityachandel.booklore.model.dto.BookMetadata; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.BookEntity; -import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.repository.BookMetadataRepository; import com.adityachandel.booklore.repository.BookRepository; @@ -71,8 +70,8 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce } @Override - public List getSupportedExtensions() { - return List.of(BookFileExtension.PDF); + public List getSupportedTypes() { + return List.of(BookFileType.PDF); } private void extractAndSetMetadata(BookEntity bookEntity) { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java index 03b8fa247..1cd534131 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/FileAsBookProcessor.java @@ -4,6 +4,7 @@ import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.LibraryEntity; import com.adityachandel.booklore.model.enums.BookFileExtension; +import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.model.websocket.Topic; import com.adityachandel.booklore.service.NotificationService; import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor; @@ -41,13 +42,13 @@ public class FileAsBookProcessor implements LibraryFileProcessor { @Transactional protected Book processLibraryFile(LibraryFile libraryFile) { - BookFileExtension extension = BookFileExtension.fromFileName(libraryFile.getFileName()).orElse(null); - if (extension == null) { + BookFileType type = libraryFile.getBookFileType(); + if (type == null) { log.warn("Unsupported file type for file: {}", libraryFile.getFileName()); return null; } - BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(type); return processor.processFile(libraryFile); } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java index 26d64ead0..d65ca38e5 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/metadata/BookMetadataService.java @@ -194,11 +194,8 @@ public class BookMetadataService { String title = book.getMetadata().getTitle(); String message = progress + "Regenerating cover for: " + title; notificationService.sendMessage(Topic.LOG, createLogNotification(message)); - - BookFileExtension extension = BookFileExtension.fromFileName(book.getFileName()) - .orElseThrow(() -> ApiError.UNSUPPORTED_BOOK_TYPE.createException(book.getBookType())); - BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(book.getBookType()); processor.generateCover(book); log.info("{}Successfully regenerated cover for book ID {} ({})", progress, book.getId(), title); diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java index eaa8759a4..6038d16c0 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/upload/FileUploadService.java @@ -81,7 +81,7 @@ public class FileUploadService { try { file.transferTo(tempPath); - + setTemporaryFileOwnership(tempPath); BookFileExtension fileExt = BookFileExtension.fromFileName(file.getOriginalFilename()).orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension")); @@ -164,21 +164,18 @@ public class FileUploadService { } } - private Book processFile(String fileName, LibraryEntity libraryEntity, LibraryPathEntity libraryPathEntity, File storageFile, BookFileType fileType) { + private Book processFile(String fileName, LibraryEntity libraryEntity, LibraryPathEntity libraryPathEntity, File storageFile, BookFileType type) { String subPath = FileUtils.getRelativeSubPath(libraryPathEntity.getPath(), storageFile.toPath()); LibraryFile libraryFile = LibraryFile.builder() .libraryEntity(libraryEntity) .libraryPathEntity(libraryPathEntity) .fileSubPath(subPath) - .bookFileType(fileType) + .bookFileType(type) .fileName(fileName) .build(); - BookFileExtension extension = BookFileExtension.fromFileName(fileName) - .orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension")); - - BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension); + BookFileProcessor processor = processorRegistry.getProcessorOrThrow(type); return processor.processFile(libraryFile); } diff --git a/booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java b/booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java index d0e5fe191..be80067ce 100644 --- a/booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java +++ b/booklore-api/src/test/java/com/adityachandel/booklore/service/library/FileAsBookProcessorTest.java @@ -4,7 +4,6 @@ import com.adityachandel.booklore.model.dto.Book; import com.adityachandel.booklore.model.dto.settings.LibraryFile; import com.adityachandel.booklore.model.entity.LibraryEntity; import com.adityachandel.booklore.model.entity.LibraryPathEntity; -import com.adityachandel.booklore.model.enums.BookFileExtension; import com.adityachandel.booklore.model.enums.BookFileType; import com.adityachandel.booklore.model.websocket.LogNotification; import com.adityachandel.booklore.model.websocket.Topic; @@ -94,8 +93,8 @@ class FileAsBookProcessorTest { .bookType(BookFileType.PDF) .build(); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor); when(bookFileProcessor.processFile(file1)).thenReturn(book1); when(bookFileProcessor.processFile(file2)).thenReturn(book2); @@ -144,7 +143,7 @@ class FileAsBookProcessorTest { .bookType(BookFileType.EPUB) .build(); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor); when(bookFileProcessor.processFile(validFile)).thenReturn(book); // When @@ -214,7 +213,7 @@ class FileAsBookProcessorTest { .bookType(BookFileType.EPUB) .build(); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor); when(bookFileProcessor.processFile(libraryFile)).thenReturn(expectedBook); // When @@ -222,7 +221,7 @@ class FileAsBookProcessorTest { // Then assertThat(result).isEqualTo(expectedBook); - verify(processorRegistry).getProcessorOrThrow(BookFileExtension.EPUB); + verify(processorRegistry).getProcessorOrThrow(BookFileType.EPUB); verify(bookFileProcessor).processFile(libraryFile); } @@ -241,7 +240,7 @@ class FileAsBookProcessorTest { .bookFileType(BookFileType.PDF) .build(); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor); when(bookFileProcessor.processFile(libraryFile)).thenReturn(null); // When @@ -269,7 +268,7 @@ class FileAsBookProcessorTest { libraryFiles.add(file); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor); when(bookFileProcessor.processFile(file)).thenReturn(null); // When @@ -344,11 +343,10 @@ class FileAsBookProcessorTest { .bookType(BookFileType.CBX) .build(); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.CBZ)).thenReturn(bookFileProcessor); - when(processorRegistry.getProcessorOrThrow(BookFileExtension.CBR)).thenReturn(bookFileProcessor); - + when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor); + when(processorRegistry.getProcessorOrThrow(BookFileType.CBX)).thenReturn(bookFileProcessor); + when(bookFileProcessor.processFile(epubFile)).thenReturn(epubBook); when(bookFileProcessor.processFile(pdfFile)).thenReturn(pdfBook); when(bookFileProcessor.processFile(cbzFile)).thenReturn(cbzBook); From 0cfec8bcd8b4239ae64fb16c08b49cd204bc8ccd Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 23 Jul 2025 12:52:07 -0700 Subject: [PATCH 10/12] style(services): remove unused usings Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../booklore/service/library/LibraryFileProcessorRegistry.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java index 94591a186..8e74c0e0c 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java @@ -4,8 +4,7 @@ import com.adityachandel.booklore.model.entity.LibraryEntity; import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; -import java.util.List; -import java.util.Map; +// Removed unused imports import java.util.function.Function; import java.util.stream.Collectors; From c1796769dc3b4cbd4ec6483248066eebf17b5f7a Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 23 Jul 2025 12:52:42 -0700 Subject: [PATCH 11/12] style(services): remove unused usings Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../booklore/service/library/LibraryFileProcessorRegistry.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java index 8e74c0e0c..84d4840e2 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java @@ -6,8 +6,6 @@ import org.springframework.stereotype.Component; // Removed unused imports import java.util.function.Function; -import java.util.stream.Collectors; - @AllArgsConstructor @Component public class LibraryFileProcessorRegistry { From a5003cb2aa0cfce87e65d3bb2ebe19b412ebebdf Mon Sep 17 00:00:00 2001 From: Alexander Puzynia Date: Wed, 23 Jul 2025 12:53:07 -0700 Subject: [PATCH 12/12] style(services): remove unused usings Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../booklore/service/library/LibraryFileProcessorRegistry.java | 1 - 1 file changed, 1 deletion(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java index 84d4840e2..2463c1043 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/library/LibraryFileProcessorRegistry.java @@ -5,7 +5,6 @@ import lombok.AllArgsConstructor; import org.springframework.stereotype.Component; // Removed unused imports -import java.util.function.Function; @AllArgsConstructor @Component public class LibraryFileProcessorRegistry {