Refactor file storage logic to include folder parameter and clean up unused code

This commit is contained in:
andrea.terzani
2025-03-23 19:53:30 +01:00
parent d6b5458e59
commit 372a7a04d0
4 changed files with 40 additions and 56 deletions

View File

@@ -45,26 +45,7 @@ public class KsDocumentController {
}
/* @PostMapping("/downloadKSDocument")
public ResponseEntity<Resource> downloadFile(@RequestBody KSDocument doc) {
try {
// Percorso al file
Path filePath = Paths.get(doc.getFilePath()).normalize();
Resource resource = new UrlResource(filePath.toUri());
if (!resource.exists()) {
return ResponseEntity.notFound().build();
}
// Configurazione della risposta HTTP
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}*/
@PostMapping("/downloadKSDocument")
public ResponseEntity<Resource> downloadFile(@RequestBody KSDocument doc) {

View File

@@ -1,28 +1,36 @@
package com.olympus.apollo.controllers;
import java.util.HashMap;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import com.olympus.dto.ExternalFileIngestionDTO;
import com.olympus.model.apollo.KSTexts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.olympus.apollo.exception.StorageFileNotFoundException;
import com.olympus.apollo.repository.KSDocumentRepository;
import com.olympus.apollo.repository.KSIngestionInfoRepository;
import com.olympus.apollo.repository.KSTextsRepository;
import com.olympus.apollo.services.DeletionService;
import com.olympus.apollo.services.KSIngestor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.olympus.apollo.services.StorageService;
import com.olympus.dto.ExternalFileIngestionDTO;
import com.olympus.dto.FileUploadDTO;
import com.olympus.model.apollo.KSDocument;
import com.olympus.model.apollo.KSIngestionInfo;
import com.olympus.apollo.repository.KSDocumentRepository;
import com.olympus.apollo.repository.KSIngestionInfoRepository;
import com.olympus.apollo.exception.StorageFileNotFoundException;
import com.olympus.apollo.services.StorageService;
import com.olympus.dto.FileUploadDTO;
import com.olympus.model.apollo.KSTexts;
@RestController
public class KSFileController {
@@ -46,7 +54,7 @@ public class KSFileController {
@RequestParam("file") MultipartFile file,
@ModelAttribute FileUploadDTO fileUploadDTO
) {
String filePath = storageService.store(file);
String filePath = storageService.store(file,fileUploadDTO.getKsProjectName());
KSDocument ksDocument = new KSDocument();
ksDocument.setFilePath(filePath);
@@ -147,8 +155,5 @@ public class KSFileController {
return ResponseEntity.ok("Request In Working");
}
/*@GetMapping("/ingest_texts/{id}")
public IngestionOutput ingestDocumentById(@PathVariable String id) {
return ksIngestor.ingestTextById(id);
}*/
}

View File

@@ -35,28 +35,26 @@ public class FileSystemStorageService implements StorageService {
}
@Override
public String store(MultipartFile file) {
String destinationFileString=null;
public String store(MultipartFile file, String folder) {
String destinationFileString = null;
try {
if (file.isEmpty()) {
throw new StorageException("Failed to store empty file.");
}
Path destinationFile = this.rootLocation.resolve(
Paths.get(file.getOriginalFilename()))
.normalize().toAbsolutePath();
if (!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())) {
Path folderPath = this.rootLocation.resolve(folder).normalize().toAbsolutePath();
if (!folderPath.startsWith(this.rootLocation.toAbsolutePath())) {
// This is a security check
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
Path destinationFile = folderPath.resolve(Paths.get(file.getOriginalFilename())).normalize().toAbsolutePath();
try (InputStream inputStream = file.getInputStream()) {
Files.copy(inputStream, destinationFile,
StandardCopyOption.REPLACE_EXISTING);
destinationFileString=destinationFile.toString();
Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING);
destinationFileString = destinationFile.toString();
}
}
catch (IOException e) {
} catch (IOException e) {
throw new StorageException("Failed to store file.", e);
}
return destinationFileString;

View File

@@ -1,16 +1,16 @@
package com.olympus.apollo.services;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.core.io.Resource;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.springframework.core.io.Resource;
import org.springframework.web.multipart.MultipartFile;
public interface StorageService {
void init();
String store(MultipartFile file);
String store(MultipartFile file,String folder);
Stream<Path> loadAll();