From 163ac4bdf4387e11297d794a3a76577f9a704924 Mon Sep 17 00:00:00 2001 From: Paolo Asperti Date: Wed, 30 Jun 2021 22:17:57 +0200 Subject: [PATCH] docmaster first commit --- docmaster/Dockerfile | 19 +++++++++ docmaster/app.py | 91 +++++++++++++++++++++++++++++++++++++++++++ docmaster/config.json | 23 +++++++++++ docmaster/start.sh | 8 ++++ 4 files changed, 141 insertions(+) create mode 100644 docmaster/Dockerfile create mode 100755 docmaster/app.py create mode 100644 docmaster/config.json create mode 100755 docmaster/start.sh diff --git a/docmaster/Dockerfile b/docmaster/Dockerfile new file mode 100644 index 0000000..2794778 --- /dev/null +++ b/docmaster/Dockerfile @@ -0,0 +1,19 @@ + +ARG BUILD_FROM=ghcr.io/hassio-addons/base/amd64:9.2.2 +FROM ${BUILD_FROM} + +# --no-cache +RUN \ + apk -U upgrade && \ + apk add ghostscript py3-flask py3-gunicorn && \ + apk add py3-unoconv --repository=http://dl-cdn.alpinelinux.org/alpine/edge/testing/ && \ + apk add font-noto-all ttf-ubuntu-font-family ttf-freefont ttf-font-awesome ttf-opensans && \ + apk add samba-client jq + +COPY start.sh /app/ +COPY app.py /app/ + +EXPOSE 6000 +HEALTHCHECK CMD curl --fail http://localhost:6000/ || exit 1 + +ENTRYPOINT ["/app/start.sh"] diff --git a/docmaster/app.py b/docmaster/app.py new file mode 100755 index 0000000..3b24d28 --- /dev/null +++ b/docmaster/app.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python3 + +from flask import Flask, jsonify, request, send_from_directory +import tempfile +import subprocess +import os +import sys + +app = Flask(__name__) + +@app.route('/') +def helloWorld(): + return "Hi! Please check usage docs.\n" + + +@app.route('/status') +def getStatus(): + PRINTER_HOST = os.environ.get("PRINTER_HOST") + return jsonify({"status": "OK", "printer_host": PRINTER_HOST}) + + +def getFile(tmpDir): + if request.files.get('file', None): + f = request.files['file'] + infile = tmpDir.name + '/' + f.name + f.save(infile) + elif request.form.get('smbfile'): + smbhost = request.form.get('smbhost', None) + smbuser = request.form.get('smbuser', None) + smbpass = request.form.get('smbpass', None) + smbworkgroup = request.form.get('smbworkgroup', None) + smbshare = request.form.get('smbshare', None) + smbdir = request.form.get('smbdir', None) + smbfile = request.form.get('smbfile', None) + v = [ smbhost, smbuser, smbpass, smbworkgroup, smbshare, smbdir, smbfile ] + if (all(v)): + os.chdir(tmpDir.name) + r = subprocess.run(['smbclient', + '--user', smbuser, + '--workgroup', smbworkgroup, + '--directory', smbdir, + '--command', 'get "%s"' % (smbfile), + '//%s/%s' % (smbhost, smbshare), + smbpass ] ) + if r.returncode != 0: + return None + infile = tmpDir.name + '/' + smbfile + else: + return None + return infile + + +@app.route('/convert/pdf', methods=["POST"]) +def toPDF(): + tmpDir = tempfile.TemporaryDirectory() + infile = getFile(tmpDir) + pdffile = infile + '.pdf' + + r = subprocess.run(['/usr/bin/unoconv', '--output', pdffile, infile]) + if r.returncode != 0: + return jsonify({"status": "conversion error"}) + + return send_from_directory(directory=tmpDir.name, + filename=os.path.basename(pdffile), + mimetype='application/pdf', + as_attachment=True) + + +@app.route('/print', methods=["POST"]) +def print(): + PRINTER_HOST = os.environ.get("PRINTER_HOST") + if not PRINTER_HOST: + raise ValueError("You have to set the PRINTER_HOST environment variable") + + tmpDir = tempfile.TemporaryDirectory() + infile = getFile(tmpDir) + + unoconv = subprocess.Popen(['/usr/bin/unoconv','--stdout',infile], stdout=subprocess.PIPE) + pdf2ps = subprocess.Popen(['/usr/bin/pdf2ps','-','-'], stdin=unoconv.stdout, stdout=subprocess.PIPE) + unoconv.wait() + unoconv.stdout.close() + printer = subprocess.Popen(['/usr/bin/nc','-w','1', PRINTER_HOST,'9100'], stdin=pdf2ps.stdout) + pdf2ps.wait() + pdf2ps.stdout.close() + printer.wait() + + return jsonify({"status": "ok"}) + + +if __name__ == '__main__': + app.run(port=6000) diff --git a/docmaster/config.json b/docmaster/config.json new file mode 100644 index 0000000..cec1a41 --- /dev/null +++ b/docmaster/config.json @@ -0,0 +1,23 @@ +{ + "name": "DocMaster", + "url": "https://git.asperti.com/paspo/hassio-addons", + "version": "0.1", + "slug": "docmaster", + "description": "document conversion and print", + "startup": "services", + "boot": "auto", + "audio": false, + "gpio": false, + "arch": [ + "amd64" + ], + "ports": { + "6000/tcp": 6000 + }, + "options": { + "printer_host": "192.168.1.30" + }, + "schema": { + "printer_host": "str" + } +} diff --git a/docmaster/start.sh b/docmaster/start.sh new file mode 100755 index 0000000..ce3eb1d --- /dev/null +++ b/docmaster/start.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +CONFIG_PATH=/data/options.json +PRINTER_HOST=$(jq --raw-output ".printer_host" $CONFIG_PATH) + +cd /app +gunicorn app:app --bind 0.0.0.0:6000 --workers=2