diff --git a/pom.xml b/pom.xml
index e61d69e..9b7780a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -144,6 +144,16 @@
commons-io
2.11.0
+
+
+
+
+
+ commons-io
+ commons-io
+ 2.11.0
+
+
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 4c720db..c4a5d98 100644
--- a/src/main/java/com/skycrate/backend/skycrateBackend/controller/HDFScontroller.java
+++ b/src/main/java/com/skycrate/backend/skycrateBackend/controller/HDFScontroller.java
@@ -5,7 +5,9 @@ import com.skycrate.backend.skycrateBackend.services.EncryptionUtil;
import com.skycrate.backend.skycrateBackend.services.HDFSOperations;
import com.skycrate.backend.skycrateBackend.utils.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -106,28 +108,46 @@ public ResponseDTO uploadFile(
// }
+// @PostMapping("/downloadFile")
+// public ResponseEntity> downloadFile(
+// @RequestParam String hdfsPath,
+// @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);
+//
+// // 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 ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
+// .body(new ResponseDTO("Failed to download file: " + e.getMessage(), false));
+// }
+// }
+
@PostMapping("/downloadFile")
public ResponseEntity> downloadFile(
@RequestParam String hdfsPath,
@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
+ String fileName = new File(hdfsPath).getName();
+ InputStreamResource resource = hdfsOperations.downloadFile(hdfsPath, username);
- // Download the file from HDFS to the local path
- hdfsOperations.downloadFile(hdfsPath, localPath, username);
-
- // 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() + "\"")
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+ .contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
@@ -135,6 +155,7 @@ public ResponseDTO uploadFile(
}
}
+
@DeleteMapping("/deleteFile")
public ResponseDTO deleteFile(@RequestParam String hdfsPath) {
try {
diff --git a/src/main/java/com/skycrate/backend/skycrateBackend/services/HDFSOperations.java b/src/main/java/com/skycrate/backend/skycrateBackend/services/HDFSOperations.java
index 85797ec..c2ce149 100644
--- a/src/main/java/com/skycrate/backend/skycrateBackend/services/HDFSOperations.java
+++ b/src/main/java/com/skycrate/backend/skycrateBackend/services/HDFSOperations.java
@@ -2,10 +2,9 @@ package com.skycrate.backend.skycrateBackend.services;
import com.skycrate.backend.skycrateBackend.config.HDFSConfig;
import com.skycrate.backend.skycrateBackend.utils.KeyUtil;
-import org.apache.hadoop.fs.FSDataOutputStream;
-import org.apache.hadoop.fs.FileStatus;
-import org.apache.hadoop.fs.FileSystem;
-import org.apache.hadoop.fs.Path;
+import org.apache.commons.io.IOUtils;
+import org.apache.hadoop.fs.*;
+import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@@ -14,6 +13,7 @@ import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
+import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
@@ -77,27 +77,48 @@ public class HDFSOperations {
}
- public void downloadFile(String hdfsPath, String localPath, String username) {
+// public void downloadFile(String hdfsPath, String localPath, String username) {
+// try {
+// FileSystem fs = HDFSConfig.getHDFS();
+// String tempFilePath = localPath + ".enc";
+//
+// fs.copyToLocalFile(new Path(hdfsPath), new Path(tempFilePath));
+//
+// byte[] encryptedData = Files.readAllBytes(Paths.get(tempFilePath));
+// byte[] decryptedData = encryptedData; // Decrypt if needed
+//
+// Files.write(Paths.get(localPath), decryptedData);
+// Files.delete(Paths.get(tempFilePath));
+// } catch (IOException e) {
+// // Handle I/O exception and log the error
+// throw new RuntimeException("Failed to download file due to I/O issue: " + e.getMessage(), e);
+// } catch (Exception e) {
+// // Catch any other exceptions
+// throw new RuntimeException("Failed to download file: " + e.getMessage(), e);
+// }
+// }
+
+ public InputStreamResource downloadFile(String hdfsPath, String username) {
try {
FileSystem fs = HDFSConfig.getHDFS();
- String tempFilePath = localPath + ".enc";
- fs.copyToLocalFile(new Path(hdfsPath), new Path(tempFilePath));
+ // Read encrypted file directly from HDFS
+ FSDataInputStream hdfsInputStream = fs.open(new Path(hdfsPath));
- byte[] encryptedData = Files.readAllBytes(Paths.get(tempFilePath));
- byte[] decryptedData = encryptedData; // Decrypt if needed
+ // Optional: If decryption is needed, read bytes, decrypt, and wrap in ByteArrayInputStream
+ byte[] encryptedData = IOUtils.toByteArray(hdfsInputStream);
+ byte[] decryptedData = encryptedData; // Apply decryption if needed
+
+ return new InputStreamResource(new ByteArrayInputStream(decryptedData));
- Files.write(Paths.get(localPath), decryptedData);
- Files.delete(Paths.get(tempFilePath));
} catch (IOException e) {
- // Handle I/O exception and log the error
- throw new RuntimeException("Failed to download file due to I/O issue: " + e.getMessage(), e);
+ throw new RuntimeException("Failed to stream file due to I/O issue: " + e.getMessage(), e);
} catch (Exception e) {
- // Catch any other exceptions
- throw new RuntimeException("Failed to download file: " + e.getMessage(), e);
+ throw new RuntimeException("Failed to stream file: " + e.getMessage(), e);
}
}
+
public void createFolder(String hdfsPath) {
try {
FileSystem fs = HDFSConfig.getHDFS();