From 6ea6f2678a964f1e24efd60cac2a9976b0e6081f Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 28 Oct 2022 01:06:15 +0200 Subject: [PATCH] * server now return hash to client * show the link to copy for download --- src/public/js/main.js | 42 ++++++++++++++++++++++++++++++++++--- src/public/styles/style.css | 3 ++- src/routes/api/upload.py | 22 ++++++++++--------- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/src/public/js/main.js b/src/public/js/main.js index 9e3cfe0..39f9058 100644 --- a/src/public/js/main.js +++ b/src/public/js/main.js @@ -64,20 +64,56 @@ const update = (element, text, tag = undefined) => { */ const send = (file, element) => { file.text().then((content) => { + const req = new XMLHttpRequest(); + element = update(element, "Génération des clefs...", "H3"); - gen_RSA_keypair(1024).then(([, sec_key]) => { + gen_RSA_keypair(1024).then(([pub_key, sec_key]) => { element = update(element, "Chiffrement du fichier...", "H3"); let data = { file: RSA_enc(content, sec_key).map((v) => v.toString()), }; - update(element, "Téléversement...", "H3"); - const req = new XMLHttpRequest(); + element = update(element, "Téléversement...", "H3"); req.open("POST", "api/upload"); req.setRequestHeader("Content-Type", "application/json"); req.send(JSON.stringify(data)); + req.onload = () => { + if (req.status == 200) { + /* Change here the area with the copy link */ + let url = window.location.href.split("/"); + url.pop(); + url.push("file"); + + let main_div = element.parentElement.parentElement; + main_div.textContent = ""; + let div = document.createElement("DIV"); + div.textContent = "Fichier prêt !"; + div.className = "link-area"; + main_div.appendChild(div); + + let message = document.createElement("P"); + message.innerHTML = `Copiez le lien pour télécharger ${file.name}`; + div.appendChild(message); + + let input = document.createElement("INPUT"); + input.value = `${url.join("/")}/${req.responseText.slice( + 1, + -2 + )}/${pub_key[0]}:${pub_key[1]}`; + input.readOnly = true; + div.appendChild(input); + + // TODO: Change button textContent on click + let button = document.createElement("BUTTON"); + button.textContent = "Copier le lien"; + div.appendChild(button); + } else { + console.error("Upload failed."); + } + }; + /* Here we need to store the public key and then wait for a response * from the server. When the server send us a hash of the file * we redirect the user the a wait page so the uploader can copy diff --git a/src/public/styles/style.css b/src/public/styles/style.css index 059516c..d734f8e 100644 --- a/src/public/styles/style.css +++ b/src/public/styles/style.css @@ -70,7 +70,8 @@ main { padding-top: 0.5em; } -.upload-area { +.upload-area, +.link-area { border: 2px dashed var(--border); height: 70vh; width: 65vw; diff --git a/src/routes/api/upload.py b/src/routes/api/upload.py index 9c1d2d6..121ae01 100644 --- a/src/routes/api/upload.py +++ b/src/routes/api/upload.py @@ -1,8 +1,8 @@ from time import time from config import Config -from flask import Blueprint, redirect, request -from utils.misc import h +from flask import Blueprint, redirect, request, jsonify +from utils.misc import hash_data from werkzeug.wrappers.response import Response router = Blueprint("upload", __name__) @@ -11,14 +11,16 @@ router = Blueprint("upload", __name__) @router.route("", methods=["POST"]) def upload() -> Response: """Upload interface (receive file from javascript client)""" - if request.method == "POST": - json = request.get_json() - if json: - data = "".join(json["file"]) - data_hash = h(data) - with open(f"{Config.uploads_dir}/{data_hash}", 'w') as f: - f.write(data) + json = request.get_json() + if json: + data = "".join(json["file"]) + data_hash = hash_data(data) + with open(f"{Config.uploads_dir}/{data_hash}", 'w') as f: + f.write(data) - Config.database.add_file(data_hash, int(time())) + Config.database.add_file(data_hash, int(time())) + + # Send the hash to the javascript client + return jsonify(data_hash) return redirect("/index")