This is an automated email from the ASF dual-hosted git repository. lahirujayathilake pushed a commit to branch agent-framewok-refactoring in repository https://gitbox.apache.org/repos/asf/airavata.git
commit e29842fcc115fa0dcc8edfbe72206c594c45a4b0 Author: DImuthuUpe <[email protected]> AuthorDate: Mon Dec 16 09:17:40 2024 -0500 Support to upload files --- .../file/server/controller/FileController.java | 32 ++++++++++++---------- .../file/server/service/AirvataFileService.java | 29 ++++++++++++++++++-- 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/modules/file-server/src/main/java/org/apache/airavata/file/server/controller/FileController.java b/modules/file-server/src/main/java/org/apache/airavata/file/server/controller/FileController.java index b735da7765..973242f39d 100644 --- a/modules/file-server/src/main/java/org/apache/airavata/file/server/controller/FileController.java +++ b/modules/file-server/src/main/java/org/apache/airavata/file/server/controller/FileController.java @@ -13,7 +13,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import java.io.File; import java.nio.file.Path; @@ -50,9 +49,9 @@ public class FileController { } } - @GetMapping("/download/{isLive}/{processId}/{subPath}") + @GetMapping("/download/{live}/{processId}/{subPath}") @ResponseBody - public ResponseEntity downloadFile(@PathVariable String isLive, + public ResponseEntity downloadFile(@PathVariable String live, @PathVariable String processId, @PathVariable String subPath) { @@ -64,24 +63,27 @@ public class FileController { "attachment; filename=\"" + new File(subPath).getName() + "\"") .body(resource); } catch (Exception e) { + logger.error("Failed to download file {} from process {}", subPath, processId, e); return ResponseEntity.internalServerError() .body("An internal server error occurred: " + e.getMessage()); } } - - @PostMapping("/upload-file") + @PostMapping("/upload/{live}/{processId}/{subPath}") @ResponseBody - public FileUploadResponse uploadFile(@RequestParam("file") MultipartFile file) { - String name = ""; - - String uri = ServletUriComponentsBuilder.fromCurrentContextPath() - .path("/download/") - .path(name) - .toUriString(); + public ResponseEntity uploadFile(@PathVariable String live, + @PathVariable String processId, + @PathVariable String subPath, + @RequestParam("file") MultipartFile file) { + try { + String name = file.getName(); + fileService.uploadFile(processId, subPath, file); + return ResponseEntity.ok(new FileUploadResponse(name, subPath, file.getContentType(), file.getSize())); - return new FileUploadResponse(name, uri, file.getContentType(), file.getSize()); + } catch (Exception e) { + logger.error("Failed to upload file {} to process {}", subPath, processId, e); + return ResponseEntity.internalServerError() + .body("An internal server error occurred: " + e.getMessage()); + } } - - } diff --git a/modules/file-server/src/main/java/org/apache/airavata/file/server/service/AirvataFileService.java b/modules/file-server/src/main/java/org/apache/airavata/file/server/service/AirvataFileService.java index 949d59c77b..99cc9cb7e0 100644 --- a/modules/file-server/src/main/java/org/apache/airavata/file/server/service/AirvataFileService.java +++ b/modules/file-server/src/main/java/org/apache/airavata/file/server/service/AirvataFileService.java @@ -11,11 +11,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardCopyOption; import java.util.List; @Service @@ -58,6 +58,31 @@ public class AirvataFileService { return airavataDirectory; } + + public void uploadFile(String processId, String subPath, MultipartFile file) throws Exception { + + Path tempFile = Files.createTempFile("tempfile_", ".data"); + tempFile.toFile().deleteOnExit(); + Files.copy(file.getInputStream(), tempFile, StandardCopyOption.REPLACE_EXISTING); + + ProcessDataManager dataManager = new ProcessDataManager(registryClientPool, processId, adaptorSupport); + + AgentAdaptor agentAdaptor = getAgentAdaptor(dataManager, processId); + subPath = dataManager.getBaseDir() + (subPath.isEmpty()? "" : "/" + subPath); + + try { + agentAdaptor.uploadFile(tempFile.toFile().getAbsolutePath(), subPath); + } catch (Exception e) { + logger.error("Failed to upload file {} from local path to process path {}", + tempFile.toFile().getAbsolutePath(), subPath); + try { + tempFile.toFile().delete(); + } catch (Exception ignore) { + // Ignore + } + throw e; + } + } public Path downloadFile(String processId, String subPath) throws Exception { ProcessDataManager dataManager = new ProcessDataManager(registryClientPool, processId, adaptorSupport);
