From 28025661317022db5af278417fd6000ae6caa6d6 Mon Sep 17 00:00:00 2001
From: Frank Sauerburger <frank@sauerburger.com>
Date: Sun, 29 Mar 2020 14:53:41 +0200
Subject: [PATCH] Evaluate 'printer' field

---
 assets/upload.js |  4 +++-
 web2print.py     | 13 ++++++++-----
 2 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/assets/upload.js b/assets/upload.js
index 8f889ea..30c05ec 100644
--- a/assets/upload.js
+++ b/assets/upload.js
@@ -39,7 +39,9 @@ function uploadFile(file) {
     $("#dropzone").addClass("drop-uploading");
     let formData = new FormData();
 
-    formData.append('document', file)
+    formData.append('document', file);
+    formData.append('printer', $("#printer").val());
+
     fetch("/", {
         method: 'POST',
         body: formData
diff --git a/web2print.py b/web2print.py
index a5382f6..499d5ef 100644
--- a/web2print.py
+++ b/web2print.py
@@ -5,9 +5,6 @@ import cups
 from flask import Flask, render_template, request, send_from_directory
 from werkzeug.utils import secure_filename
 
-CUPS_SERVER = "10.1.9.104:631"
-cups.setServer(CUPS_SERVER)
-
 app = Flask(__name__)
 app.config['UPLOAD_FOLDER'] = "/tmp/web2print"
 os.makedirs("/tmp/web2print", exist_ok=True)
@@ -23,16 +20,22 @@ def get_printers():
 @app.route('/', methods=["POST", "GET"])
 def index():
     if (request.method == "GET") or ("document" not in request.files) or \
-            (not request.files["document"].filename):
+            (not request.files["document"].filename) or \
+            ("printer" not in request.form) or (not request.form["printer"]):
         return render_template("index.html", printers=printers)
 
+
+    printer = request.form["printer"]
+    if printer not in printers:
+        printer = printers[0]
+
     file = request.files['document']
     filename = secure_filename(file.filename)
     local_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
     file.save(local_path)
 
     conn = cups.Connection()
-    conn.printFile(printers[0], local_path, "web2print-%d" % time(),
+    conn.printFile(printer, local_path, "web2print-%d" % time(),
                    {"fit-to-page": "True", "sides": "two-sided-long-edge"})
 
     try:
-- 
GitLab