Removed tmp folder while downloading

This commit is contained in:
vedang29
2025-04-15 22:47:28 +05:30
parent 508405077d
commit a1dc9a840e
3 changed files with 82 additions and 30 deletions
+10
View File
@@ -144,6 +144,16 @@
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<!-- NEWLY ADDED DEPENDENCY FOR DOWNLOAD ENDPOINT-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
<build>
@@ -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 {
@@ -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();