From 372a7a04d0838e4f8038c3dda2fce2b1ff0c9f33 Mon Sep 17 00:00:00 2001 From: "andrea.terzani" Date: Sun, 23 Mar 2025 19:53:30 +0100 Subject: [PATCH] Refactor file storage logic to include folder parameter and clean up unused code --- .../FeApi/KsDocumentController.java | 21 +-------- .../apollo/controllers/KSFileController.java | 43 +++++++++++-------- .../services/FileSystemStorageService.java | 24 +++++------ .../apollo/services/StorageService.java | 8 ++-- 4 files changed, 40 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java b/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java index ac01a41..210a4bb 100644 --- a/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java +++ b/src/main/java/com/olympus/apollo/controllers/FeApi/KsDocumentController.java @@ -45,26 +45,7 @@ public class KsDocumentController { } - /* @PostMapping("/downloadKSDocument") - public ResponseEntity 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 downloadFile(@RequestBody KSDocument doc) { diff --git a/src/main/java/com/olympus/apollo/controllers/KSFileController.java b/src/main/java/com/olympus/apollo/controllers/KSFileController.java index 4e8fd5f..968e008 100644 --- a/src/main/java/com/olympus/apollo/controllers/KSFileController.java +++ b/src/main/java/com/olympus/apollo/controllers/KSFileController.java @@ -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); - }*/ + } \ No newline at end of file diff --git a/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java b/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java index 65065b2..b10a8db 100644 --- a/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java +++ b/src/main/java/com/olympus/apollo/services/FileSystemStorageService.java @@ -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; diff --git a/src/main/java/com/olympus/apollo/services/StorageService.java b/src/main/java/com/olympus/apollo/services/StorageService.java index 43510d9..f8aba4f 100644 --- a/src/main/java/com/olympus/apollo/services/StorageService.java +++ b/src/main/java/com/olympus/apollo/services/StorageService.java @@ -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 loadAll();