This commit is contained in:
zman 2025-04-07 16:34:02 -04:00
parent 4017c732d5
commit eb33a3868f

25
app.py
View File

@ -8,9 +8,19 @@ from brother_ql.backends.helpers import send
from brother_ql.raster import BrotherQLRaster
from PIL import Image
import mimetypes
import logging
from logging.handlers import RotatingFileHandler
app = Flask(__name__)
# Set up logging
log_file = 'log.txt'
log_handler = RotatingFileHandler(log_file, maxBytes=10*1024*1024, backupCount=5) # 10MB per log file, keep 5 backups
log_handler.setLevel(logging.INFO)
log_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
log_handler.setFormatter(log_formatter)
app.logger.addHandler(log_handler)
# Create a queue to hold the files for processing
file_queue = queue.Queue()
@ -33,7 +43,7 @@ def convert_pdf_to_image(pdf_path):
return images[0] # We will only print the first page for now
return None
except Exception as e:
print(f"Error converting PDF: {str(e)}")
app.logger.error(f"Error converting PDF: {str(e)}")
return None
# Function to print address labels
@ -52,7 +62,7 @@ def print_address_label(pdf_path):
qlr = BrotherQLRaster(printer_model)
qlr.exception_on_warning = True
print("Converting image to printer instructions...")
app.logger.info("Converting image to printer instructions...")
instructions = convert(
qlr=qlr,
images=[image], # Pass as a list with the single image
@ -66,17 +76,17 @@ def print_address_label(pdf_path):
cut=True
)
print("Sending to printer...")
app.logger.info("Sending to printer...")
send(
instructions=instructions,
printer_identifier=printer,
backend_identifier=backend,
blocking=True
)
print("Print job sent successfully")
app.logger.info("Print job sent successfully")
except Exception as e:
print(f"Error during printing: {str(e)}")
app.logger.error(f"Error during printing: {str(e)}")
# Worker thread that processes files from the queue
def process_queue():
@ -86,6 +96,7 @@ def process_queue():
if file_path is None: # Shutdown signal
break
# Process the file (convert and print)
app.logger.info(f"Processing file: {file_path}")
print_address_label(file_path)
# Signal that the task is done
file_queue.task_done()
@ -98,21 +109,25 @@ thread.start()
def upload_file():
# Check if a file is part of the request
if 'file' not in request.files:
app.logger.warning('No file part in request')
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
app.logger.warning('No selected file')
return jsonify({'error': 'No selected file'}), 400
# Check file type (ensure it's a PDF)
mime_type, _ = mimetypes.guess_type(file.filename)
if mime_type != 'application/pdf':
app.logger.warning(f"Invalid file type: {mime_type}. Only PDF files are allowed.")
return jsonify({'error': 'Only PDF files are allowed'}), 400
# Save the file to the uploads folder
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
file.save(file_path)
app.logger.info(f"File {file.filename} uploaded and saved to {file_path}")
# Add the file path to the queue for processing
file_queue.put(file_path)