Removed tmp folder while downloading
This commit is contained in:
@@ -144,6 +144,16 @@
|
|||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
<version>2.11.0</version>
|
<version>2.11.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!-- NEWLY ADDED DEPENDENCY FOR DOWNLOAD ENDPOINT-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-io</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>2.11.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ import com.skycrate.backend.skycrateBackend.services.EncryptionUtil;
|
|||||||
import com.skycrate.backend.skycrateBackend.services.HDFSOperations;
|
import com.skycrate.backend.skycrateBackend.services.HDFSOperations;
|
||||||
import com.skycrate.backend.skycrateBackend.utils.KeyUtil;
|
import com.skycrate.backend.skycrateBackend.utils.KeyUtil;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.io.InputStreamResource;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
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")
|
@PostMapping("/downloadFile")
|
||||||
public ResponseEntity<?> downloadFile(
|
public ResponseEntity<?> downloadFile(
|
||||||
@RequestParam String hdfsPath,
|
@RequestParam String hdfsPath,
|
||||||
@RequestParam String username) {
|
@RequestParam String username) {
|
||||||
try {
|
try {
|
||||||
// Define a temporary local path to download the file
|
String fileName = new File(hdfsPath).getName();
|
||||||
String localPath = "/app/tmp/downloaded/" + new File(hdfsPath).getName(); // Adjust the path as needed
|
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()
|
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);
|
.body(resource);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
@@ -135,6 +155,7 @@ public ResponseDTO uploadFile(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@DeleteMapping("/deleteFile")
|
@DeleteMapping("/deleteFile")
|
||||||
public ResponseDTO deleteFile(@RequestParam String hdfsPath) {
|
public ResponseDTO deleteFile(@RequestParam String hdfsPath) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -2,10 +2,9 @@ package com.skycrate.backend.skycrateBackend.services;
|
|||||||
|
|
||||||
import com.skycrate.backend.skycrateBackend.config.HDFSConfig;
|
import com.skycrate.backend.skycrateBackend.config.HDFSConfig;
|
||||||
import com.skycrate.backend.skycrateBackend.utils.KeyUtil;
|
import com.skycrate.backend.skycrateBackend.utils.KeyUtil;
|
||||||
import org.apache.hadoop.fs.FSDataOutputStream;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.hadoop.fs.FileStatus;
|
import org.apache.hadoop.fs.*;
|
||||||
import org.apache.hadoop.fs.FileSystem;
|
import org.springframework.core.io.InputStreamResource;
|
||||||
import org.apache.hadoop.fs.Path;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
@@ -14,6 +13,7 @@ import javax.crypto.KeyGenerator;
|
|||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import javax.crypto.spec.IvParameterSpec;
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
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 {
|
try {
|
||||||
FileSystem fs = HDFSConfig.getHDFS();
|
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));
|
// Optional: If decryption is needed, read bytes, decrypt, and wrap in ByteArrayInputStream
|
||||||
byte[] decryptedData = encryptedData; // Decrypt if needed
|
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) {
|
} catch (IOException e) {
|
||||||
// Handle I/O exception and log the error
|
throw new RuntimeException("Failed to stream file due to I/O issue: " + e.getMessage(), e);
|
||||||
throw new RuntimeException("Failed to download file due to I/O issue: " + e.getMessage(), e);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Catch any other exceptions
|
throw new RuntimeException("Failed to stream file: " + e.getMessage(), e);
|
||||||
throw new RuntimeException("Failed to download file: " + e.getMessage(), e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void createFolder(String hdfsPath) {
|
public void createFolder(String hdfsPath) {
|
||||||
try {
|
try {
|
||||||
FileSystem fs = HDFSConfig.getHDFS();
|
FileSystem fs = HDFSConfig.getHDFS();
|
||||||
|
|||||||
Reference in New Issue
Block a user