mirror of
https://github.com/AmanTahiliani/PeerNotes.git
synced 2026-08-07 11:55:12 -04:00
add download functionality
This commit is contained in:
@@ -54,7 +54,7 @@ def send():
|
||||
)
|
||||
|
||||
|
||||
@app.route("/request", methods=["GET"])
|
||||
@app.route("/request", methods=["POST"])
|
||||
def request_file():
|
||||
data = request.json
|
||||
file_id = data["id"]
|
||||
@@ -64,7 +64,7 @@ def request_file():
|
||||
url = f"http://{ip}:8080/send"
|
||||
params = {
|
||||
"id": file_id,
|
||||
"filename": filename,
|
||||
"filename": filename.replace(" ", "_"),
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
|
||||
@@ -16,7 +16,7 @@ export const usePoll = () => {
|
||||
|
||||
const pollServer = async () => {
|
||||
// request private ip from local api
|
||||
return fetch('http://127.0.0.1:8080/ip')
|
||||
return fetch('http://localhost:8080/ip')
|
||||
.then(response => response.text())
|
||||
.then((data) => {
|
||||
// send private ip to central server
|
||||
|
||||
@@ -72,7 +72,7 @@ const Results: React.FC = () => {
|
||||
<td>{file.upvotes.length}</td>
|
||||
<td>{file.downvotes.length}</td>
|
||||
<td>{file.original_author.username}</td>
|
||||
<td><a href="">Download</a></td>
|
||||
<td><DownloadButton file={file} /></td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
@@ -86,3 +86,36 @@ const Results: React.FC = () => {
|
||||
};
|
||||
|
||||
export default Results;
|
||||
|
||||
|
||||
function DownloadButton({ file }: { file: File }) {
|
||||
const [success, setSuccess] = useState(false);
|
||||
const handleDownload = () => {
|
||||
// Implement download functionality
|
||||
fetch("http://localhost:8080/request", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({ id: file.id, filename: file.filename, ip: file.original_author.ip_address }),
|
||||
})
|
||||
.then((response) => response.text())
|
||||
.then((data) => {
|
||||
console.log("Success download response data:", data);
|
||||
setSuccess(true);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error("Error:", error);
|
||||
setSuccess(false);
|
||||
});
|
||||
};
|
||||
return (
|
||||
<>
|
||||
{
|
||||
success === true ?
|
||||
<p>Downloaded!</p> :
|
||||
<button onClick={handleDownload}>Download</button>
|
||||
}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user