* use json to send file to flask
* hash file to have a filename * store the data to custom dir "uploads" * init everyting in init fn *
This commit is contained in:
parent
930eeacd2e
commit
7ce888bf94
6 changed files with 53 additions and 14 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -9,3 +9,5 @@ __pycache__/
|
||||||
|
|
||||||
src/public/fonts/
|
src/public/fonts/
|
||||||
src/public/js/libs/
|
src/public/js/libs/
|
||||||
|
|
||||||
|
uploads/
|
||||||
|
|
10
src/app.py
10
src/app.py
|
@ -1,18 +1,16 @@
|
||||||
from flask import Flask, redirect
|
from flask import Flask, redirect
|
||||||
|
|
||||||
from routes.index import router as index
|
from config import init
|
||||||
from routes.api.upload import router as api_upload
|
|
||||||
from routes.api.download import router as api_download
|
from routes.api.download import router as api_download
|
||||||
from utils.font import init as init_font
|
from routes.api.upload import router as api_upload
|
||||||
from utils.libjs import init as init_libjs
|
from routes.index import router as index
|
||||||
|
|
||||||
app = Flask(__name__, static_url_path="/", static_folder="public")
|
app = Flask(__name__, static_url_path="/", static_folder="public")
|
||||||
app.register_blueprint(index, url_prefix="/index")
|
app.register_blueprint(index, url_prefix="/index")
|
||||||
app.register_blueprint(api_upload, url_prefix="/api/upload")
|
app.register_blueprint(api_upload, url_prefix="/api/upload")
|
||||||
app.register_blueprint(api_download, url_prefix="/api/download")
|
app.register_blueprint(api_download, url_prefix="/api/download")
|
||||||
|
|
||||||
init_font("1.3.0")
|
init()
|
||||||
init_libjs("fc5e3c53e41490e24ca7f67cb24e7ab389b770f9")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
|
|
|
@ -1,2 +1,20 @@
|
||||||
|
from os import mkdir
|
||||||
|
|
||||||
|
from utils.font import init as init_font
|
||||||
|
from utils.libjs import init as init_libjs
|
||||||
|
from utils.misc import exist
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
name = "Sand"
|
name = "Sand"
|
||||||
|
uploads_dir = "uploads"
|
||||||
|
|
||||||
|
|
||||||
|
def init():
|
||||||
|
# Download dependencies
|
||||||
|
init_font("1.3.0")
|
||||||
|
init_libjs("fc5e3c53e41490e24ca7f67cb24e7ab389b770f9")
|
||||||
|
|
||||||
|
# Create upload folder if doesn't exists
|
||||||
|
if not exist(Config.uploads_dir):
|
||||||
|
mkdir(Config.uploads_dir)
|
||||||
|
|
|
@ -63,15 +63,14 @@ const send = (file, element) => {
|
||||||
// Encrypt the file
|
// Encrypt the file
|
||||||
file.text().then((content) => {
|
file.text().then((content) => {
|
||||||
gen_RSA_keypair(1024).then(([pub_key, sec_key]) => {
|
gen_RSA_keypair(1024).then(([pub_key, sec_key]) => {
|
||||||
let encoded_file = RSA_enc(content, sec_key);
|
let data = {
|
||||||
|
file: RSA_enc(content, sec_key).map((v) => v.toString()),
|
||||||
// Send it
|
};
|
||||||
const data = new FormData();
|
|
||||||
data.append("file", encoded_file);
|
|
||||||
|
|
||||||
const req = new XMLHttpRequest();
|
const req = new XMLHttpRequest();
|
||||||
req.open("POST", "api/upload");
|
req.open("POST", "api/upload");
|
||||||
req.send(data);
|
req.setRequestHeader("Content-Type", "application/json");
|
||||||
|
req.send(JSON.stringify(data));
|
||||||
|
|
||||||
/* Here we need to store the public key and then wait for a response
|
/* 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
|
* from the server. When the server send us a hash of the file
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
from config import Config
|
||||||
from flask import Blueprint, redirect, request
|
from flask import Blueprint, redirect, request
|
||||||
|
from utils.misc import h
|
||||||
|
|
||||||
router = Blueprint("upload", __name__)
|
router = Blueprint("upload", __name__)
|
||||||
|
|
||||||
|
@ -6,6 +8,11 @@ router = Blueprint("upload", __name__)
|
||||||
@router.route("", methods=["POST"])
|
@router.route("", methods=["POST"])
|
||||||
def upload():
|
def upload():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
print(request.get_data())
|
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)
|
||||||
|
|
||||||
return redirect("index")
|
return redirect("index")
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
def exist(path):
|
from hashlib import sha256
|
||||||
|
|
||||||
|
BYTEORDER = "big"
|
||||||
|
|
||||||
|
|
||||||
|
def exist(path: str) -> bool:
|
||||||
"""Check if file or directory exists"""
|
"""Check if file or directory exists"""
|
||||||
try:
|
try:
|
||||||
open(path, "r")
|
open(path, "r")
|
||||||
|
@ -8,3 +13,13 @@ def exist(path):
|
||||||
return True # Directory exists
|
return True # Directory exists
|
||||||
else:
|
else:
|
||||||
return True # File exists
|
return True # File exists
|
||||||
|
|
||||||
|
|
||||||
|
def int_to_bytes(data: int) -> bytes:
|
||||||
|
"""Transforme un int en bytes"""
|
||||||
|
return data.to_bytes((data.bit_length() + 7) // 8, BYTEORDER)
|
||||||
|
|
||||||
|
|
||||||
|
def h(data: str) -> str:
|
||||||
|
"""Hash un texte"""
|
||||||
|
return str(int.from_bytes(sha256(int_to_bytes(int(data))).digest(), BYTEORDER))
|
||||||
|
|
Reference in a new issue