* server now return hash to client

* show the link to copy for download
This commit is contained in:
Mylloon 2022-10-28 01:06:15 +02:00
parent 5b57ab7e2e
commit 6ea6f2678a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 53 additions and 14 deletions

View file

@ -64,20 +64,56 @@ const update = (element, text, tag = undefined) => {
*/ */
const send = (file, element) => { const send = (file, element) => {
file.text().then((content) => { file.text().then((content) => {
const req = new XMLHttpRequest();
element = update(element, "Génération des clefs...", "H3"); 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"); element = update(element, "Chiffrement du fichier...", "H3");
let data = { let data = {
file: RSA_enc(content, sec_key).map((v) => v.toString()), file: RSA_enc(content, sec_key).map((v) => v.toString()),
}; };
update(element, "Téléversement...", "H3"); element = update(element, "Téléversement...", "H3");
const req = new XMLHttpRequest();
req.open("POST", "api/upload"); req.open("POST", "api/upload");
req.setRequestHeader("Content-Type", "application/json"); req.setRequestHeader("Content-Type", "application/json");
req.send(JSON.stringify(data)); 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 <code>${file.name}</code>`;
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 /* 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 * 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 * we redirect the user the a wait page so the uploader can copy

View file

@ -70,7 +70,8 @@ main {
padding-top: 0.5em; padding-top: 0.5em;
} }
.upload-area { .upload-area,
.link-area {
border: 2px dashed var(--border); border: 2px dashed var(--border);
height: 70vh; height: 70vh;
width: 65vw; width: 65vw;

View file

@ -1,8 +1,8 @@
from time import time from time import time
from config import Config from config import Config
from flask import Blueprint, redirect, request from flask import Blueprint, redirect, request, jsonify
from utils.misc import h from utils.misc import hash_data
from werkzeug.wrappers.response import Response from werkzeug.wrappers.response import Response
router = Blueprint("upload", __name__) router = Blueprint("upload", __name__)
@ -11,14 +11,16 @@ router = Blueprint("upload", __name__)
@router.route("", methods=["POST"]) @router.route("", methods=["POST"])
def upload() -> Response: def upload() -> Response:
"""Upload interface (receive file from javascript client)""" """Upload interface (receive file from javascript client)"""
if request.method == "POST": json = request.get_json()
json = request.get_json() if json:
if json: data = "".join(json["file"])
data = "".join(json["file"]) data_hash = hash_data(data)
data_hash = h(data) with open(f"{Config.uploads_dir}/{data_hash}", 'w') as f:
with open(f"{Config.uploads_dir}/{data_hash}", 'w') as f: f.write(data)
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") return redirect("/index")