imagestack/tools/pxe_utils/app.py
2026-01-28 09:21:36 +01:00

100 lines
2.6 KiB
Python

from flask import Flask, send_from_directory, abort, request
import json
import requests
import os
import label
import subprocess
app = Flask(__name__)
def pretty_print_POST(req):
print('{}\n{}\r\n{}\r\n\r\n{}'.format(
'-----------START-----------',
req.method + ' ' + req.url,
'\r\n'.join('{}: {}'.format(k, v) for k, v in req.headers.items()),
req.body,
))
FILES_DIR = os.path.join(os.path.dirname(__file__), "files")
@app.route('/')
def index():
return("↑ ↑ ↓ ↓ ← → ← → B A")
@app.route('/snipe_api')
def home():
with open("./api_key.txt", "r") as f:
return f.read().strip()
@app.route('/late_command.sh')
def download_late():
try:
return send_from_directory(FILES_DIR, "late_command.sh", as_attachment=True)
except FileNotFoundError:
abort(404, description="File not found")
@app.route('/preseed_late.sh')
def download_file():
try:
return send_from_directory(FILES_DIR, "preseed_late.sh", as_attachment=True)
except FileNotFoundError:
abort(404, description="File not found")
@app.route('/preseed.cfg')
def download_pre():
try:
return send_from_directory(FILES_DIR, "preseed.cfg", as_attachment=True)
except FileNotFoundError:
abort(404, description="File not found")
@app.route('/change_language.sh')
def download_lang():
try:
return send_from_directory(FILES_DIR, "change_language.sh", as_attachment=True)
except FileNotFoundError:
abort(404, description="File not found")
@app.route("/label-demo", methods=["POST"])
def build():
typst_bytes = request.get_data()
typst_string = typst_bytes.decode("utf-8")
try:
j = json.loads(typst_string)
except json.JSONDecodeError as e:
print(e)
return "Failed"
pdf = label.compile_pdf(j)
# print(pdf)
# with open("./temp.pdf","wb") as f:
# f.write(pdf)
subprocess.run(
["lpr", "-H", "10.200.4.12", "-P", "DYMO", "-o", "landscape", "-"], input=pdf
)
return "Success"
@app.route("/label", methods=["POST"])
def batch():
typst_bytes = request.get_data()
typst_string = typst_bytes.decode("utf-8")
try:
j = json.loads(typst_string)
except json.JSONDecodeError as e:
print(e)
return "Failed"
pdf = label.compile_pdf(j,batch=True)
# print(pdf)
# with open("./temp.pdf","wb") as f:
# f.write(pdf)
print("Would print")
return "Success"
subprocess.run(
["lpr", "-H", "10.200.4.12", "-P", "DYMO", "-o", "landscape", "-"], input=pdf
)
return "Success"
if __name__ == '__main__':
app.run(debug=True)