send the file to the client and decrypt it
This commit is contained in:
parent
0a7b0b7d4a
commit
765aa5357e
5 changed files with 63 additions and 20 deletions
|
@ -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.");
|
||||
}
|
||||
};
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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("<file_hash>", 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")
|
||||
|
|
|
@ -4,8 +4,7 @@ from flask import Blueprint, render_template
|
|||
router = Blueprint("file", __name__)
|
||||
|
||||
|
||||
@router.route("<int:file_hash>")
|
||||
def file(file_hash: int) -> str:
|
||||
@router.route("<int:_>")
|
||||
def file(_: int) -> str:
|
||||
"""Download page"""
|
||||
print(f"hash : {file_hash}")
|
||||
return render_template("download.html", config=Config)
|
||||
|
|
|
@ -17,8 +17,8 @@
|
|||
<main>
|
||||
<div class="download-area">
|
||||
<h3>Téléchargement</h3>
|
||||
<p>Cliquez pour lancer le téléchargement du fichier</p>
|
||||
<button>Hihi</button>
|
||||
<p>Cliquez sur le bouton pour lancer le téléchargement</p>
|
||||
<button id="download">Télécharger</button>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
|
|
Reference in a new issue