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") @PostMapping("/downloadKSDocument")
public ResponseEntity<Resource> downloadFile(@RequestBody KSDocument doc) { public ResponseEntity<Resource> downloadFile(@RequestBody KSDocument doc) {

View File

@@ -1,28 +1,36 @@
package com.olympus.apollo.controllers; package com.olympus.apollo.controllers;
import java.util.HashMap;
import java.util.Date;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import com.olympus.dto.ExternalFileIngestionDTO; import org.springframework.beans.factory.annotation.Autowired;
import com.olympus.model.apollo.KSTexts; 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.repository.KSTextsRepository;
import com.olympus.apollo.services.DeletionService; import com.olympus.apollo.services.DeletionService;
import com.olympus.apollo.services.KSIngestor; import com.olympus.apollo.services.KSIngestor;
import org.springframework.beans.factory.annotation.Autowired; import com.olympus.apollo.services.StorageService;
import org.springframework.http.ResponseEntity; import com.olympus.dto.ExternalFileIngestionDTO;
import org.springframework.web.bind.annotation.*; import com.olympus.dto.FileUploadDTO;
import org.springframework.web.multipart.MultipartFile;
import com.olympus.model.apollo.KSDocument; import com.olympus.model.apollo.KSDocument;
import com.olympus.model.apollo.KSIngestionInfo; import com.olympus.model.apollo.KSIngestionInfo;
import com.olympus.apollo.repository.KSDocumentRepository; import com.olympus.model.apollo.KSTexts;
import com.olympus.apollo.repository.KSIngestionInfoRepository;
import com.olympus.apollo.exception.StorageFileNotFoundException;
import com.olympus.apollo.services.StorageService;
import com.olympus.dto.FileUploadDTO;
@RestController @RestController
public class KSFileController { public class KSFileController {
@@ -46,7 +54,7 @@ public class KSFileController {
@RequestParam("file") MultipartFile file, @RequestParam("file") MultipartFile file,
@ModelAttribute FileUploadDTO fileUploadDTO @ModelAttribute FileUploadDTO fileUploadDTO
) { ) {
String filePath = storageService.store(file); String filePath = storageService.store(file,fileUploadDTO.getKsProjectName());
KSDocument ksDocument = new KSDocument(); KSDocument ksDocument = new KSDocument();
ksDocument.setFilePath(filePath); ksDocument.setFilePath(filePath);
@@ -147,8 +155,5 @@ public class KSFileController {
return ResponseEntity.ok("Request In Working"); 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 @Override
public String store(MultipartFile file) { public String store(MultipartFile file, String folder) {
String destinationFileString=null; String destinationFileString = null;
try { try {
if (file.isEmpty()) { if (file.isEmpty()) {
throw new StorageException("Failed to store empty file."); throw new StorageException("Failed to store empty file.");
} }
Path destinationFile = this.rootLocation.resolve( Path folderPath = this.rootLocation.resolve(folder).normalize().toAbsolutePath();
Paths.get(file.getOriginalFilename())) if (!folderPath.startsWith(this.rootLocation.toAbsolutePath())) {
.normalize().toAbsolutePath();
if (!destinationFile.getParent().equals(this.rootLocation.toAbsolutePath())) {
// This is a security check // This is a security check
throw new StorageException( throw new StorageException("Cannot store file outside current directory.");
"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()) { try (InputStream inputStream = file.getInputStream()) {
Files.copy(inputStream, destinationFile, Files.copy(inputStream, destinationFile, StandardCopyOption.REPLACE_EXISTING);
StandardCopyOption.REPLACE_EXISTING); destinationFileString = destinationFile.toString();
destinationFileString=destinationFile.toString();
} }
} } catch (IOException e) {
catch (IOException e) {
throw new StorageException("Failed to store file.", e); throw new StorageException("Failed to store file.", e);
} }
return destinationFileString; return destinationFileString;

View File

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