store and return the filename
This commit is contained in:
parent
9d49a5f581
commit
1b72004d7f
5 changed files with 31 additions and 18 deletions
|
@ -22,11 +22,15 @@ const main = () => {
|
||||||
req.onload = () => {
|
req.onload = () => {
|
||||||
if (req.status == 200) {
|
if (req.status == 200) {
|
||||||
// Decrypt the file
|
// Decrypt the file
|
||||||
|
const json = JSON.parse(req.responseText);
|
||||||
const decrypted_file = RSA_dec(
|
const decrypted_file = RSA_dec(
|
||||||
req.responseText
|
json.file.split(",").map((v) => BigInt(v)),
|
||||||
.slice(1, -2)
|
pub_key
|
||||||
.split(",")
|
);
|
||||||
.map((v) => BigInt(v)),
|
|
||||||
|
// Decrypt the filename
|
||||||
|
const decrypted_filename = RSA_dec(
|
||||||
|
json.filename.split(",").map((v) => BigInt(v)),
|
||||||
pub_key
|
pub_key
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -35,7 +39,7 @@ const main = () => {
|
||||||
type: "application/json",
|
type: "application/json",
|
||||||
});
|
});
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
download(url, "text.txt"); // Retrieve the original filename
|
download(url, decrypted_filename);
|
||||||
} else {
|
} else {
|
||||||
console.error("Download failed.");
|
console.error("Download failed.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,9 +77,9 @@ const send = (file, element) => {
|
||||||
gen_RSA_keypair(1024).then(([pub_key, 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");
|
||||||
|
|
||||||
console.log(RSA_enc(content, sec_key));
|
|
||||||
let data = {
|
let data = {
|
||||||
file: RSA_enc(content, sec_key).join(","),
|
file: RSA_enc(content, sec_key).join(","),
|
||||||
|
filename: RSA_enc(file.name, sec_key).join(","),
|
||||||
};
|
};
|
||||||
|
|
||||||
element = update(element, "Téléversement...", "H3");
|
element = update(element, "Téléversement...", "H3");
|
||||||
|
|
|
@ -8,13 +8,16 @@ router = Blueprint("download", __name__)
|
||||||
@router.route("", methods=["POST"])
|
@router.route("", methods=["POST"])
|
||||||
def download() -> Response:
|
def download() -> Response:
|
||||||
"""Download interface (send file to javascript client)"""
|
"""Download interface (send file to javascript client)"""
|
||||||
json = request.get_json()
|
hash_data = request.get_json()
|
||||||
if json:
|
if hash_data:
|
||||||
data = ""
|
data = ""
|
||||||
with open(f"{Config.uploads_dir}/{json}", 'r') as f:
|
with open(f"{Config.uploads_dir}/{hash_data}", 'r') as f:
|
||||||
data = f.read()
|
data = f.read()
|
||||||
|
|
||||||
|
response = Config.database.get_filename(hash_data)
|
||||||
|
response["file"] = data
|
||||||
|
|
||||||
# Send the encrypted file to the javascript client
|
# Send the encrypted file to the javascript client
|
||||||
return jsonify(data)
|
return jsonify(response)
|
||||||
|
|
||||||
return redirect("/file")
|
return redirect("/file")
|
||||||
|
|
|
@ -14,12 +14,12 @@ def upload() -> Response:
|
||||||
json = request.get_json()
|
json = request.get_json()
|
||||||
if json:
|
if json:
|
||||||
data = "".join(json["file"])
|
data = "".join(json["file"])
|
||||||
|
filename = "".join(json["filename"])
|
||||||
data_hash = hash_data(data.replace(",", ""))
|
data_hash = hash_data(data.replace(",", ""))
|
||||||
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)
|
||||||
|
|
||||||
# Maybe add the encrypted filename ?
|
Config.database.add_file(data_hash, filename, int(time()))
|
||||||
Config.database.add_file(data_hash, int(time()))
|
|
||||||
|
|
||||||
# Send the hash to the javascript client
|
# Send the hash to the javascript client
|
||||||
return jsonify(data_hash)
|
return jsonify(data_hash)
|
||||||
|
|
|
@ -59,15 +59,21 @@ class FilesDB(Database):
|
||||||
|
|
||||||
self.request(
|
self.request(
|
||||||
f"CREATE TABLE IF NOT EXISTS {self.table_name} \
|
f"CREATE TABLE IF NOT EXISTS {self.table_name} \
|
||||||
(filename TEXT, date INTEGER);")
|
(hash TEXT, filename TEXT, date INTEGER);")
|
||||||
|
|
||||||
def add_file(self, filename: str, date: int) -> None:
|
def add_file(self, hash_file: str, filename: str, date: int) -> None:
|
||||||
"""Add a file"""
|
"""Add a file"""
|
||||||
self.request(
|
self.request(
|
||||||
f"INSERT INTO {self.table_name} (filename, date) VALUES (?, ?);",
|
f"INSERT INTO {self.table_name} (hash, filename, date) VALUES (?, ?, ?);",
|
||||||
[filename, date])
|
[hash_file, filename, date])
|
||||||
|
|
||||||
def remove_file(self, filename: str) -> None:
|
def remove_file(self, hash_file: str) -> None:
|
||||||
"""Remove a file"""
|
"""Remove a file"""
|
||||||
self.request(
|
self.request(
|
||||||
f"DELETE FROM {self.table_name} WHERE filename = ?", filename)
|
f"DELETE FROM {self.table_name} WHERE hash = ?", hash_file)
|
||||||
|
|
||||||
|
def get_filename(self, hash_file: str) -> dict:
|
||||||
|
"""Return the filename of a specific file"""
|
||||||
|
query = "filename"
|
||||||
|
return self.format(query, self.request(
|
||||||
|
f"SELECT {query} FROM {self.table_name} WHERE hash = ?", hash_file))
|
||||||
|
|
Reference in a new issue