From e4fd9af325f14776e07dc893835f2471e3aade55 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Thu, 27 Oct 2022 17:09:11 +0200 Subject: [PATCH] * fix 404 issue * update element text --- src/app.py | 2 ++ src/public/js/main.js | 42 +++++++++++++++++++++++----------------- src/routes/api/upload.py | 2 +- src/routes/uploaded.py | 9 +++++++++ 4 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 src/routes/uploaded.py diff --git a/src/app.py b/src/app.py index 2734b06..56f4ae8 100644 --- a/src/app.py +++ b/src/app.py @@ -5,9 +5,11 @@ from config import init from routes.api.download import router as api_download from routes.api.upload import router as api_upload from routes.index import router as index +from routes.uploaded import router as uploaded app = Flask(__name__, static_url_path="/", static_folder="public") app.register_blueprint(index, url_prefix="/index") +app.register_blueprint(uploaded, url_prefix="/uploaded") app.register_blueprint(api_upload, url_prefix="/api/upload") app.register_blueprint(api_download, url_prefix="/api/download") diff --git a/src/public/js/main.js b/src/public/js/main.js index 290fbca..9e3cfe0 100644 --- a/src/public/js/main.js +++ b/src/public/js/main.js @@ -1,8 +1,4 @@ -import { - gen_RSA_keypair, - RSA_enc_data as RSA_enc, - RSA_dec_data as RSA_dec, -} from "./rsa.js"; +import { gen_RSA_keypair, RSA_enc_data as RSA_enc } from "./rsa.js"; window.addEventListener("load", () => main()); @@ -46,27 +42,37 @@ const fetchFile = (list, element = undefined) => { } }; +const update = (element, text, tag = undefined) => { + if (element) { + let parent = element.parentElement; + parent.textContent = ""; + let newElement = document.createElement( + tag === undefined ? element.tagName : tag + ); + newElement.textContent = text; + parent.appendChild(newElement); + + return newElement; + } + + return undefined; +}; + /** * Send a file to the server * @param file File to send */ const send = (file, element) => { - // Show the user a file is uploading - if (element) { - let parent = element.parentElement; - parent.textContent = ""; - let newText = document.createElement("h3"); - newText.textContent = "Téléversement..."; - parent.appendChild(newText); - } - - // Encrypt the file file.text().then((content) => { - gen_RSA_keypair(1024).then(([pub_key, sec_key]) => { + element = update(element, "Génération des clefs...", "H3"); + gen_RSA_keypair(1024).then(([, 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(); req.open("POST", "api/upload"); req.setRequestHeader("Content-Type", "application/json"); @@ -74,8 +80,8 @@ const send = (file, element) => { /* 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 - * salted (so 2 same file don't have a different URL) we redirect - * the user the a wait page so the uploader can copy a link like: + * we redirect the user the a wait page so the uploader can copy + * a link like: * Wait page: https://d/upload * Copy link: https://d/download/hash#pub_key_0:pub_key_1 * When the user click on the link, he can download the file, asking diff --git a/src/routes/api/upload.py b/src/routes/api/upload.py index de4cc67..24896a1 100644 --- a/src/routes/api/upload.py +++ b/src/routes/api/upload.py @@ -16,4 +16,4 @@ def upload() -> Response: with open(f"{Config.uploads_dir}/{data_hash}", "w") as f: f.write(data) - return redirect("index") + return redirect("/uploaded") diff --git a/src/routes/uploaded.py b/src/routes/uploaded.py new file mode 100644 index 0000000..ba881b9 --- /dev/null +++ b/src/routes/uploaded.py @@ -0,0 +1,9 @@ +from config import Config +from flask import Blueprint, render_template + +router = Blueprint("uploaded", __name__) + + +@router.route("") +def uploaded() -> str: + return render_template("index.html", name=Config.name)