|
| 1 | +package org.apache.airavata.file.server.controller; |
| 2 | + |
| 3 | +import org.apache.airavata.file.server.model.AiravataDirectory; |
| 4 | +import org.apache.airavata.file.server.model.FileUploadResponse; |
| 5 | +import org.apache.airavata.file.server.service.AirvataFileService; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.core.io.Resource; |
| 10 | +import org.springframework.core.io.UrlResource; |
| 11 | +import org.springframework.http.HttpHeaders; |
| 12 | +import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.stereotype.Controller; |
| 14 | +import org.springframework.web.bind.annotation.*; |
| 15 | +import org.springframework.web.multipart.MultipartFile; |
| 16 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.nio.file.Path; |
| 20 | + |
| 21 | +@Controller |
| 22 | +public class FileController { |
| 23 | + |
| 24 | + private final static Logger logger = LoggerFactory.getLogger(FileController.class); |
| 25 | + |
| 26 | + @Autowired |
| 27 | + private AirvataFileService fileService; |
| 28 | + |
| 29 | + @GetMapping("/list/{live}/{processId}") |
| 30 | + @ResponseBody |
| 31 | + public Object listFilesRoot(@PathVariable String live, |
| 32 | + @PathVariable String processId) throws Exception { |
| 33 | + try { |
| 34 | + return fileService.listFiles(processId, ""); |
| 35 | + } catch (Exception e) { |
| 36 | + logger.error("Failed to list files for path {} in process {}", "root path", processId, e); |
| 37 | + throw e; |
| 38 | + } |
| 39 | + } |
| 40 | + @GetMapping("/list/{live}/{processId}/{subPath}") |
| 41 | + @ResponseBody |
| 42 | + public Object listFiles(@PathVariable String live, |
| 43 | + @PathVariable String processId, |
| 44 | + @PathVariable String subPath) throws Exception { |
| 45 | + try { |
| 46 | + return fileService.listFiles(processId, subPath); |
| 47 | + } catch (Exception e) { |
| 48 | + logger.error("Failed to list files for path {} in process {}", subPath, processId, e); |
| 49 | + throw e; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("/download/{isLive}/{processId}/{subPath}") |
| 54 | + @ResponseBody |
| 55 | + public ResponseEntity downloadFile(@PathVariable String isLive, |
| 56 | + @PathVariable String processId, |
| 57 | + @PathVariable String subPath) { |
| 58 | + |
| 59 | + try { |
| 60 | + Path localPath = fileService.downloadFile(processId, subPath); |
| 61 | + Resource resource = new UrlResource(localPath.toUri()); |
| 62 | + return ResponseEntity.ok() |
| 63 | + .header(HttpHeaders.CONTENT_DISPOSITION, |
| 64 | + "attachment; filename=\"" + new File(subPath).getName() + "\"") |
| 65 | + .body(resource); |
| 66 | + } catch (Exception e) { |
| 67 | + return ResponseEntity.internalServerError() |
| 68 | + .body("An internal server error occurred: " + e.getMessage()); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + @PostMapping("/upload-file") |
| 74 | + @ResponseBody |
| 75 | + public FileUploadResponse uploadFile(@RequestParam("file") MultipartFile file) { |
| 76 | + String name = ""; |
| 77 | + |
| 78 | + String uri = ServletUriComponentsBuilder.fromCurrentContextPath() |
| 79 | + .path("/download/") |
| 80 | + .path(name) |
| 81 | + .toUriString(); |
| 82 | + |
| 83 | + return new FileUploadResponse(name, uri, file.getContentType(), file.getSize()); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments