diff --git a/src/main/java/com/skycrate/backend/skycrateBackend/controller/HDFScontroller.java b/src/main/java/com/skycrate/backend/skycrateBackend/controller/HDFScontroller.java index 1510208..ddfd29e 100644 --- a/src/main/java/com/skycrate/backend/skycrateBackend/controller/HDFScontroller.java +++ b/src/main/java/com/skycrate/backend/skycrateBackend/controller/HDFScontroller.java @@ -19,6 +19,11 @@ import java.security.PrivateKey; import java.security.PublicKey; import java.util.List; +import org.springframework.core.io.FileSystemResource; // For FileSystemResource +import org.springframework.core.io.Resource; // For Resource +import org.springframework.http.HttpHeaders; // For HttpHeaders +import java.io.File; // For java.io.File + import static com.skycrate.backend.skycrateBackend.utils.KeyUtil.getPrivateKeyForUser; import static com.skycrate.backend.skycrateBackend.utils.KeyUtil.getPublicKeyForUser; @@ -86,15 +91,31 @@ public class HDFScontroller { @PostMapping("/downloadFile") - public ResponseDTO downloadFile( + public ResponseEntity downloadFile( @RequestParam String hdfsPath, - @RequestParam String localPath, @RequestParam String username) { try { + // Define a temporary local path to download the file + String localPath = "/app/tmp/downloaded/" + new File(hdfsPath).getName(); // Adjust the path as needed + + // Download the file from HDFS to the local path hdfsOperations.downloadFile(hdfsPath, localPath, username); - return new ResponseDTO("File downloaded successfully", true); + + // Create a File object for the downloaded file + File file = new File(localPath); + if (!file.exists()) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(new ResponseDTO("File not found", false)); + } + + // Create a Resource from the file + Resource resource = new FileSystemResource(file); + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") + .body(resource); } catch (Exception e) { - return new ResponseDTO("Failed to download file: " + e.getMessage(), false); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(new ResponseDTO("Failed to download file: " + e.getMessage(), false)); } } @@ -128,4 +149,4 @@ public class HDFScontroller { .body("Failed to list files: " + e.getMessage()); } } -} \ No newline at end of file +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 53d9a87..c462e02 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -17,7 +17,7 @@ spring.jpa.properties.hibernate.format_sql=true spring.jpa.open-in-view=false logging.level.org.springframework.security.config.annotation.authentication.configuration.InitializeUserDetailsBeanManagerConfigurer=ERROR -server.port=8081 +server.port=8080 diff --git a/src/test/java/com/skycrate/backend/skycrateBackend/SkycrateBackendApplicationTests.java b/src/test/java/com/skycrate/backend/skycrateBackend/SkycrateBackendApplicationTests.java deleted file mode 100644 index 8195f98..0000000 --- a/src/test/java/com/skycrate/backend/skycrateBackend/SkycrateBackendApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.skycrate.backend.skycrateBackend; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SkycrateBackendApplicationTests { - - @Test - void contextLoads() { - } - -}