diff --git a/src/public/js/download.js b/src/public/js/download.js index 6d5cf9b..4aa874c 100644 --- a/src/public/js/download.js +++ b/src/public/js/download.js @@ -3,7 +3,37 @@ import { RSA_dec_data as RSA_dec } from "./rsa.js"; window.addEventListener("load", () => main()); const main = () => { - const pub_key = window.location.hash.slice(1).split(":"); - const hash = window.location.pathname.split("/").pop(); - console.log(pub_key, hash); + /* Handle when a file is added to the input element */ + const button = document.getElementById("download"); + button.addEventListener("click", () => { + const req = new XMLHttpRequest(); + req.open("POST", "/api/download"); + req.setRequestHeader("Content-Type", "application/json"); + + const pub_key = window.location.hash + .slice(1) + .split(":") + .map((v) => BigInt(v)); + const hash = window.location.pathname.split("/").pop(); + + // Send the hash to the server + req.send(JSON.stringify(hash)); + + req.onload = () => { + if (req.status == 200) { + // Decrypt the file + const decrypted_file = RSA_dec( + req.responseText + .slice(1, -2) + .split(",") + .map((v) => BigInt(v)), + pub_key + ); + + console.log(decrypted_file); + } else { + console.error("Download failed."); + } + }; + }); }; diff --git a/src/public/styles/style.css b/src/public/styles/style.css index bd42cc0..c93a953 100644 --- a/src/public/styles/style.css +++ b/src/public/styles/style.css @@ -73,7 +73,8 @@ main { } .upload-area, -.link-area { +.link-area, +.download-area { border: 2px dashed var(--border); height: 70vh; width: 65vw; @@ -124,7 +125,8 @@ main { outline: none; } -.link-area > button { +.link-area > button, +.download-area > button { border-radius: 6px; border: 2px solid var(--shadow); width: 50%; @@ -137,7 +139,8 @@ main { cursor: pointer; } -.link-area > button:hover { +.link-area > button:hover, +.download-area > button:hover { background-color: var(--text-color); color: var(--bg-color); box-shadow: 0 4px 15px 0 var(--shadow), 0 4px 15px 0 var(--shadow); @@ -150,7 +153,8 @@ main { } .upload-area, - .link-area { + .link-area, + .download-area { height: 65vh; } } @@ -162,7 +166,9 @@ main { flex-direction: column; } - .upload-area { + .upload-area, + .link-area, + .download-area { width: 35vw; height: 55vh; } diff --git a/src/routes/api/download.py b/src/routes/api/download.py index a055421..8110922 100644 --- a/src/routes/api/download.py +++ b/src/routes/api/download.py @@ -1,12 +1,20 @@ -from flask import Blueprint, redirect +from config import Config +from flask import Blueprint, jsonify, redirect, request from werkzeug.wrappers.response import Response router = Blueprint("download", __name__) -@router.route("", methods=["POST"]) -def download(file_hash: str) -> Response: - """Download interface""" - # TODO: Send the encrypted file to the javascript client - print("download of file", file_hash) - return redirect("index") +@router.route("", methods=["POST"]) +def download() -> Response: + """Download interface (send file to javascript client)""" + json = request.get_json() + if json: + data = "" + with open(f"{Config.uploads_dir}/{json}", 'r') as f: + data = f.read() + + # Send the encrypted file to the javascript client + return jsonify(data) + + return redirect("/file") diff --git a/src/routes/file.py b/src/routes/file.py index 4f7b787..ea1c58e 100644 --- a/src/routes/file.py +++ b/src/routes/file.py @@ -4,8 +4,7 @@ from flask import Blueprint, render_template router = Blueprint("file", __name__) -@router.route("") -def file(file_hash: int) -> str: +@router.route("") +def file(_: int) -> str: """Download page""" - print(f"hash : {file_hash}") return render_template("download.html", config=Config) diff --git a/src/templates/download.html b/src/templates/download.html index 0f5e07c..b24bb3b 100644 --- a/src/templates/download.html +++ b/src/templates/download.html @@ -17,8 +17,8 @@

Téléchargement

-

Cliquez pour lancer le téléchargement du fichier

- +

Cliquez sur le bouton pour lancer le téléchargement

+