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.StorageException;
|
||||||
import com.olympus.apollo.exception.StorageFileNotFoundException;
|
import com.olympus.apollo.exception.StorageFileNotFoundException;
|
||||||
import com.olympus.apollo.properties.StorageProperties;
|
import com.olympus.apollo.properties.StorageProperties;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.core.io.UrlResource;
|
import org.springframework.core.io.UrlResource;
|
||||||
@@ -26,6 +29,8 @@ public class FileSystemStorageService implements StorageService {
|
|||||||
private final Path videoLocation;
|
private final Path videoLocation;
|
||||||
private Path currentLocation;
|
private Path currentLocation;
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(FileSystemStorageService.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public FileSystemStorageService(StorageProperties properties) {
|
public FileSystemStorageService(StorageProperties properties) {
|
||||||
if(properties.getLocation().trim().length() == 0){
|
if(properties.getLocation().trim().length() == 0){
|
||||||
@@ -38,35 +43,54 @@ public class FileSystemStorageService implements StorageService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String store(MultipartFile file, String folder, Boolean isVideo) {
|
public String store(MultipartFile file, String folder, Boolean isVideo) {
|
||||||
|
logger.info("[STORE] Start uploading file: {}, folder: {}, isVideo: {}", file.getOriginalFilename(), folder, isVideo);
|
||||||
|
|
||||||
String destinationFileString = null;
|
String destinationFileString = null;
|
||||||
if(isVideo){
|
|
||||||
|
if (isVideo) {
|
||||||
this.currentLocation = this.videoLocation;
|
this.currentLocation = this.videoLocation;
|
||||||
}else{
|
logger.debug("[STORE] Using videoLocation: {}", this.videoLocation);
|
||||||
|
} else {
|
||||||
this.currentLocation = this.defaultLocation;
|
this.currentLocation = this.defaultLocation;
|
||||||
|
logger.debug("[STORE] Using defaultLocation: {}", this.defaultLocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (file.isEmpty()) {
|
if (file.isEmpty()) {
|
||||||
|
logger.error("[STORE] File is empty: {}", file.getOriginalFilename());
|
||||||
throw new StorageException("Failed to store empty file.");
|
throw new StorageException("Failed to store empty file.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Path folderPath = this.currentLocation.resolve(folder).normalize().toAbsolutePath();
|
Path folderPath = this.currentLocation.resolve(folder).normalize().toAbsolutePath();
|
||||||
|
logger.debug("[STORE] FolderPath resolved: {}", folderPath);
|
||||||
|
|
||||||
if (!folderPath.startsWith(this.currentLocation.toAbsolutePath())) {
|
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.");
|
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();
|
Path destinationFile = folderPath.resolve(Paths.get(file.getOriginalFilename())).normalize().toAbsolutePath();
|
||||||
|
logger.info("[STORE] Storing file in: {}", destinationFile);
|
||||||
|
|
||||||
try (InputStream inputStream = file.getInputStream()) {
|
try (InputStream inputStream = file.getInputStream()) {
|
||||||
Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING);
|
Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING);
|
||||||
destinationFileString = destinationFile.toString();
|
destinationFileString = destinationFile.toString();
|
||||||
|
logger.info("[STORE] Storing successfully completes: {}", destinationFileString);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
logger.error("[STORE] Failed to store file: {}", file.getOriginalFilename(), e);
|
||||||
throw new StorageException("Failed to store file.", e);
|
throw new StorageException("Failed to store file.", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return destinationFileString;
|
return destinationFileString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Stream<Path> loadAll() {
|
public Stream<Path> loadAll() {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user