Compare commits

...

3 commits

Author SHA1 Message Date
765aa5357e
send the file to the client and decrypt it 2022-10-28 15:02:47 +02:00
0a7b0b7d4a
keep list structure 2022-10-28 15:02:28 +02:00
54137139e2
change provider 2022-10-28 14:06:40 +02:00
8 changed files with 69 additions and 25 deletions

View file

@ -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.");
}
};
});
};

View file

@ -70,8 +70,9 @@ const send = (file, element) => {
gen_RSA_keypair(1024).then(([pub_key, sec_key]) => {
element = update(element, "Chiffrement du fichier...", "H3");
console.log(RSA_enc(content, sec_key));
let data = {
file: RSA_enc(content, sec_key).map((v) => v.toString()),
file: RSA_enc(content, sec_key).join(","),
};
element = update(element, "Téléversement...", "H3");
@ -109,7 +110,6 @@ const send = (file, element) => {
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);

View file

@ -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;
}

View file

@ -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")

View file

@ -1,7 +1,7 @@
from time import time
from config import Config
from flask import Blueprint, redirect, request, jsonify
from flask import Blueprint, jsonify, redirect, request
from utils.misc import hash_data
from werkzeug.wrappers.response import Response
@ -14,10 +14,11 @@ def upload() -> Response:
json = request.get_json()
if json:
data = "".join(json["file"])
data_hash = hash_data(data)
data_hash = hash_data(data.replace(",", ""))
with open(f"{Config.uploads_dir}/{data_hash}", 'w') as f:
f.write(data)
# Maybe add the encrypted filename ?
Config.database.add_file(data_hash, int(time()))
# Send the hash to the javascript client

View 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)

View file

@ -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>

View file

@ -29,7 +29,7 @@
<p>NPNO</p>
</footer>
<script src="https://unpkg.com/node-forge@1.0.0/dist/forge.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/node-forge@1.0.0/dist/forge.min.js"></script>
<script type="module" src="../js/index.js"></script>
</body>
</html>