From 6ab652d3b8c895c8c283b79beaf8aa44000743f9 Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sun, 4 Nov 2018 11:54:21 +0100 Subject: [PATCH] Initial commit of rough idea --- Dockerfile | 13 +++++++++++++ requirements.txt | 3 +++ server.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 Dockerfile create mode 100644 requirements.txt create mode 100644 server.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d0e406e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM python:3-slim + +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + pandoc \ + texlive-xetex \ + texlive-latex-base \ + texlive-latex-base \ + texlive-fonts-recommended \ + texlive-latex-extra +WORKDIR /app +COPY ./requirements.txt /app/requirements.txt +RUN pip install -r requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e6acba7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +pypandoc +flask +gunicorn diff --git a/server.py b/server.py new file mode 100644 index 0000000..ae007de --- /dev/null +++ b/server.py @@ -0,0 +1,46 @@ +import logging +import uuid +import tempfile +import os +import flask_fs as fs +from flask import Flask, abort, request, Response, send_file +import pypandoc + +app = Flask(__name__) +fs.init_app(app) +app.logger.setLevel(logging.INFO) + +@app.route('/') +def index(): + return 'Hello' + +@app.route('/convert', methods=['POST']) +def convert(): + if 'input' not in request.files: + abort(400) + if 'outputs' not in request.form: + abort(400) + + job_name = uuid.uuid4().hex + job_dir = tempfile.mkdtemp(suffix='-' + job_name) + input_file = request.files['input'] + input_base, input_ext = input_file.filename.rsplit('.') + input_path = os.path.join(job_dir, 'input.' + input_ext) + input_file.save(input_path) + + + outputs = request.form['outputs'].split(',') + results = [] + for output in outputs: + output_filename = '{0}.{1}'.format(input_base, output) + output_path = os.path.join(job_dir, output_filename) + print('Converting {0} to {1}'.format(input_path, output_path)) + pypandoc.convert(input_path, output, outputfile=output_path) + results.append({ + 'input': input_path, + 'output': output_path, + 'format': output, + }) + if len(results) == 1: + return send_file(results[0]['output']) + return Response(str(results))