Support to upload files
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 b735da7..973242f 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.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 @@
}
}
- @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 @@
"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 = "";
+ 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()));
- String uri = ServletUriComponentsBuilder.fromCurrentContextPath()
- .path("/download/")
- .path(name)
- .toUriString();
-
- 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 949d59c..99cc9cb 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.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 @@
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);