From 3e1685716815207a0236d1cc999fd4705e5508c5 Mon Sep 17 00:00:00 2001 From: Emanuele Ferrelli Date: Tue, 13 May 2025 15:29:39 +0200 Subject: [PATCH] Added file storage logs --- .../services/FileSystemStorageService.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java b/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java index 1d0a3d1..30b9431 100644 --- a/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java +++ b/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java @@ -12,6 +12,9 @@ import java.util.stream.Stream; import com.olympus.apollo.exception.StorageException; import com.olympus.apollo.exception.StorageFileNotFoundException; import com.olympus.apollo.properties.StorageProperties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; @@ -26,6 +29,8 @@ public class FileSystemStorageService implements StorageService { private final Path videoLocation; private Path currentLocation; + private static final Logger logger = LoggerFactory.getLogger(FileSystemStorageService.class); + @Autowired public FileSystemStorageService(StorageProperties properties) { if(properties.getLocation().trim().length() == 0){ @@ -38,35 +43,54 @@ public class FileSystemStorageService implements StorageService { } public String store(MultipartFile file, String folder, Boolean isVideo) { + logger.info("[STORE] Start uploading file: {}, folder: {}, isVideo: {}", file.getOriginalFilename(), folder, isVideo); + String destinationFileString = null; - if(isVideo){ + + if (isVideo) { this.currentLocation = this.videoLocation; - }else{ + logger.debug("[STORE] Using videoLocation: {}", this.videoLocation); + } else { this.currentLocation = this.defaultLocation; + logger.debug("[STORE] Using defaultLocation: {}", this.defaultLocation); } try { if (file.isEmpty()) { + logger.error("[STORE] File is empty: {}", file.getOriginalFilename()); throw new StorageException("Failed to store empty file."); } + Path folderPath = this.currentLocation.resolve(folder).normalize().toAbsolutePath(); + logger.debug("[STORE] FolderPath resolved: {}", folderPath); + if (!folderPath.startsWith(this.currentLocation.toAbsolutePath())) { - // This is a security check + logger.error("[STORE] Cannot store file outside current directory."); throw new StorageException("Cannot store file outside current directory."); } - Files.createDirectories(folderPath); // Ensure the folder exists + + // Crea la directory se non esiste + Files.createDirectories(folderPath); + logger.debug("[STORE] Created folder: {}", folderPath); Path destinationFile = folderPath.resolve(Paths.get(file.getOriginalFilename())).normalize().toAbsolutePath(); + logger.info("[STORE] Storing file in: {}", destinationFile); + try (InputStream inputStream = file.getInputStream()) { Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING); destinationFileString = destinationFile.toString(); + logger.info("[STORE] Storing successfully completes: {}", destinationFileString); } + } catch (IOException e) { + logger.error("[STORE] Failed to store file: {}", file.getOriginalFilename(), e); throw new StorageException("Failed to store file.", e); } + return destinationFileString; } + @Override public Stream loadAll() { try {