Compare commits

...

2 commits

Author SHA1 Message Date
d56cbea697
add types 2022-10-27 16:19:11 +02:00
7ce888bf94
* 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
*
2022-10-27 16:08:04 +02:00
10 changed files with 64 additions and 22 deletions

2
.gitignore vendored
View file

@ -9,3 +9,5 @@ __pycache__/
src/public/fonts/
src/public/js/libs/
uploads/

View file

@ -1,20 +1,19 @@
from flask import Flask, redirect
from werkzeug.wrappers.response import Response
from routes.index import router as index
from routes.api.upload import router as api_upload
from config import init
from routes.api.download import router as api_download
from utils.font import init as init_font
from utils.libjs import init as init_libjs
from routes.api.upload import router as api_upload
from routes.index import router as index
app = Flask(__name__, static_url_path="/", static_folder="public")
app.register_blueprint(index, url_prefix="/index")
app.register_blueprint(api_upload, url_prefix="/api/upload")
app.register_blueprint(api_download, url_prefix="/api/download")
init_font("1.3.0")
init_libjs("fc5e3c53e41490e24ca7f67cb24e7ab389b770f9")
init()
@app.route("/")
def root():
def root() -> Response:
return redirect("index")

View file

@ -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:
name = "Sand"
uploads_dir = "uploads"
def init() -> None:
# 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)

View file

@ -63,15 +63,14 @@ const send = (file, element) => {
// Encrypt the file
file.text().then((content) => {
gen_RSA_keypair(1024).then(([pub_key, sec_key]) => {
let encoded_file = RSA_enc(content, sec_key);
// Send it
const data = new FormData();
data.append("file", encoded_file);
let data = {
file: RSA_enc(content, sec_key).map((v) => v.toString()),
};
const req = new XMLHttpRequest();
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
* from the server. When the server send us a hash of the file

View file

@ -1,8 +1,9 @@
from flask import Blueprint, redirect, request
from flask import Blueprint, redirect
from werkzeug.wrappers.response import Response
router = Blueprint("download", __name__)
@router.route("", methods=["POST"])
def download():
def download() -> Response:
return redirect("index")

View file

@ -1,11 +1,19 @@
from config import Config
from flask import Blueprint, redirect, request
from utils.misc import h
from werkzeug.wrappers.response import Response
router = Blueprint("upload", __name__)
@router.route("", methods=["POST"])
def upload():
def upload() -> Response:
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")

View file

@ -1,9 +1,9 @@
from flask import Blueprint, render_template
from config import Config
from flask import Blueprint, render_template
router = Blueprint("index", __name__)
@router.route("")
def index():
def index() -> str:
return render_template("index.html", name=Config.name)

View file

@ -5,7 +5,7 @@ from requests import get
from utils.misc import exist
def init(version):
def init(version: str) -> None:
""" Download font """
path = "./src/public/fonts"
filename = "RiluRegular.ttf"

View file

@ -5,7 +5,7 @@ from requests import get
from utils.misc import exist
def init(commit_hash):
def init(commit_hash: str) -> None:
""" Download JS libraries"""
path = "./src/public/js/libs"
filename = "bigint-mod.js"

View file

@ -1,4 +1,9 @@
def exist(path):
from hashlib import sha256
BYTEORDER = "big"
def exist(path: str) -> bool:
"""Check if file or directory exists"""
try:
open(path, "r")
@ -8,3 +13,13 @@ def exist(path):
return True # Directory exists
else:
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))