* server now return hash to client
* show the link to copy for download
This commit is contained in:
parent
5b57ab7e2e
commit
6ea6f2678a
3 changed files with 53 additions and 14 deletions
|
@ -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 <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
|
||||
* 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
|
||||
|
|
|
@ -70,7 +70,8 @@ main {
|
|||
padding-top: 0.5em;
|
||||
}
|
||||
|
||||
.upload-area {
|
||||
.upload-area,
|
||||
.link-area {
|
||||
border: 2px dashed var(--border);
|
||||
height: 70vh;
|
||||
width: 65vw;
|
||||
|
|
|
@ -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")
|
||||
|
|
Reference in a new issue