From dff2de5ddd614b947c60604e52b75d32fcf93a3a Mon Sep 17 00:00:00 2001 From: Atharva Ombase <94031822+atharvaombase@users.noreply.github.com> Date: Sun, 3 Aug 2025 19:27:00 +0530 Subject: [PATCH] Add PasswordForDownload component for secure file access with password protection --- .../src/components/PasswordForDownlaod.jsx | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Frontend/src/components/PasswordForDownlaod.jsx diff --git a/Frontend/src/components/PasswordForDownlaod.jsx b/Frontend/src/components/PasswordForDownlaod.jsx new file mode 100644 index 0000000..f661a1e --- /dev/null +++ b/Frontend/src/components/PasswordForDownlaod.jsx @@ -0,0 +1,94 @@ +import { useState } from "react"; +import PropTypes from "prop-types"; + +const API_URL = import.meta.env.VITE_API_URL; + +const PasswordForDownload = ({ filename, onDownload, onClose }) => { + const [password, setPassword] = useState(""); + const [error, setError] = useState(""); + const [loading, setLoading] = useState(false); + + const handleDownload = async () => { + if (!password) { + setError("Password is required"); + return; + } + setLoading(true); + setError(""); + try { + const response = await fetch(`${API_URL}/api/files/download`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${localStorage.getItem("token")}`, + }, + body: JSON.stringify({ filename, password }), + }); + + if (!response.ok) { + const msg = await response.text(); + throw new Error(msg); + } + + const blob = await response.blob(); + const url = window.URL.createObjectURL(blob); + const a = document.createElement("a"); + a.href = url; + a.download = filename; + document.body.appendChild(a); + a.click(); + a.remove(); + window.URL.revokeObjectURL(url); + + onDownload(); + onClose(); + } catch (err) { + console.error(err); + setError(`Download failed: ${err.message}`); + } finally { + setLoading(false); + } + }; + + return ( +
+
+

Enter Password

+ setPassword(e.target.value)} + /> + {error &&

{error}

} +
+ + +
+
+
+ ); +}; + +PasswordForDownload.propTypes = { + filename: PropTypes.string.isRequired, + onDownload: PropTypes.func.isRequired, + onClose: PropTypes.func.isRequired, +}; + +export default PasswordForDownload;