refactor(api): use Path for FileUtils.getBookFullPath (#209)

* refactor(api): use `Path` for FileUtils.getBookFullPath

* test: update tests for new behavior
This commit is contained in:
James Ward
2026-04-22 17:26:53 -04:00
committed by Zach Yale
parent 85c1749a13
commit 97411fa51d
21 changed files with 69 additions and 74 deletions
@@ -59,7 +59,7 @@ public class BookDownloadService {
if (primaryFile == null) {
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
}
Path file = Paths.get(FileUtils.getBookFullPath(bookEntity)).toAbsolutePath().normalize();
Path file = FileUtils.getBookFullPath(bookEntity).toAbsolutePath().normalize();
if (!Files.exists(file)) {
throw ApiError.FAILED_TO_DOWNLOAD_FILE.createException(bookId);
@@ -258,7 +258,7 @@ public class BookDownloadService {
int compressionPercentage = koboSettings.getConversionImageCompressionPercentage();
Path tempDir = null;
try {
File inputFile = new File(FileUtils.getBookFullPath(bookEntity));
File inputFile = FileUtils.getBookFullPath(bookEntity).toFile();
File fileToSend = inputFile;
if (convertCbxToEpub || convertEpubToKepub) {
@@ -338,7 +338,7 @@ public class BookService {
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
filePath = bookFile.getFullFilePath().toString();
} else {
filePath = FileUtils.getBookFullPath(bookEntity);
filePath = FileUtils.getBookFullPath(bookEntity).toString();
}
File file = new File(filePath);
if (!file.exists()) {
@@ -362,7 +362,7 @@ public class BookService {
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
filePath = bookFile.getFullFilePath().toString();
} else {
filePath = FileUtils.getBookFullPath(bookEntity);
filePath = FileUtils.getBookFullPath(bookEntity).toString();
}
Path path = Paths.get(filePath);
@@ -96,7 +96,7 @@ public class SendEmailV2Service {
helper.setTo(recipientEmail);
helper.setSubject("Your Book from Booklore: " + book.getMetadata().getTitle());
helper.setText(generateEmailBody(book.getMetadata().getTitle()));
File bookFile = new File(FileUtils.getBookFullPath(book, bookFileEntity));
File bookFile = FileUtils.getBookFullPath(book, bookFileEntity).toFile();
helper.addAttachment(bookFile.getName(), bookFile);
dynamicMailSender.send(message);
log.info("Book sent successfully to {}", recipientEmail);
@@ -160,7 +160,7 @@ public class AudiobookProcessor extends AbstractFileProcessor implements BookFil
.map(java.nio.file.Path::toFile)
.orElse(null);
} else {
return new File(FileUtils.getBookFullPath(bookEntity));
return FileUtils.getBookFullPath(bookEntity).toFile();
}
}
@@ -72,7 +72,7 @@ public class Azw3Processor extends AbstractFileProcessor implements BookFileProc
@Override
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
try {
File azw3File = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
File azw3File = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
byte[] coverData = azw3MetadataExtractor.extractCover(azw3File);
if (coverData == null || coverData.length == 0) {
@@ -33,6 +33,7 @@ import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.nio.file.Path;
import java.io.InputStream;
import java.util.*;
import java.util.regex.Pattern;
@@ -65,7 +66,7 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
@Override
public BookEntity processNewFile(LibraryFile libraryFile) {
BookEntity bookEntity = bookCreatorService.createShellBook(libraryFile, BookFileType.CBX);
bookEntity.getPrimaryBookFile().setArchiveType(ArchiveUtils.detectArchiveType(new File(FileUtils.getBookFullPath(bookEntity))));
bookEntity.getPrimaryBookFile().setArchiveType(ArchiveUtils.detectArchiveType(FileUtils.getBookFullPath(bookEntity).toFile()));
boolean coverGenerated = generateCover(bookEntity);
if (!coverGenerated) {
var folder = getBookFolderForCoverFallback(libraryFile);
@@ -88,7 +89,7 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
@Override
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
File file = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
File file = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
try {
Optional<BufferedImage> imageOptional = extractImagesFromArchive(file);
if (imageOptional.isPresent()) {
@@ -251,7 +252,7 @@ public class CbxProcessor extends AbstractFileProcessor implements BookFileProce
private void extractAndSetMetadata(BookEntity bookEntity) {
try {
BookMetadata extracted = cbxMetadataExtractor.extractMetadata(new File(FileUtils.getBookFullPath(bookEntity)));
BookMetadata extracted = cbxMetadataExtractor.extractMetadata(FileUtils.getBookFullPath(bookEntity).toFile());
if (extracted == null) {
// Fallback to filename-derived title
setMetadata(bookEntity);
@@ -73,7 +73,7 @@ public class EpubProcessor extends AbstractFileProcessor implements BookFileProc
@Override
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
try {
File epubFile = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
File epubFile = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
byte[] coverData = epubMetadataExtractor.extractCover(epubFile);
if (coverData == null) {
@@ -71,7 +71,7 @@ public class Fb2Processor extends AbstractFileProcessor implements BookFileProce
@Override
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
try {
File fb2File = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
File fb2File = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
byte[] coverData = fb2MetadataExtractor.extractCover(fb2File);
if (coverData == null || coverData.length == 0) {
@@ -72,7 +72,7 @@ public class MobiProcessor extends AbstractFileProcessor implements BookFileProc
@Override
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
try {
File mobiFile = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
File mobiFile = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
byte[] coverData = mobiMetadataExtractor.extractCover(mobiFile);
if (coverData == null || coverData.length == 0) {
@@ -74,7 +74,7 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce
@Override
public boolean generateCover(BookEntity bookEntity, BookFileEntity bookFile) {
File pdfFile = new File(FileUtils.getBookFullPath(bookEntity, bookFile));
File pdfFile = FileUtils.getBookFullPath(bookEntity, bookFile).toFile();
try (RandomAccessReadBufferedFile randomAccessRead = new RandomAccessReadBufferedFile(pdfFile);
PDDocument pdf = Loader.loadPDF(randomAccessRead)) {
return generateCoverImageAndSave(bookEntity.getId(), pdf);
@@ -104,7 +104,7 @@ public class PdfProcessor extends AbstractFileProcessor implements BookFileProce
private void extractAndSetMetadata(BookEntity bookEntity) {
try {
BookMetadata extracted = pdfMetadataExtractor.extractMetadata(new File(FileUtils.getBookFullPath(bookEntity)));
BookMetadata extracted = pdfMetadataExtractor.extractMetadata(FileUtils.getBookFullPath(bookEntity).toFile());
if (StringUtils.isNotBlank(extracted.getTitle())) {
bookEntity.getMetadata().setTitle(truncate(extracted.getTitle(), 1000));
@@ -197,7 +197,7 @@ public class BookMetadataService {
log.info("Unsupported operation for book ID {} - no file or not CBX type", bookId);
return null;
}
return cbxMetadataExtractor.extractMetadata(new File(FileUtils.getBookFullPath(bookEntity)));
return cbxMetadataExtractor.extractMetadata(FileUtils.getBookFullPath(bookEntity).toFile());
}
public BookMetadata getFileMetadata(long bookId) {
@@ -207,7 +207,7 @@ public class BookMetadataService {
if (primaryFile == null) {
throw ApiError.GENERIC_BAD_REQUEST.createException("Book has no file to extract metadata from");
}
return metadataExtractorFactory.extractMetadata(primaryFile.getBookType(), new File(FileUtils.getBookFullPath(bookEntity)));
return metadataExtractorFactory.extractMetadata(primaryFile.getBookType(), FileUtils.getBookFullPath(bookEntity).toFile());
}
@Transactional
@@ -144,8 +144,7 @@ public class CbxReaderService {
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
return bookFile.getFullFilePath();
}
String bookFullPath = FileUtils.getBookFullPath(bookEntity);
return Path.of(bookFullPath);
return FileUtils.getBookFullPath(bookEntity);
}
private void validatePageRequest(Long bookId, int page, List<String> imageEntries) throws FileNotFoundException {
@@ -217,8 +217,7 @@ public class EpubReaderService {
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
return bookFile.getFullFilePath();
}
String bookFullPath = FileUtils.getBookFullPath(bookEntity);
return Path.of(bookFullPath);
return FileUtils.getBookFullPath(bookEntity);
}
private CachedEpubMetadata getCachedMetadata(Path epubPath) throws IOException {
@@ -113,8 +113,7 @@ public class PdfReaderService {
.orElseThrow(() -> ApiError.FILE_NOT_FOUND.createException("No file of type " + bookType + " found for book"));
return bookFile.getFullFilePath();
}
String bookFullPath = FileUtils.getBookFullPath(bookEntity);
return Path.of(bookFullPath);
return FileUtils.getBookFullPath(bookEntity);
}
private void validatePageRequest(Long bookId, int page, int pageCount) throws FileNotFoundException {
@@ -24,27 +24,18 @@ public class FileUtils {
private final String FILE_NOT_FOUND_MESSAGE = "File does not exist: ";
public String getBookFullPath(BookEntity bookEntity) {
public Path getBookFullPath(BookEntity bookEntity) {
BookFileEntity bookFile = bookEntity.getPrimaryBookFile();
if (bookFile == null || bookEntity.getLibraryPath() == null) {
return null;
}
return Path.of(bookEntity.getLibraryPath().getPath(), bookFile.getFileSubPath(), bookFile.getFileName())
.normalize()
.toString()
.replace("\\", "/");
return getBookFullPath(bookEntity, bookFile);
}
public String getBookFullPath(BookEntity bookEntity, BookFileEntity bookFile) {
public Path getBookFullPath(BookEntity bookEntity, BookFileEntity bookFile) {
if (bookFile == null || bookEntity.getLibraryPath() == null) {
return null;
}
return Path.of(bookEntity.getLibraryPath().getPath(), bookFile.getFileSubPath(), bookFile.getFileName())
.normalize()
.toString()
.replace("\\", "/");
.normalize();
}
public String getBookFullPath(Book book) {
@@ -60,7 +51,7 @@ public class FileUtils {
}
public Long getFileSizeInKb(BookEntity bookEntity) {
Path filePath = Path.of(getBookFullPath(bookEntity));
Path filePath = getBookFullPath(bookEntity);
return getFileSizeInKb(filePath);
}