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());
|
window.addEventListener("load", () => main());
|
||||||
|
|
||||||
const main = () => {
|
const main = () => {
|
||||||
const pub_key = window.location.hash.slice(1).split(":");
|
/* 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();
|
const hash = window.location.pathname.split("/").pop();
|
||||||
console.log(pub_key, hash);
|
|
||||||
|
// 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,
|
.upload-area,
|
||||||
.link-area {
|
.link-area,
|
||||||
|
.download-area {
|
||||||
border: 2px dashed var(--border);
|
border: 2px dashed var(--border);
|
||||||
height: 70vh;
|
height: 70vh;
|
||||||
width: 65vw;
|
width: 65vw;
|
||||||
|
@ -124,7 +125,8 @@ main {
|
||||||
outline: none;
|
outline: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.link-area > button {
|
.link-area > button,
|
||||||
|
.download-area > button {
|
||||||
border-radius: 6px;
|
border-radius: 6px;
|
||||||
border: 2px solid var(--shadow);
|
border: 2px solid var(--shadow);
|
||||||
width: 50%;
|
width: 50%;
|
||||||
|
@ -137,7 +139,8 @@ main {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.link-area > button:hover {
|
.link-area > button:hover,
|
||||||
|
.download-area > button:hover {
|
||||||
background-color: var(--text-color);
|
background-color: var(--text-color);
|
||||||
color: var(--bg-color);
|
color: var(--bg-color);
|
||||||
box-shadow: 0 4px 15px 0 var(--shadow), 0 4px 15px 0 var(--shadow);
|
box-shadow: 0 4px 15px 0 var(--shadow), 0 4px 15px 0 var(--shadow);
|
||||||
|
@ -150,7 +153,8 @@ main {
|
||||||
}
|
}
|
||||||
|
|
||||||
.upload-area,
|
.upload-area,
|
||||||
.link-area {
|
.link-area,
|
||||||
|
.download-area {
|
||||||
height: 65vh;
|
height: 65vh;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -162,7 +166,9 @@ main {
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
}
|
}
|
||||||
|
|
||||||
.upload-area {
|
.upload-area,
|
||||||
|
.link-area,
|
||||||
|
.download-area {
|
||||||
width: 35vw;
|
width: 35vw;
|
||||||
height: 55vh;
|
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
|
from werkzeug.wrappers.response import Response
|
||||||
|
|
||||||
router = Blueprint("download", __name__)
|
router = Blueprint("download", __name__)
|
||||||
|
|
||||||
|
|
||||||
@router.route("<file_hash>", methods=["POST"])
|
@router.route("", methods=["POST"])
|
||||||
def download(file_hash: str) -> Response:
|
def download() -> Response:
|
||||||
"""Download interface"""
|
"""Download interface (send file to javascript client)"""
|
||||||
# TODO: Send the encrypted file to the javascript client
|
json = request.get_json()
|
||||||
print("download of file", file_hash)
|
if json:
|
||||||
return redirect("index")
|
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 = Blueprint("file", __name__)
|
||||||
|
|
||||||
|
|
||||||
@router.route("<int:file_hash>")
|
@router.route("<int:_>")
|
||||||
def file(file_hash: int) -> str:
|
def file(_: int) -> str:
|
||||||
"""Download page"""
|
"""Download page"""
|
||||||
print(f"hash : {file_hash}")
|
|
||||||
return render_template("download.html", config=Config)
|
return render_template("download.html", config=Config)
|
||||||
|
|
|
@ -17,8 +17,8 @@
|
||||||
<main>
|
<main>
|
||||||
<div class="download-area">
|
<div class="download-area">
|
||||||
<h3>Téléchargement</h3>
|
<h3>Téléchargement</h3>
|
||||||
<p>Cliquez pour lancer le téléchargement du fichier</p>
|
<p>Cliquez sur le bouton pour lancer le téléchargement</p>
|
||||||
<button>Hihi</button>
|
<button id="download">Télécharger</button>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
|
|
Reference in a new issue