refactor(services): update BookFileProcessor to use BookFileType for processor retrieval
This commit is contained in:
committed by
Aditya Chandel
parent
3f23e4a3eb
commit
f4207cf9b3
+1
-4
@@ -167,10 +167,7 @@ public class BookDropService {
|
|||||||
.fileName(fileName)
|
.fileName(fileName)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
BookFileExtension extension = BookFileExtension.fromFileName(fileName)
|
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(type);
|
||||||
.orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension"));
|
|
||||||
|
|
||||||
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension);
|
|
||||||
return processor.processFile(libraryFile);
|
return processor.processFile(libraryFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -3,12 +3,12 @@ package com.adityachandel.booklore.service.fileprocessor;
|
|||||||
import com.adityachandel.booklore.model.dto.Book;
|
import com.adityachandel.booklore.model.dto.Book;
|
||||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
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;
|
import java.util.List;
|
||||||
|
|
||||||
public interface BookFileProcessor {
|
public interface BookFileProcessor {
|
||||||
List<BookFileExtension> getSupportedExtensions();
|
List<BookFileType> getSupportedTypes();
|
||||||
Book processFile(LibraryFile libraryFile);
|
Book processFile(LibraryFile libraryFile);
|
||||||
boolean generateCover(BookEntity bookEntity);
|
boolean generateCover(BookEntity bookEntity);
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-13
@@ -1,6 +1,6 @@
|
|||||||
package com.adityachandel.booklore.service.fileprocessor;
|
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 lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -13,32 +13,32 @@ import java.util.Optional;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class BookFileProcessorRegistry {
|
public class BookFileProcessorRegistry {
|
||||||
|
|
||||||
private final Map<BookFileExtension, BookFileProcessor> processorMap;
|
private final Map<BookFileType, BookFileProcessor> processorMap;
|
||||||
|
|
||||||
public BookFileProcessorRegistry(List<BookFileProcessor> processors) {
|
public BookFileProcessorRegistry(List<BookFileProcessor> processors) {
|
||||||
this.processorMap = new EnumMap<>(BookFileExtension.class);
|
this.processorMap = new EnumMap<>(BookFileType.class);
|
||||||
initializeProcessorMap(processors);
|
initializeProcessorMap(processors);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeProcessorMap(List<BookFileProcessor> processors) {
|
private void initializeProcessorMap(List<BookFileProcessor> processors) {
|
||||||
for (BookFileProcessor processor : processors) {
|
for (BookFileProcessor processor : processors) {
|
||||||
List<BookFileExtension> supportedExtensions = processor.getSupportedExtensions();
|
List<BookFileType> supportedTypes = processor.getSupportedTypes();
|
||||||
for (BookFileExtension extension : supportedExtensions) {
|
for (BookFileType type : supportedTypes) {
|
||||||
processorMap.put(extension, processor);
|
processorMap.put(type, processor);
|
||||||
log.debug("Registered {} for extension: {}", processor.getClass().getSimpleName(), extension);
|
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());
|
processors.size(), processorMap.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<BookFileProcessor> getProcessor(BookFileExtension extension) {
|
public Optional<BookFileProcessor> getProcessor(BookFileType type) {
|
||||||
return Optional.ofNullable(processorMap.get(extension));
|
return Optional.ofNullable(processorMap.get(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
public BookFileProcessor getProcessorOrThrow(BookFileExtension extension) {
|
public BookFileProcessor getProcessorOrThrow(BookFileType type) {
|
||||||
return getProcessor(extension)
|
return getProcessor(type)
|
||||||
.orElseThrow(() -> new IllegalArgumentException(
|
.orElseThrow(() -> new IllegalArgumentException(
|
||||||
"No processor found for file extension: " + extension));
|
"No processor found for file type: " + type));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+2
-4
@@ -1,10 +1,8 @@
|
|||||||
package com.adityachandel.booklore.service.fileprocessor;
|
package com.adityachandel.booklore.service.fileprocessor;
|
||||||
|
|
||||||
import com.adityachandel.booklore.mapper.BookMapper;
|
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.dto.settings.LibraryFile;
|
||||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
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.model.enums.BookFileType;
|
||||||
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
||||||
import com.adityachandel.booklore.repository.BookRepository;
|
import com.adityachandel.booklore.repository.BookRepository;
|
||||||
@@ -72,8 +70,8 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BookFileExtension> getSupportedExtensions() {
|
public List<BookFileType> getSupportedTypes() {
|
||||||
return List.of(BookFileExtension.CBZ, BookFileExtension.CBR, BookFileExtension.CB7);
|
return List.of(BookFileType.CBX);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<BufferedImage> extractImagesFromArchive(File file) {
|
private Optional<BufferedImage> extractImagesFromArchive(File file) {
|
||||||
|
|||||||
+2
-3
@@ -5,7 +5,6 @@ import com.adityachandel.booklore.model.dto.BookMetadata;
|
|||||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||||
import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
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.model.enums.BookFileType;
|
||||||
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
||||||
import com.adityachandel.booklore.repository.BookRepository;
|
import com.adityachandel.booklore.repository.BookRepository;
|
||||||
@@ -95,8 +94,8 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BookFileExtension> getSupportedExtensions() {
|
public List<BookFileType> getSupportedTypes() {
|
||||||
return List.of(BookFileExtension.EPUB);
|
return List.of(BookFileType.EPUB);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setBookMetadata(BookEntity bookEntity) {
|
private void setBookMetadata(BookEntity bookEntity) {
|
||||||
|
|||||||
+2
-3
@@ -4,7 +4,6 @@ import com.adityachandel.booklore.mapper.BookMapper;
|
|||||||
import com.adityachandel.booklore.model.dto.BookMetadata;
|
import com.adityachandel.booklore.model.dto.BookMetadata;
|
||||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
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.model.enums.BookFileType;
|
||||||
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
import com.adityachandel.booklore.repository.BookMetadataRepository;
|
||||||
import com.adityachandel.booklore.repository.BookRepository;
|
import com.adityachandel.booklore.repository.BookRepository;
|
||||||
@@ -71,8 +70,8 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<BookFileExtension> getSupportedExtensions() {
|
public List<BookFileType> getSupportedTypes() {
|
||||||
return List.of(BookFileExtension.PDF);
|
return List.of(BookFileType.PDF);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void extractAndSetMetadata(BookEntity bookEntity) {
|
private void extractAndSetMetadata(BookEntity bookEntity) {
|
||||||
|
|||||||
+4
-3
@@ -4,6 +4,7 @@ import com.adityachandel.booklore.model.dto.Book;
|
|||||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||||
import com.adityachandel.booklore.model.enums.BookFileExtension;
|
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.model.websocket.Topic;
|
||||||
import com.adityachandel.booklore.service.NotificationService;
|
import com.adityachandel.booklore.service.NotificationService;
|
||||||
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
|
import com.adityachandel.booklore.service.fileprocessor.BookFileProcessor;
|
||||||
@@ -41,13 +42,13 @@ public class FileAsBookProcessor implements LibraryFileProcessor {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
protected Book processLibraryFile(LibraryFile libraryFile) {
|
protected Book processLibraryFile(LibraryFile libraryFile) {
|
||||||
BookFileExtension extension = BookFileExtension.fromFileName(libraryFile.getFileName()).orElse(null);
|
BookFileType type = libraryFile.getBookFileType();
|
||||||
if (extension == null) {
|
if (type == null) {
|
||||||
log.warn("Unsupported file type for file: {}", libraryFile.getFileName());
|
log.warn("Unsupported file type for file: {}", libraryFile.getFileName());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension);
|
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(type);
|
||||||
return processor.processFile(libraryFile);
|
return processor.processFile(libraryFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-4
@@ -195,10 +195,7 @@ public class BookMetadataService {
|
|||||||
String message = progress + "Regenerating cover for: " + title;
|
String message = progress + "Regenerating cover for: " + title;
|
||||||
notificationService.sendMessage(Topic.LOG, createLogNotification(message));
|
notificationService.sendMessage(Topic.LOG, createLogNotification(message));
|
||||||
|
|
||||||
BookFileExtension extension = BookFileExtension.fromFileName(book.getFileName())
|
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(book.getBookType());
|
||||||
.orElseThrow(() -> ApiError.UNSUPPORTED_BOOK_TYPE.createException(book.getBookType()));
|
|
||||||
|
|
||||||
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension);
|
|
||||||
processor.generateCover(book);
|
processor.generateCover(book);
|
||||||
|
|
||||||
log.info("{}Successfully regenerated cover for book ID {} ({})", progress, book.getId(), title);
|
log.info("{}Successfully regenerated cover for book ID {} ({})", progress, book.getId(), title);
|
||||||
|
|||||||
+3
-6
@@ -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());
|
String subPath = FileUtils.getRelativeSubPath(libraryPathEntity.getPath(), storageFile.toPath());
|
||||||
|
|
||||||
LibraryFile libraryFile = LibraryFile.builder()
|
LibraryFile libraryFile = LibraryFile.builder()
|
||||||
.libraryEntity(libraryEntity)
|
.libraryEntity(libraryEntity)
|
||||||
.libraryPathEntity(libraryPathEntity)
|
.libraryPathEntity(libraryPathEntity)
|
||||||
.fileSubPath(subPath)
|
.fileSubPath(subPath)
|
||||||
.bookFileType(fileType)
|
.bookFileType(type)
|
||||||
.fileName(fileName)
|
.fileName(fileName)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
BookFileExtension extension = BookFileExtension.fromFileName(fileName)
|
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(type);
|
||||||
.orElseThrow(() -> ApiError.INVALID_FILE_FORMAT.createException("Unsupported file extension"));
|
|
||||||
|
|
||||||
BookFileProcessor processor = processorRegistry.getProcessorOrThrow(extension);
|
|
||||||
return processor.processFile(libraryFile);
|
return processor.processFile(libraryFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+10
-12
@@ -4,7 +4,6 @@ import com.adityachandel.booklore.model.dto.Book;
|
|||||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||||
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
import com.adityachandel.booklore.model.entity.LibraryEntity;
|
||||||
import com.adityachandel.booklore.model.entity.LibraryPathEntity;
|
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.enums.BookFileType;
|
||||||
import com.adityachandel.booklore.model.websocket.LogNotification;
|
import com.adityachandel.booklore.model.websocket.LogNotification;
|
||||||
import com.adityachandel.booklore.model.websocket.Topic;
|
import com.adityachandel.booklore.model.websocket.Topic;
|
||||||
@@ -94,8 +93,8 @@ class FileAsBookProcessorTest {
|
|||||||
.bookType(BookFileType.PDF)
|
.bookType(BookFileType.PDF)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor);
|
||||||
when(bookFileProcessor.processFile(file1)).thenReturn(book1);
|
when(bookFileProcessor.processFile(file1)).thenReturn(book1);
|
||||||
when(bookFileProcessor.processFile(file2)).thenReturn(book2);
|
when(bookFileProcessor.processFile(file2)).thenReturn(book2);
|
||||||
|
|
||||||
@@ -144,7 +143,7 @@ class FileAsBookProcessorTest {
|
|||||||
.bookType(BookFileType.EPUB)
|
.bookType(BookFileType.EPUB)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||||
when(bookFileProcessor.processFile(validFile)).thenReturn(book);
|
when(bookFileProcessor.processFile(validFile)).thenReturn(book);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@@ -214,7 +213,7 @@ class FileAsBookProcessorTest {
|
|||||||
.bookType(BookFileType.EPUB)
|
.bookType(BookFileType.EPUB)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||||
when(bookFileProcessor.processFile(libraryFile)).thenReturn(expectedBook);
|
when(bookFileProcessor.processFile(libraryFile)).thenReturn(expectedBook);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@@ -222,7 +221,7 @@ class FileAsBookProcessorTest {
|
|||||||
|
|
||||||
// Then
|
// Then
|
||||||
assertThat(result).isEqualTo(expectedBook);
|
assertThat(result).isEqualTo(expectedBook);
|
||||||
verify(processorRegistry).getProcessorOrThrow(BookFileExtension.EPUB);
|
verify(processorRegistry).getProcessorOrThrow(BookFileType.EPUB);
|
||||||
verify(bookFileProcessor).processFile(libraryFile);
|
verify(bookFileProcessor).processFile(libraryFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,7 +240,7 @@ class FileAsBookProcessorTest {
|
|||||||
.bookFileType(BookFileType.PDF)
|
.bookFileType(BookFileType.PDF)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor);
|
||||||
when(bookFileProcessor.processFile(libraryFile)).thenReturn(null);
|
when(bookFileProcessor.processFile(libraryFile)).thenReturn(null);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@@ -269,7 +268,7 @@ class FileAsBookProcessorTest {
|
|||||||
|
|
||||||
libraryFiles.add(file);
|
libraryFiles.add(file);
|
||||||
|
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||||
when(bookFileProcessor.processFile(file)).thenReturn(null);
|
when(bookFileProcessor.processFile(file)).thenReturn(null);
|
||||||
|
|
||||||
// When
|
// When
|
||||||
@@ -344,10 +343,9 @@ class FileAsBookProcessorTest {
|
|||||||
.bookType(BookFileType.CBX)
|
.bookType(BookFileType.CBX)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.EPUB)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.EPUB)).thenReturn(bookFileProcessor);
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.PDF)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.PDF)).thenReturn(bookFileProcessor);
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.CBZ)).thenReturn(bookFileProcessor);
|
when(processorRegistry.getProcessorOrThrow(BookFileType.CBX)).thenReturn(bookFileProcessor);
|
||||||
when(processorRegistry.getProcessorOrThrow(BookFileExtension.CBR)).thenReturn(bookFileProcessor);
|
|
||||||
|
|
||||||
when(bookFileProcessor.processFile(epubFile)).thenReturn(epubBook);
|
when(bookFileProcessor.processFile(epubFile)).thenReturn(epubBook);
|
||||||
when(bookFileProcessor.processFile(pdfFile)).thenReturn(pdfBook);
|
when(bookFileProcessor.processFile(pdfFile)).thenReturn(pdfBook);
|
||||||
|
|||||||
Reference in New Issue
Block a user