-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (49 loc) · 1.92 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
from flask import Flask, flash, request, redirect, url_for, send_from_directory, render_template
from werkzeug.utils import secure_filename
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__, static_url_path="/assets", static_folder="assets")
app.config['UPLOAD_FOLDER'] = "assets/uploads"
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000
@app.route('/')
def home():
stickers = os.listdir(app.config["UPLOAD_FOLDER"])
stickers = [s for s in stickers if s[0] != "."]
return render_template(
"index.html",
stickers=stickers,
)
@app.route('/uploads', methods=['POST'])
def upload_file():
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# check if the post request has the file part
if 'file' not in request.files:
print('No file part')
return redirect(request.url)
file = request.files['file']
# If the user does not select a file, the browser submits an
# empty file without a filename.
if file.filename == '':
print("No selected file")
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
if os.path.exists(path):
return "Il existe déjà un fichier avec ce nom"
file.save(path)
return redirect(url_for('home'))
@app.route('/print/<name>')
def print_sticker(name):
name = secure_filename(name)
path = os.path.join(app.config['UPLOAD_FOLDER'], name)
os.system(f"SIZE=big DITHERING=true bash scripts/print.sh '{path}'")
return redirect(url_for('home'))
@app.route('/delete/<name>')
def delete(name):
name = secure_filename(name)
path = os.path.join(app.config['UPLOAD_FOLDER'], name)
os.unlink(path)
return redirect(url_for('home'))