Implement file handler with separated data per user
This commit is contained in:
parent
c4436d040b
commit
cd4df840b6
5 changed files with 184 additions and 79 deletions
181
app.py
181
app.py
|
@ -1,76 +1,22 @@
|
||||||
from flask import Flask, render_template, request, redirect, url_for, session
|
from flask import Flask, render_template, request, redirect, url_for, session, send_from_directory
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from gen_image import gen_image
|
from gen_image import gen_image
|
||||||
from printer_info import *
|
from printer_info import *
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from print import *
|
from print import *
|
||||||
from format_image import *
|
from format_image import *
|
||||||
|
from file_handler import *
|
||||||
|
from colorama import Fore, Style
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
generated_image_filename = "generated.png"
|
||||||
|
uploaded_image_filename = "uploaded" #extention is depended on uploaded file format
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['UPLOAD_FOLDER'] = 'static/uploads'
|
|
||||||
app.config['TEXT_FOLDER'] = 'static/text'
|
|
||||||
app.secret_key = "blahaj"
|
app.secret_key = "blahaj"
|
||||||
|
|
||||||
# Ensure the upload directory exists
|
|
||||||
if not os.path.exists(app.config['UPLOAD_FOLDER']):
|
|
||||||
os.makedirs(app.config['UPLOAD_FOLDER'])
|
|
||||||
if not os.path.exists(app.config['TEXT_FOLDER']):
|
|
||||||
os.makedirs(app.config['TEXT_FOLDER'])
|
|
||||||
|
|
||||||
#TODO: make it so sessions don't interfere with eachother
|
|
||||||
|
|
||||||
@app.route('/', methods=['GET', 'POST'])
|
|
||||||
def base():
|
|
||||||
return render_template('base.html')
|
|
||||||
|
|
||||||
@app.route('/image', methods=['GET', 'POST'])
|
|
||||||
def image():
|
|
||||||
if request.method == 'POST':
|
|
||||||
if 'image' not in request.files:
|
|
||||||
return redirect(request.url)
|
|
||||||
file = request.files['image']
|
|
||||||
|
|
||||||
if file.filename == '':
|
|
||||||
return redirect(request.url)
|
|
||||||
if file:
|
|
||||||
extension = os.path.splitext(file.filename)[1]
|
|
||||||
filepath = os.path.join(app.config['UPLOAD_FOLDER'], "upload" + extension)
|
|
||||||
session["filepath"] = filepath
|
|
||||||
|
|
||||||
file.save(filepath)
|
|
||||||
|
|
||||||
# Process image #
|
|
||||||
format_image(filepath).save(filepath)
|
|
||||||
#################
|
|
||||||
|
|
||||||
session["filename"] = "upload"+extension
|
|
||||||
return render_template('image.html', filename=session["filename"], cut=session["cut"])
|
|
||||||
|
|
||||||
session["cut"] = True
|
|
||||||
return render_template('image.html', cut=True)
|
|
||||||
|
|
||||||
|
|
||||||
#TODO: maybe merge some stuff with text-print
|
|
||||||
@app.route('/image-print', methods=['GET', 'POST'])
|
|
||||||
def image_print():
|
|
||||||
if request.method == 'POST':
|
|
||||||
image = format_image_to_label(session["filepath"])
|
|
||||||
print_image(image)
|
|
||||||
|
|
||||||
if 'cut' in request.form:
|
|
||||||
print("printing and cutting")
|
|
||||||
cut_paper()
|
|
||||||
session["cut"] = True;
|
|
||||||
else:
|
|
||||||
print("printing")
|
|
||||||
session["cut"] = False;
|
|
||||||
|
|
||||||
return render_template('image.html', filename=session["filename"], cut=session["cut"])
|
|
||||||
|
|
||||||
return render_template('image.html', filename=session["filename"], cut=session["cut"])
|
|
||||||
|
|
||||||
|
|
||||||
templates = {
|
templates = {
|
||||||
"DNH": {
|
"DNH": {
|
||||||
"text1": {
|
"text1": {
|
||||||
|
@ -100,8 +46,78 @@ templates = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/', methods=['GET', 'POST'])
|
||||||
|
def base():
|
||||||
|
check_for_new_user(session)
|
||||||
|
return render_template('base.html')
|
||||||
|
|
||||||
|
|
||||||
|
# gets triggered when the user_data folder for the user is created
|
||||||
|
@new_user_handler
|
||||||
|
def on_new_user(session):
|
||||||
|
print(Fore.BLUE + "new user with ID:", session["ID"], Style.RESET_ALL)
|
||||||
|
|
||||||
|
session["text image path"] = None
|
||||||
|
session["uploaded image path"] = None
|
||||||
|
session["cut"] = True
|
||||||
|
session["text1"] = templates["DNH"]["text1"]
|
||||||
|
session["text2"] = templates["DNH"]["text2"]
|
||||||
|
session["cut"] = templates["DNH"]["cut"]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/image', methods=['GET', 'POST'])
|
||||||
|
def image():
|
||||||
|
check_for_new_user(session)
|
||||||
|
|
||||||
|
if request.method == 'POST':
|
||||||
|
if 'image' not in request.files:
|
||||||
|
return redirect(request.url)
|
||||||
|
file = request.files['image']
|
||||||
|
|
||||||
|
if file.filename == '':
|
||||||
|
return redirect(request.url)
|
||||||
|
if file:
|
||||||
|
extension = os.path.splitext(file.filename)[1]
|
||||||
|
session["uploaded image path"] = uploaded_image_filename+extension
|
||||||
|
|
||||||
|
filepath = get_file_path(session, session["uploaded image path"])
|
||||||
|
file.save(filepath)
|
||||||
|
|
||||||
|
format_image(filepath).save(filepath)
|
||||||
|
|
||||||
|
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"])
|
||||||
|
|
||||||
|
|
||||||
|
#TODO: maybe merge some stuff with text-print
|
||||||
|
@app.route('/image-print', methods=['GET', 'POST'])
|
||||||
|
def image_print():
|
||||||
|
check_for_new_user(session)
|
||||||
|
|
||||||
|
if session["uploaded image path"] == None:
|
||||||
|
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL)
|
||||||
|
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"])
|
||||||
|
|
||||||
|
if request.method == 'POST':
|
||||||
|
image = format_image_to_label(get_file_path(session, session["uploaded image path"]))
|
||||||
|
print_image(image)
|
||||||
|
|
||||||
|
if 'cut' in request.form:
|
||||||
|
print("printing and cutting")
|
||||||
|
cut_paper()
|
||||||
|
session["cut"] = True;
|
||||||
|
else:
|
||||||
|
print("printing")
|
||||||
|
session["cut"] = False;
|
||||||
|
|
||||||
|
return render_template('image.html', filename=session["uploaded image path"], cut=session["cut"])
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text-template', methods=['GET', 'POST'])
|
@app.route('/text-template', methods=['GET', 'POST'])
|
||||||
def text_template():
|
def text_template():
|
||||||
|
check_for_new_user(session)
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
template = templates[request.form["template"]]
|
template = templates[request.form["template"]]
|
||||||
if request.form["template"] == "Food":
|
if request.form["template"] == "Food":
|
||||||
|
@ -111,12 +127,13 @@ def text_template():
|
||||||
session["text2"] = template["text2"]
|
session["text2"] = template["text2"]
|
||||||
session["cut"] = template["cut"]
|
session["cut"] = template["cut"]
|
||||||
|
|
||||||
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
|
||||||
|
|
||||||
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text-form', methods=['GET', 'POST'])
|
@app.route('/text-form', methods=['GET', 'POST'])
|
||||||
def text_form():
|
def text_form():
|
||||||
|
check_for_new_user(session)
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
session["text1"] = {
|
session["text1"] = {
|
||||||
"string": request.form["string1"],
|
"string": request.form["string1"],
|
||||||
|
@ -130,28 +147,35 @@ def text_form():
|
||||||
}
|
}
|
||||||
|
|
||||||
img = gen_image(label_width, session["text1"], session["text2"])
|
img = gen_image(label_width, session["text1"], session["text2"])
|
||||||
filepath = os.path.join(app.config['TEXT_FOLDER'], "text.png")
|
|
||||||
img.save(filepath)
|
|
||||||
print(session)
|
|
||||||
|
|
||||||
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
get_folder_path(session)
|
||||||
|
|
||||||
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
session["text image path"] = generated_image_filename
|
||||||
|
img.save(get_file_path(session, session["text image path"]))
|
||||||
|
print()
|
||||||
|
print(session["text image path"])
|
||||||
|
|
||||||
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text', methods=['GET', 'POST'])
|
@app.route('/text', methods=['GET', 'POST'])
|
||||||
def text():
|
def text():
|
||||||
session["text1"] = templates["DNH"]["text1"]
|
check_for_new_user(session)
|
||||||
session["text2"] = templates["DNH"]["text2"]
|
|
||||||
session["cut"] = templates["DNH"]["cut"]
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
||||||
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text-print', methods=['GET', 'POST'])
|
@app.route('/text-print', methods=['GET', 'POST'])
|
||||||
def text_print():
|
def text_print():
|
||||||
|
check_for_new_user(session)
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
image = format_image_to_label("static/text/text.png")
|
if session["text image path"] == None:
|
||||||
|
print(Fore.YELLOW + "Warning, file doesn't exist" + Style.RESET_ALL)
|
||||||
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
||||||
|
|
||||||
|
|
||||||
|
image = format_image_to_label(get_file_path(session, session["text image path"]))
|
||||||
print_image(image)
|
print_image(image)
|
||||||
|
|
||||||
if 'cut' in request.form:
|
if 'cut' in request.form:
|
||||||
|
@ -162,19 +186,24 @@ def text_print():
|
||||||
print("printing")
|
print("printing")
|
||||||
session["cut"] = False;
|
session["cut"] = False;
|
||||||
|
|
||||||
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
||||||
|
|
||||||
return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"])
|
|
||||||
|
@app.route('/user_data/<filename>')
|
||||||
|
def user_data(filename):
|
||||||
|
return send_from_directory(get_folder_path(session), filename)
|
||||||
|
|
||||||
|
|
||||||
@app.route('/uploads/<filename>')
|
@app.route('/uploads/<filename>')
|
||||||
def uploaded_file(filename):
|
def uploaded_file(filename):
|
||||||
return redirect(url_for('static', filename='uploads/' + filename))
|
return redirect(url_for('static', filename='uploads/' + filename))
|
||||||
|
|
||||||
|
|
||||||
@app.route('/text/<filename>')
|
@app.route('/text/<filename>')
|
||||||
def generated_file(filename):
|
def generated_file(filename):
|
||||||
return redirect(url_for('static', filename='text/' + filename))
|
return redirect(url_for('static', filename='text/' + filename))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
||||||
|
|
76
file_handler.py
Normal file
76
file_handler.py
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
import random
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
import time
|
||||||
|
|
||||||
|
user_data_lifetime_seconds = 2*60*60
|
||||||
|
data_path = "user_data"
|
||||||
|
users = dict([])
|
||||||
|
|
||||||
|
if os.path.exists(data_path):
|
||||||
|
shutil.rmtree(data_path)
|
||||||
|
|
||||||
|
def delete_old_users(session):
|
||||||
|
for ID in users.copy():
|
||||||
|
elapsed = time.time() - users[ID]["last accessed"]
|
||||||
|
|
||||||
|
if (elapsed > user_data_lifetime_seconds):
|
||||||
|
print(ID, "Hasn't accessed their files in", elapsed, "seconds, removing all user data")
|
||||||
|
|
||||||
|
shutil.rmtree(users[ID]["path"])
|
||||||
|
del users[ID]
|
||||||
|
|
||||||
|
new_user_handler_func = None
|
||||||
|
|
||||||
|
|
||||||
|
def new_user_handler(func):
|
||||||
|
global new_user_handler_func
|
||||||
|
new_user_handler_func = func
|
||||||
|
return func
|
||||||
|
|
||||||
|
|
||||||
|
def new_user(session):
|
||||||
|
while(True):
|
||||||
|
ID = random.randint(0, 100000000)
|
||||||
|
if not ID in users:
|
||||||
|
break
|
||||||
|
|
||||||
|
users[ID] = {
|
||||||
|
"path": data_path + "/" + str(ID),
|
||||||
|
"last accessed": time.time()
|
||||||
|
}
|
||||||
|
|
||||||
|
session["ID"] = ID
|
||||||
|
|
||||||
|
os.makedirs(users[session["ID"]]["path"])
|
||||||
|
|
||||||
|
if new_user_handler_func:
|
||||||
|
new_user_handler_func(session)
|
||||||
|
|
||||||
|
|
||||||
|
def get_file_path(session, filename):
|
||||||
|
return os.path.join(get_folder_path(session), filename)
|
||||||
|
|
||||||
|
|
||||||
|
def file_exists(session, filename):
|
||||||
|
return os.path.exists(get_file_path(session, filename))
|
||||||
|
|
||||||
|
|
||||||
|
def check_for_new_user(session):
|
||||||
|
if not "ID" in session:
|
||||||
|
new_user(session)
|
||||||
|
elif not session["ID"] in users:
|
||||||
|
new_user(session)
|
||||||
|
|
||||||
|
delete_old_users(session)
|
||||||
|
|
||||||
|
|
||||||
|
def get_folder_path(session):
|
||||||
|
check_for_new_user(session)
|
||||||
|
ID = session["ID"]
|
||||||
|
user = users[ID]
|
||||||
|
|
||||||
|
user["last accessed"] = time.time()
|
||||||
|
|
||||||
|
return user["path"]
|
||||||
|
|
|
@ -24,4 +24,4 @@ def format_image(path):
|
||||||
return image
|
return image
|
||||||
|
|
||||||
|
|
||||||
format_image("static/uploads/upload.png").save("output.png")
|
#format_image("static/uploads/upload.png").save("output.png")
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
{% if filename %}
|
{% if filename %}
|
||||||
<br><br>
|
<br><br>
|
||||||
<img src="{{ url_for('uploaded_file', filename=filename) }}" alt="Uploaded Image">
|
<img src="user_data/{{filename}}" alt="Uploaded image">
|
||||||
|
|
||||||
<form method="POST" action="/image-print">
|
<form method="POST" action="/image-print">
|
||||||
{% if cut %}
|
{% if cut %}
|
||||||
|
|
|
@ -33,7 +33,7 @@
|
||||||
|
|
||||||
{% if filename %}
|
{% if filename %}
|
||||||
<br><br>
|
<br><br>
|
||||||
<img src="{{ url_for('generated_file', filename=filename) }}" alt="Uploaded Image">
|
<img src="user_data/{{filename}}" alt="Generated Image">
|
||||||
|
|
||||||
<form method="POST" action="/text-print">
|
<form method="POST" action="/text-print">
|
||||||
{% if cut %}
|
{% if cut %}
|
||||||
|
|
Loading…
Reference in a new issue