Mercurial > odfcast
changeset 0:4a34f72f036b
Add initial conversion service
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Fri, 26 Sep 2014 14:21:21 +0200 |
parents | |
children | f2cd3c54a132 |
files | .hgignore castclient.py main.py odfcast/__init__.py odfcast/convert.py odfcast/settings.py |
diffstat | 5 files changed, 146 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Sep 26 14:21:21 2014 +0200 @@ -0,0 +1,18 @@ +syntax: glob +**/*.pyc +**/*.pyo +*.swp +.tmp* +*.egg-info +dist/* +build/* +lib/* +*.orig +*.log +.settings/* +data/* +storage/* +.project +.pydevproject +*.db +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/castclient.py Fri Sep 26 14:21:21 2014 +0200 @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import optparse +import sys + +import requests + +url = "http://127.0.0.1:5000/convert/" + + +def main(): + usage = "usage: %prog [options] infile outfile" + parser = optparse.OptionParser(usage=usage) + parser.add_option("-f", "--format", default="pdf") + parser.add_option("-s", "--host", default="localhost") + parser.add_option("-p", "--port", default="5000") + (options, args) = parser.parse_args() + + if len(args) < 2: + parser.print_usage() + sys.exit(1) + + url = "http://%s:%s/convert/" % (options.host, options.port) + infilename = args[0] + outfilename = args[1] + format = options.format + + files = {'file': open(infilename, 'rb')} + data = {'format': format} + + r = requests.post(url, data=data, files=files) + + if r.status_code == 200: + with open(outfilename, "wb") as f: + f.write(r.content) + print "OK" + else: + print "An error has occured" + print r.status_code, r.headers + sys.exit(2) + + +if __name__ == "__main__": + main()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.py Fri Sep 26 14:21:21 2014 +0200 @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- + +from flask import Flask + +from odfcast.convert import ConvertView + +app = Flask(__name__) +app.config.from_object('odfcast.settings') +app.config.from_envvar('ODFCAST_SETTINGS', silent=True) + + +app.add_url_rule("/convert/", + view_func=ConvertView.as_view( + "convert", + app.config["PY3O_UNO_DRIVER"], + app.config["PY3O_UNO_SERVER_HOSTNAME"], + app.config["PY3O_UNO_SERVER_PORT"], + )) + +if __name__ == "__main__": + app.run()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/odfcast/convert.py Fri Sep 26 14:21:21 2014 +0200 @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- + +import tempfile + +from flask import request, Response +from flask.views import MethodView + +ALLOWED_FORMATS = ["pdf", "doc", "docx", "odt"] + +MIMETYPES = { + "odt": "application/vnd.oasis.opendocument.text", + "doc": "application/msword", + "docx": "application/vnd.openxmlformats-officedocument" + ".wordprocessingml.document", + "pdf": "application/pdf", +} + +DEFAULT_MIMETYPE = "application/octet-stream" + + +class ConvertView(MethodView): + + def __init__(self, pyuno_driver_name="", hostname="localhost", port=2001): + driver_module = self._load_driver_module(pyuno_driver_name) + self.convertor = driver_module.Convertor(hostname, port) + + def _load_driver_module(self, pyuno_driver_name): + return __import__(pyuno_driver_name, globals(), locals(), + ["Convertor"]) + + def post(self): + file = request.files['file'] + format = request.form['format'] + + if format not in ALLOWED_FORMATS: + return "Format %s not allowed" % format, 401 + + outfile = self.convert(file, format) + mimetype = self.get_mimetype_for_format(format) + return Response(outfile, mimetype=mimetype) + + def convert(self, file, format): + infile = tempfile.NamedTemporaryFile() + file.save(infile.name) + outfile = tempfile.NamedTemporaryFile() + + self.convertor.convert(infile.name, outfile.name, format) + + infile.close() + return outfile + + def get_mimetype_for_format(self, format): + return MIMETYPES.get(format, DEFAULT_MIMETYPE)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/odfcast/settings.py Fri Sep 26 14:21:21 2014 +0200 @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +DEBUG = True + +MAX_CONTENT_LENGTH = 50 * 1024 * 1024 # allow 200 MB upload files + +PY3O_UNO_DRIVER = "py3o.renderers.pyuno" +PY3O_UNO_SERVER_HOSTNAME = "localhost" +PY3O_UNO_SERVER_PORT = 2001