File processing minor refactoring (#751)
* refactor(service): remove finishAndReturnBook to avoid hash calculation twice * refactor(service): remove forceProcess argument cause it always passed as false * refactor(services): merge FileProcessor into BookFileProcessor
This commit is contained in:
+3
-3
@@ -170,9 +170,9 @@ public class BookDropService {
|
||||
.build();
|
||||
|
||||
return switch (type) {
|
||||
case PDF -> pdfProcessor.processFile(libraryFile, false);
|
||||
case EPUB -> epubProcessor.processFile(libraryFile, false);
|
||||
case CBX -> cbxProcessor.processFile(libraryFile, false);
|
||||
case PDF -> pdfProcessor.processFile(libraryFile);
|
||||
case EPUB -> epubProcessor.processFile(libraryFile);
|
||||
case CBX -> cbxProcessor.processFile(libraryFile);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+17
-21
@@ -17,7 +17,7 @@ import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
public abstract class AbstractFileProcessor implements FileProcessor {
|
||||
public abstract class AbstractFileProcessor implements BookFileProcessor {
|
||||
|
||||
protected final BookRepository bookRepository;
|
||||
protected final BookCreatorService bookCreatorService;
|
||||
@@ -37,35 +37,31 @@ public abstract class AbstractFileProcessor implements FileProcessor {
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRES_NEW)
|
||||
@Override
|
||||
public Book processFile(LibraryFile libraryFile, boolean forceProcess) {
|
||||
public Book processFile(LibraryFile libraryFile) {
|
||||
Path filePath = libraryFile.getFullPath();
|
||||
String fileName = filePath.getFileName().toString();
|
||||
String hash = FileUtils.computeFileHash(filePath);
|
||||
|
||||
Optional<Book> existing = fileProcessingUtils.checkForDuplicateAndUpdateMetadataIfNeeded(libraryFile, hash, forceProcess, bookRepository, bookMapper);
|
||||
Optional<Book> existing = fileProcessingUtils.checkForDuplicateAndUpdateMetadataIfNeeded(libraryFile, hash, bookRepository, bookMapper);
|
||||
if (existing.isPresent()) {
|
||||
return existing.get();
|
||||
}
|
||||
if (!forceProcess) {
|
||||
Optional<BookEntity> byName = bookRepository.findBookByFileNameAndLibraryId(fileName, libraryFile.getLibraryEntity().getId());
|
||||
if (byName.isPresent()) {
|
||||
return bookMapper.toBook(byName.get());
|
||||
}
|
||||
Optional<BookEntity> byName = bookRepository.findBookByFileNameAndLibraryId(fileName, libraryFile.getLibraryEntity().getId());
|
||||
if (byName.isPresent()) {
|
||||
return bookMapper.toBook(byName.get());
|
||||
}
|
||||
return processNewFile(libraryFile);
|
||||
|
||||
BookEntity book = processNewFile(libraryFile);
|
||||
book.setCurrentHash(hash);
|
||||
|
||||
Float score = metadataMatchService.calculateMatchScore(book);
|
||||
book.setMetadataMatchScore(score);
|
||||
|
||||
bookCreatorService.saveConnections(book);
|
||||
|
||||
return bookMapper.toBook(book);
|
||||
}
|
||||
|
||||
protected abstract Book processNewFile(LibraryFile libraryFile);
|
||||
protected abstract BookEntity processNewFile(LibraryFile libraryFile);
|
||||
|
||||
protected Book finishAndReturnBook(BookEntity bookEntity) {
|
||||
String hash = FileUtils.computeFileHash(bookEntity);
|
||||
bookEntity.setCurrentHash(hash);
|
||||
|
||||
Float score = metadataMatchService.calculateMatchScore(bookEntity);
|
||||
bookEntity.setMetadataMatchScore(score);
|
||||
|
||||
bookCreatorService.saveConnections(bookEntity);
|
||||
|
||||
return bookMapper.toBook(bookEntity);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,6 +5,6 @@ import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
|
||||
public interface BookFileProcessor {
|
||||
Book processNewFile(LibraryFile libraryFile);
|
||||
Book processFile(LibraryFile libraryFile);
|
||||
boolean generateCover(BookEntity bookEntity);
|
||||
}
|
||||
|
||||
+2
-2
@@ -42,13 +42,13 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book processNewFile(LibraryFile libraryFile) {
|
||||
public BookEntity processNewFile(LibraryFile libraryFile) {
|
||||
BookEntity bookEntity = bookCreatorService.createShellBook(libraryFile, BookFileType.CBX);
|
||||
if (generateCover(bookEntity)) {
|
||||
fileProcessingUtils.setBookCoverPath(bookEntity.getId(), bookEntity.getMetadata());
|
||||
}
|
||||
setMetadata(bookEntity);
|
||||
return finishAndReturnBook(bookEntity);
|
||||
return bookEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-5
@@ -1,7 +1,6 @@
|
||||
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.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
@@ -16,14 +15,12 @@ import com.adityachandel.booklore.util.FileUtils;
|
||||
import io.documentnode.epub4j.domain.Resource;
|
||||
import io.documentnode.epub4j.epub.EpubReader;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -49,13 +46,13 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book processNewFile(LibraryFile libraryFile) {
|
||||
public BookEntity processNewFile(LibraryFile libraryFile) {
|
||||
BookEntity bookEntity = bookCreatorService.createShellBook(libraryFile, BookFileType.EPUB);
|
||||
setBookMetadata(bookEntity);
|
||||
if (generateCover(bookEntity)) {
|
||||
fileProcessingUtils.setBookCoverPath(bookEntity.getId(), bookEntity.getMetadata());
|
||||
}
|
||||
return finishAndReturnBook(bookEntity);
|
||||
return bookEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-3
@@ -7,7 +7,6 @@ import com.adityachandel.booklore.model.entity.BookMetadataEntity;
|
||||
import com.adityachandel.booklore.repository.BookRepository;
|
||||
import com.adityachandel.booklore.service.appsettings.AppSettingService;
|
||||
import com.adityachandel.booklore.util.FileService;
|
||||
import com.adityachandel.booklore.util.FileUtils;
|
||||
import com.adityachandel.booklore.mapper.BookMapper;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -95,13 +94,13 @@ public class FileProcessingUtils {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Optional<Book> checkForDuplicateAndUpdateMetadataIfNeeded(LibraryFile libraryFile, String hash, boolean forceProcess, BookRepository bookRepository, BookMapper bookMapper) {
|
||||
public Optional<Book> checkForDuplicateAndUpdateMetadataIfNeeded(LibraryFile libraryFile, String hash, BookRepository bookRepository, BookMapper bookMapper) {
|
||||
if (StringUtils.isBlank(hash)) {
|
||||
log.warn("Skipping file due to missing hash: {}", libraryFile.getFullPath());
|
||||
return Optional.empty();
|
||||
}
|
||||
Optional<BookEntity> existingByHash = bookRepository.findByCurrentHash(hash);
|
||||
if (existingByHash.isPresent() && !forceProcess) {
|
||||
if (existingByHash.isPresent()) {
|
||||
BookEntity book = existingByHash.get();
|
||||
String fileName = libraryFile.getFullPath().getFileName().toString();
|
||||
if (!book.getFileName().equals(fileName)) {
|
||||
@@ -113,6 +112,7 @@ public class FileProcessingUtils {
|
||||
}
|
||||
return Optional.of(bookMapper.toBook(book));
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.adityachandel.booklore.service.fileprocessor;
|
||||
|
||||
import com.adityachandel.booklore.model.dto.Book;
|
||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
|
||||
public interface FileProcessor {
|
||||
Book processFile(LibraryFile libraryFile, boolean forceProcess);
|
||||
}
|
||||
+2
-3
@@ -1,7 +1,6 @@
|
||||
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.BookMetadata;
|
||||
import com.adityachandel.booklore.model.dto.settings.LibraryFile;
|
||||
import com.adityachandel.booklore.model.entity.BookEntity;
|
||||
@@ -47,13 +46,13 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book processNewFile(LibraryFile libraryFile) {
|
||||
public BookEntity processNewFile(LibraryFile libraryFile) {
|
||||
BookEntity bookEntity = bookCreatorService.createShellBook(libraryFile, BookFileType.PDF);
|
||||
if (generateCover(bookEntity)) {
|
||||
fileProcessingUtils.setBookCoverPath(bookEntity.getId(), bookEntity.getMetadata());
|
||||
}
|
||||
extractAndSetMetadata(bookEntity);
|
||||
return finishAndReturnBook(bookEntity);
|
||||
return bookEntity;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+3
-3
@@ -134,9 +134,9 @@ public class LibraryProcessingService {
|
||||
@Transactional
|
||||
protected Book processLibraryFile(LibraryFile libraryFile) {
|
||||
return switch (libraryFile.getBookFileType()) {
|
||||
case PDF -> pdfProcessor.processFile(libraryFile, false);
|
||||
case EPUB -> epubProcessor.processFile(libraryFile, false);
|
||||
case CBX -> cbxProcessor.processFile(libraryFile, false);
|
||||
case PDF -> pdfProcessor.processFile(libraryFile);
|
||||
case EPUB -> epubProcessor.processFile(libraryFile);
|
||||
case CBX -> cbxProcessor.processFile(libraryFile);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -179,9 +179,9 @@ public class FileUploadService {
|
||||
.build();
|
||||
|
||||
return switch (fileType) {
|
||||
case PDF -> pdfProcessor.processFile(libraryFile, false);
|
||||
case EPUB -> epubProcessor.processFile(libraryFile, false);
|
||||
case CBX -> cbxProcessor.processFile(libraryFile, false);
|
||||
case PDF -> pdfProcessor.processFile(libraryFile);
|
||||
case EPUB -> epubProcessor.processFile(libraryFile);
|
||||
case CBX -> cbxProcessor.processFile(libraryFile);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user