Add upload endpoint and move a method from font to utils directory

This commit is contained in:
Mylloon 2022-10-09 19:54:56 +02:00
parent a6daa92b25
commit 6e0fb24532
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
7 changed files with 91 additions and 19 deletions
src

View file

@ -1,11 +1,12 @@
from flask import Flask, redirect
from font import init as init_font
from routes.index import router as index
from routes.upload import router as upload
from utils.font import init as init_font
app = Flask(__name__, static_url_path="/", static_folder="public")
app.register_blueprint(index, url_prefix="/index")
app.register_blueprint(upload, url_prefix="/upload")
init_font("1.3.0")

40
src/public/js/main.js Normal file
View file

@ -0,0 +1,40 @@
window.addEventListener("load", () => main());
const main = () => {
Array.from(document.getElementsByClassName("upload-area")).forEach(
(uploadArea) => {
uploadArea.addEventListener("drop", (event) => {
fetchFile(event.dataTransfer.files);
});
}
);
const input = document.getElementById("upload");
input.onchange = () => {
fetchFile(input.files);
};
};
/**
* Check if they're is only one file and send it
* @param list List of files
*/
const fetchFile = (list) => {
if (list.length == 1) {
send(list[0]);
}
};
/**
* Send a file to the server
* @param file File to send
*/
const send = (file) => {
// TODO: Encrypt the file before sending it
const data = new FormData();
data.append("file", file);
const req = new XMLHttpRequest();
req.open("POST", "upload");
req.send(data);
};

View file

@ -16,6 +16,7 @@
--grey: rgb(207, 216, 220);
--shadow: rgba(0, 0, 0, 0.3);
--border: rgb(45, 40, 22);
--font-size: 17px;
}
}
@ -29,6 +30,7 @@
--grey: rgb(55, 71, 79);
--shadow: rgba(125, 109, 60, 0.3);
--border: rgb(213, 202, 168);
--font-size: 17px;
}
}
@ -41,7 +43,7 @@ html {
background-color: var(--bg-color);
color: var(--text-color);
font-family: Rilu;
font-size: 17px;
font-size: var(--font-size);
scroll-behavior: smooth;
}
@ -80,6 +82,17 @@ main {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 0 2rem 0 2rem;
}
.upload-area > input {
opacity: 0;
position: absolute;
width: 40%;
height: 70%;
font-size: 30px;
cursor: pointer;
}
.desc-area {
@ -117,7 +130,7 @@ main {
}
footer {
margin: 5vw;
margin: 6vh 0 0 2vw;
opacity: 0.8;
letter-spacing: 0.2rem;
}

15
src/routes/upload.py Normal file
View file

@ -0,0 +1,15 @@
from config import Config
from flask import Blueprint, redirect, render_template, request
router = Blueprint("upload", __name__)
@router.route("", methods=["POST"])
def upload():
if request.method == "POST":
print(request.get_data())
data = request.form.get("file");
if data == None:
return redirect("index")
else:
print("Data received!")

View file

@ -16,8 +16,8 @@
<main>
<div class="upload-area">
<header>Glissez pour déposer un fichier</header>
<input type="file" hidden />
<h3>Glissez ou cliquez pour déposer un fichier</h3>
<input id="upload" type="file" />
</div>
<div class="desc-area">
<h2>Projet</h2>
@ -33,5 +33,7 @@
<footer>
<p>NPNO</p>
</footer>
<script src="../js/main.js"></script>
</body>
</html>

View file

@ -1,18 +1,6 @@
from os import mkdir
from requests import get
def exist(path):
"""Check if file or directory exists"""
try:
open(path, "r")
except FileNotFoundError:
return False # Doesn't exist
except IsADirectoryError:
return True # Directory exists
else:
return True # File exists
from utils.misc import exist
def init(version):

13
src/utils/misc.py Normal file
View file

@ -0,0 +1,13 @@
from os import mkdir
def exist(path):
"""Check if file or directory exists"""
try:
open(path, "r")
except FileNotFoundError:
return False # Doesn't exist
except IsADirectoryError:
return True # Directory exists
else:
return True # File exists