From 5283a2b9f17218101972b97f1bc6103b9e15a14b Mon Sep 17 00:00:00 2001
From: Atharva Ombase <94031822+atharvaombase@users.noreply.github.com>
Date: Sun, 3 Aug 2025 19:26:45 +0530
Subject: [PATCH] Refactor FileList component to open a download modal instead
of direct downloads; added PasswordForDownload component for secure file
access.
---
Frontend/src/components/FileList.jsx | 200 +++++++++++++--------------
1 file changed, 93 insertions(+), 107 deletions(-)
diff --git a/Frontend/src/components/FileList.jsx b/Frontend/src/components/FileList.jsx
index 76e097b..268689f 100644
--- a/Frontend/src/components/FileList.jsx
+++ b/Frontend/src/components/FileList.jsx
@@ -2,6 +2,7 @@ import { useState, useEffect } from "react";
import PropTypes from "prop-types";
import { useDispatch, useSelector } from "react-redux";
import { setCurrentPath } from "../store/pathSlice";
+import PasswordForDownload from "./PasswordForDownload";
import {
FileText,
FileVideo,
@@ -20,16 +21,16 @@ import {
const API_URL = import.meta.env.VITE_API_URL;
-const FileTable = ({ initialPath }) => {
- // Read username dynamically to avoid stale null on first load
+const FileList = ({ initialPath }) => {
const username = localStorage.getItem("username") || "";
const userRoot = `/${username}`;
- // Initialize currentPath only once with the correct userRoot
const [currentPath, setCurrentPathState] = useState(
() => initialPath || userRoot
);
const [files, setFiles] = useState([]);
+ const [showDownloadModal, setShowDownloadModal] = useState(false);
+ const [downloadFilename, setDownloadFilename] = useState("");
const dispatch = useDispatch();
const isUploading = useSelector((state) => state.upload.isUploading);
@@ -41,7 +42,6 @@ const FileTable = ({ initialPath }) => {
const getIcon = (name, type) => {
if (type === "Folder")
return ;
-
const ext = name.split(".").pop().toLowerCase();
switch (ext) {
case "txt":
@@ -111,7 +111,6 @@ const FileTable = ({ initialPath }) => {
: `${API_URL}/api/hdfs/deleteFolder?hdfsPath=${encodeURIComponent(
hdfsPath
)}`;
-
const resp = await fetch(endpoint, { method: "DELETE" });
if (!resp.ok) console.error("Deletion failed:", await resp.text());
fetchFiles();
@@ -136,116 +135,103 @@ const FileTable = ({ initialPath }) => {
setCurrentPathState(parts.length === 0 ? userRoot : `/${parts.join("/")}`);
};
- const handleFileDownload = async (hdfsPath, name, event) => {
- event.stopPropagation();
- try {
- const authToken = localStorage.getItem("token");
- const response = await fetch(
- `${API_URL}/api/hdfs/downloadFile?hdfsEncPath=${encodeURIComponent(
- hdfsPath
- )}&localPath=${name}&username=${username}`,
- {
- method: "POST",
- headers: { Authorization: `Bearer ${authToken}` },
- }
- );
-
- if (!response.ok) throw new Error(await response.text());
- const blob = await response.blob();
- const url = window.URL.createObjectURL(blob);
- const link = document.createElement("a");
- link.href = url;
- link.download = name;
- document.body.appendChild(link);
- link.click();
- link.remove();
- window.URL.revokeObjectURL(url);
- fetchFiles();
- } catch (error) {
- console.error("Download failed:", error);
- alert("Something went wrong while downloading the file.");
- }
+ // open modal instead of direct download
+ const openDownloadModal = (name, e) => {
+ e.stopPropagation();
+ setDownloadFilename(name);
+ setShowDownloadModal(true);
};
return (
-
-
-
Path: {currentPath}
- {currentPath !== userRoot && (
-
- )}
+ <>
+
+
+
Path: {currentPath}
+ {currentPath !== userRoot && (
+
+ )}
+
+
+
+
+
+ | Name |
+ Actions |
+
+
+
+ {files.length === 0 ? (
+
+ |
+ No files found.
+ |
+
+ ) : (
+ files.map((entry, idx) => {
+ const name = getName(entry);
+ const type = getType(entry);
+ // const hdfsPath = `${currentPath}/${name}`;
+ return (
+ handleOpenFolder(name)
+ : undefined
+ }
+ className={`even:bg-blue-50 odd:bg-white border-b border-blue-100 transition hover:bg-blue-100 ${
+ type === "Folder" ? "cursor-pointer" : ""
+ }`}
+ >
+ |
+ {getIcon(name, type)}
+ {name}
+ |
+
+ {isFile(entry) && (
+
+ )}
+
+ |
+
+ );
+ })
+ )}
+
+
-
-
-
- | Name |
- Actions |
-
-
-
- {files.length === 0 ? (
-
- |
- No files found.
- |
-
- ) : (
- files.map((entry, idx) => {
- const name = getName(entry);
- const type = getType(entry);
- const hdfsPath = `${currentPath}/${name}`;
-
- return (
- handleOpenFolder(name) : undefined
- }
- className={`even:bg-blue-50 odd:bg-white border-b border-blue-100 transition hover:bg-blue-100 ${
- type === "Folder" ? "cursor-pointer" : ""
- }`}
- >
- |
- {getIcon(name, type)}
- {name}
- |
-
- {isFile(entry) && (
-
- )}
-
- |
-
- );
- })
- )}
-
-
-
+ {showDownloadModal && (
+
setShowDownloadModal(false)}
+ />
+ )}
+ >
);
};
-FileTable.propTypes = {
+FileList.propTypes = {
initialPath: PropTypes.string,
};
-export default FileTable;
+export default FileList;