From edb31b77ffbbd8b96b260210985a9bad702546b3 Mon Sep 17 00:00:00 2001 From: Oleh Astappiev <4512729+astappiev@users.noreply.github.com> Date: Sat, 19 Apr 2025 17:46:32 +0200 Subject: [PATCH] fix: uploading books on Windows (dev env) doesn't work --- .../booklore/config/AppProperties.java | 1 - .../booklore/service/FileUploadService.java | 4 ++-- .../service/fileprocessor/FileProcessingUtils.java | 13 +++++-------- booklore-api/src/main/resources/application.yaml | 1 - 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/config/AppProperties.java b/booklore-api/src/main/java/com/adityachandel/booklore/config/AppProperties.java index 3e4a7a89f..acfa4252a 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/config/AppProperties.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/config/AppProperties.java @@ -10,6 +10,5 @@ import org.springframework.stereotype.Component; @Getter @Setter public class AppProperties { - private String pathBook; private String pathConfig; } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/FileUploadService.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/FileUploadService.java index 4959fc978..894c210e9 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/FileUploadService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/FileUploadService.java @@ -55,14 +55,14 @@ public class FileUploadService { } try { - file.transferTo(storageFile); + file.transferTo(storagePath); // storagePath here is required for windows, otherwise it will be resoled relative to Tomcat FS log.info("File uploaded successfully: {}", storageFile.getAbsolutePath()); Book book = processFile(file, libraryEntity, libraryPathEntity, storageFile); notificationService.sendMessage(Topic.BOOK_ADD, book); log.info("Book processed successfully: {}", book.getMetadata().getTitle()); return book; } catch (IOException e) { - log.error("Error saving file: {}", e.getMessage()); + log.error("Error saving file", e); throw ApiError.FILE_READ_ERROR.createException(e.getMessage()); } } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/FileProcessingUtils.java b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/FileProcessingUtils.java index a5a5e2291..df49c04dd 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/FileProcessingUtils.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/service/fileprocessor/FileProcessingUtils.java @@ -1,9 +1,8 @@ package com.adityachandel.booklore.service.fileprocessor; -import com.adityachandel.booklore.config.AppProperties; import com.adityachandel.booklore.model.entity.BookMetadataEntity; -import com.adityachandel.booklore.repository.BookRepository; import com.adityachandel.booklore.service.AppSettingService; +import com.adityachandel.booklore.util.FileService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -26,24 +25,22 @@ import java.util.stream.Stream; @Slf4j public class FileProcessingUtils { - private final AppProperties appProperties; + private final FileService fileService; private final AppSettingService appSettingService; public void setBookCoverPath(long bookId, BookMetadataEntity bookMetadataEntity) { - bookMetadataEntity.setThumbnail(appProperties.getPathConfig() + "/thumbs/" + bookId + "/f.jpg"); + bookMetadataEntity.setThumbnail(fileService.getThumbnailPath(bookId) + "/f.jpg"); bookMetadataEntity.setCoverUpdatedOn(Instant.now()); } public boolean saveCoverImage(BufferedImage coverImage, long bookId) throws IOException { - File coverDirectory = new File(appProperties.getPathConfig() + "/thumbs"); - String resolution = appSettingService.getAppSettings().getCoverSettings().getResolution(); String[] split = resolution.split("x"); int x = Integer.parseInt(split[0]); int y = Integer.parseInt(split[1]); BufferedImage resizedImage = resizeImage(coverImage, x, y); - File bookDirectory = new File(coverDirectory, String.valueOf(bookId)); + File bookDirectory = new File(fileService.getThumbnailPath(bookId)); if (!bookDirectory.exists()) { if (!bookDirectory.mkdirs()) { throw new IOException("Failed to create directory: " + bookDirectory.getAbsolutePath()); @@ -65,7 +62,7 @@ public class FileProcessingUtils { public void deleteBookCovers(Set bookIds) { for (Long bookId : bookIds) { - String bookCoverFolder = appProperties.getPathConfig() + "/thumbs/" + bookId; + String bookCoverFolder = fileService.getThumbnailPath(bookId); Path folderPath = Paths.get(bookCoverFolder); try { if (Files.exists(folderPath) && Files.isDirectory(folderPath)) { diff --git a/booklore-api/src/main/resources/application.yaml b/booklore-api/src/main/resources/application.yaml index dbe9fb6cd..d1140417b 100644 --- a/booklore-api/src/main/resources/application.yaml +++ b/booklore-api/src/main/resources/application.yaml @@ -1,5 +1,4 @@ app: - path-book: '/app/books' path-config: '/app/data' version: 'v0.0.40'