From cd4df840b60b37aa1e6aad54e335de6cba5d4756 Mon Sep 17 00:00:00 2001 From: TT-392 Date: Tue, 17 Sep 2024 20:12:24 +0200 Subject: [PATCH] Implement file handler with separated data per user --- app.py | 181 +++++++++++++++++++++++++------------------ file_handler.py | 76 ++++++++++++++++++ format_image.py | 2 +- templates/image.html | 2 +- templates/text.html | 2 +- 5 files changed, 184 insertions(+), 79 deletions(-) create mode 100644 file_handler.py diff --git a/app.py b/app.py index 99ae582..3ca3164 100644 --- a/app.py +++ b/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 gen_image import gen_image from printer_info import * from datetime import datetime from print import * from format_image import * +from file_handler import * +from colorama import Fore, Style import os +generated_image_filename = "generated.png" +uploaded_image_filename = "uploaded" #extention is depended on uploaded file format + app = Flask(__name__) -app.config['UPLOAD_FOLDER'] = 'static/uploads' -app.config['TEXT_FOLDER'] = 'static/text' + + 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 = { "DNH": { "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']) def text_template(): + check_for_new_user(session) + if request.method == 'POST': template = templates[request.form["template"]] if request.form["template"] == "Food": @@ -111,12 +127,13 @@ def text_template(): session["text2"] = template["text2"] 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"]) + @app.route('/text-form', methods=['GET', 'POST']) def text_form(): + check_for_new_user(session) + if request.method == 'POST': session["text1"] = { "string": request.form["string1"], @@ -130,28 +147,35 @@ def text_form(): } 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']) def text(): - session["text1"] = templates["DNH"]["text1"] - session["text2"] = templates["DNH"]["text2"] - session["cut"] = templates["DNH"]["cut"] - return render_template('text.html', filename="text.png", text1=session["text1"], text2=session["text2"], cut=session["cut"]) + check_for_new_user(session) + + return render_template('text.html', filename=session["text image path"], text1=session["text1"], text2=session["text2"], cut=session["cut"]) @app.route('/text-print', methods=['GET', 'POST']) def text_print(): + check_for_new_user(session) + 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) if 'cut' in request.form: @@ -162,19 +186,24 @@ def text_print(): print("printing") 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/') +def user_data(filename): + return send_from_directory(get_folder_path(session), filename) @app.route('/uploads/') def uploaded_file(filename): return redirect(url_for('static', filename='uploads/' + filename)) + @app.route('/text/') def generated_file(filename): return redirect(url_for('static', filename='text/' + filename)) + if __name__ == "__main__": app.run(debug=True) diff --git a/file_handler.py b/file_handler.py new file mode 100644 index 0000000..6be4e2f --- /dev/null +++ b/file_handler.py @@ -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"] + diff --git a/format_image.py b/format_image.py index 47efbf2..f8425c4 100644 --- a/format_image.py +++ b/format_image.py @@ -24,4 +24,4 @@ def format_image(path): return image -format_image("static/uploads/upload.png").save("output.png") +#format_image("static/uploads/upload.png").save("output.png") diff --git a/templates/image.html b/templates/image.html index 87312ab..83e33c4 100644 --- a/templates/image.html +++ b/templates/image.html @@ -9,7 +9,7 @@ {% if filename %}

- Uploaded Image + Uploaded image
{% if cut %} diff --git a/templates/text.html b/templates/text.html index cd0ef2d..b7fd7a3 100644 --- a/templates/text.html +++ b/templates/text.html @@ -33,7 +33,7 @@ {% if filename %}

- Uploaded Image + Generated Image {% if cut %}