Files
littleprynter/src/web.py
2026-05-22 11:01:06 +02:00

116 lines
4.1 KiB
Python

from flask import flash
from werkzeug.utils import secure_filename
import time
import os
from task import TextTask, ImageTask, CutTask
class Web(object):
"""Web is the class that gets all of the information from web calls
( API and Web page ) and provides checks before sending stuff to printing"""
def __init__(self, app, print_queue):
super(Web).__init__()
self.print_queue = print_queue
self.app = app
def print_sms(self, texte, sign: str) -> bool:
"""
Get text and a signature, prints the text and cuts after that.
"""
self.app.logger.debug("Printing : " + str(texte) + " from " + str(sign))
try:
# We create two new tasks and add them directly to the queue
# TODO: this might need to be improved because
# !! there is no garantee !! that both the SMS task and the Cut task
# are added back to back, another task could be
# inserted between the two.
sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign))
cut = self.print_queue.enqueue(CutTask())
except Exception as e:
self.app.logger.error(e)
raise RuntimeError("Could not add SMS to queue, " + str(e)) from e
self.app.logger.info("Added two new tasks at position %s and %s", sms, cut)
return True
def print_image(self, image, sign: str) -> bool:
"""
Get an image and a signature, prints the image and cuts after that.
"""
try:
file_uploaded = self.upload_file(image)
except Exception as e:
self.app.logger.error(e)
raise RuntimeError("Could not upload file") from e
if file_uploaded:
self.app.logger.debug("File has been uploaded, printing...")
try:
img = self.print_queue.enqueue(
ImageTask(
os.path.join(
self.app.config["UPLOAD_FOLDER"],
secure_filename(image.filename),
),
signature=sign,
process=True,
)
)
cut = self.print_queue.enqueue(CutTask())
except Exception as e:
raise RuntimeError("Could not add IMG to queue" + str(e)) from e
self.app.logger.info("Added two new tasks at position %s and %s", img, cut)
return True
def login(self, username: str, password: str) -> bool:
"""Not implemented"""
return
def logout(self, username: str, password: str) -> bool:
"""Not implemented"""
return
def allowed_file(self, filename) -> bool:
self.app.logger.debug("Is the filename allowed ?")
return (
"." in filename
and filename.rsplit(".", 1)[1].lower()
in self.app.config["ALLOWED_EXTENSIONS"]
)
def upload_file(self, image) -> bool:
self.app.logger.debug("Validating file")
if image:
if self.allowed_file(image.filename):
filename = secure_filename(image.filename)
self.app.logger.debug("File valid")
try:
image.save(os.path.join(self.app.config["UPLOAD_FOLDER"], filename))
except OSError as e:
self.app.logger.error("Could not save file %s", e)
return False
self.app.logger.debug(
"File saved to "
+ str(os.path.join(self.app.config["UPLOAD_FOLDER"], filename))
)
return True
else:
self.app.logger.error(
"Could not save file because the filename is forbidden"
)
return False
else:
self.app.logger.error(
"Could not save file, it seems to be null ? : " + str(filename)
)
return False
def get_queue_state(self):
"""Return current queue state"""
return self.print_queue.get_queue_state()