Merged PR 123: Added file storage logs
Added file storage logs
This commit is contained in:
@@ -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) {
|
||||
this.currentLocation = this.videoLocation;
|
||||
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<Path> loadAll() {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user