refactor(library): extract processLibraryFiles into separate FileAsBookProcessor class
This commit is contained in:
committed by
Aditya Chandel
parent
f084bb1da6
commit
c9e05b1c28
+52
@@ -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<LibraryFile> 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);
|
||||
};
|
||||
}
|
||||
}
|
||||
+10
@@ -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<LibraryFile> libraryFiles, LibraryEntity libraryEntity);
|
||||
}
|
||||
+7
-30
@@ -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<LibraryFile> 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<LibraryFile> 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<LibraryFile> libraryFiles, LibraryEntity libraryEntity) {
|
||||
fileAsBookProcessor.processLibraryFiles(libraryFiles, libraryEntity);
|
||||
}
|
||||
|
||||
public static List<Long> detectDeletedBookIds(List<LibraryFile> libraryFiles, LibraryEntity libraryEntity) {
|
||||
Set<String> currentFileNames = libraryFiles.stream()
|
||||
.map(LibraryFile::getFileName)
|
||||
@@ -119,27 +117,6 @@ public class LibraryProcessingService {
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void processLibraryFiles(List<LibraryFile> 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<LibraryFile> getLibraryFiles(LibraryEntity libraryEntity) throws IOException {
|
||||
List<LibraryFile> allFiles = new ArrayList<>();
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user