a
This commit is contained in:
parent
4017c732d5
commit
eb33a3868f
25
app.py
25
app.py
@ -8,9 +8,19 @@ from brother_ql.backends.helpers import send
|
|||||||
from brother_ql.raster import BrotherQLRaster
|
from brother_ql.raster import BrotherQLRaster
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import mimetypes
|
import mimetypes
|
||||||
|
import logging
|
||||||
|
from logging.handlers import RotatingFileHandler
|
||||||
|
|
||||||
app = Flask(__name__)
|
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
|
# Create a queue to hold the files for processing
|
||||||
file_queue = queue.Queue()
|
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 images[0] # We will only print the first page for now
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error converting PDF: {str(e)}")
|
app.logger.error(f"Error converting PDF: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Function to print address labels
|
# Function to print address labels
|
||||||
@ -52,7 +62,7 @@ def print_address_label(pdf_path):
|
|||||||
qlr = BrotherQLRaster(printer_model)
|
qlr = BrotherQLRaster(printer_model)
|
||||||
qlr.exception_on_warning = True
|
qlr.exception_on_warning = True
|
||||||
|
|
||||||
print("Converting image to printer instructions...")
|
app.logger.info("Converting image to printer instructions...")
|
||||||
instructions = convert(
|
instructions = convert(
|
||||||
qlr=qlr,
|
qlr=qlr,
|
||||||
images=[image], # Pass as a list with the single image
|
images=[image], # Pass as a list with the single image
|
||||||
@ -66,17 +76,17 @@ def print_address_label(pdf_path):
|
|||||||
cut=True
|
cut=True
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Sending to printer...")
|
app.logger.info("Sending to printer...")
|
||||||
send(
|
send(
|
||||||
instructions=instructions,
|
instructions=instructions,
|
||||||
printer_identifier=printer,
|
printer_identifier=printer,
|
||||||
backend_identifier=backend,
|
backend_identifier=backend,
|
||||||
blocking=True
|
blocking=True
|
||||||
)
|
)
|
||||||
print("Print job sent successfully")
|
app.logger.info("Print job sent successfully")
|
||||||
|
|
||||||
except Exception as e:
|
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
|
# Worker thread that processes files from the queue
|
||||||
def process_queue():
|
def process_queue():
|
||||||
@ -86,6 +96,7 @@ def process_queue():
|
|||||||
if file_path is None: # Shutdown signal
|
if file_path is None: # Shutdown signal
|
||||||
break
|
break
|
||||||
# Process the file (convert and print)
|
# Process the file (convert and print)
|
||||||
|
app.logger.info(f"Processing file: {file_path}")
|
||||||
print_address_label(file_path)
|
print_address_label(file_path)
|
||||||
# Signal that the task is done
|
# Signal that the task is done
|
||||||
file_queue.task_done()
|
file_queue.task_done()
|
||||||
@ -98,21 +109,25 @@ thread.start()
|
|||||||
def upload_file():
|
def upload_file():
|
||||||
# Check if a file is part of the request
|
# Check if a file is part of the request
|
||||||
if 'file' not in request.files:
|
if 'file' not in request.files:
|
||||||
|
app.logger.warning('No file part in request')
|
||||||
return jsonify({'error': 'No file part'}), 400
|
return jsonify({'error': 'No file part'}), 400
|
||||||
|
|
||||||
file = request.files['file']
|
file = request.files['file']
|
||||||
|
|
||||||
if file.filename == '':
|
if file.filename == '':
|
||||||
|
app.logger.warning('No selected file')
|
||||||
return jsonify({'error': 'No selected file'}), 400
|
return jsonify({'error': 'No selected file'}), 400
|
||||||
|
|
||||||
# Check file type (ensure it's a PDF)
|
# Check file type (ensure it's a PDF)
|
||||||
mime_type, _ = mimetypes.guess_type(file.filename)
|
mime_type, _ = mimetypes.guess_type(file.filename)
|
||||||
if mime_type != 'application/pdf':
|
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
|
return jsonify({'error': 'Only PDF files are allowed'}), 400
|
||||||
|
|
||||||
# Save the file to the uploads folder
|
# Save the file to the uploads folder
|
||||||
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
||||||
file.save(file_path)
|
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
|
# Add the file path to the queue for processing
|
||||||
file_queue.put(file_path)
|
file_queue.put(file_path)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user