Mercurial > odfcast > odfcast
comparison odfcast/convert.py @ 29:1dadc59c4b9a
Make the conversion more error prone
author | Björn Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Thu, 16 Oct 2014 10:03:32 +0200 |
parents | 0e40926735e1 |
children | 9d65de2ebe22 |
comparison
equal
deleted
inserted
replaced
28:1dd40e13da74 | 29:1dadc59c4b9a |
---|---|
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 | 2 |
3 import logging | |
3 import tempfile | 4 import tempfile |
4 | 5 |
5 from flask import request, Response, json, render_template | 6 from flask import request, Response, json, render_template |
6 from flask.views import MethodView | 7 from flask.views import MethodView |
7 | 8 |
8 from py3o.template import Template | 9 from py3o.template import Template |
9 | 10 |
10 from PyPDF2 import PdfFileMerger | 11 from PyPDF2 import PdfFileMerger |
11 | 12 |
13 log = logging.getLogger(__name__) | |
12 | 14 |
13 ALLOWED_FORMATS = ["pdf", "doc", "docx", "odt"] | 15 ALLOWED_FORMATS = ["pdf", "doc", "docx", "odt"] |
14 | 16 |
15 PDF_MIMETYPE = "application/pdf" | 17 PDF_MIMETYPE = "application/pdf" |
16 | 18 |
38 def is_format_supported(self, fformat): | 40 def is_format_supported(self, fformat): |
39 return fformat and fformat.lower() in ALLOWED_FORMATS | 41 return fformat and fformat.lower() in ALLOWED_FORMATS |
40 | 42 |
41 def post(self): | 43 def post(self): |
42 ffile = request.files['file'] | 44 ffile = request.files['file'] |
45 if not ffile.filename: | |
46 return "Please upload a file for conversion", 401 | |
47 | |
43 fformat = request.form['format'] | 48 fformat = request.form['format'] |
44 | |
45 if not self.is_format_supported(fformat): | 49 if not self.is_format_supported(fformat): |
46 return "Format %s not allowed" % fformat, 401 | 50 return "Format %s not allowed" % fformat, 401 |
47 | 51 |
52 mimetype = self.get_mimetype_for_format(fformat) | |
53 | |
48 infile = self.save_form_file(ffile) | 54 infile = self.save_form_file(ffile) |
49 outfile = self.convert(infile, fformat) | 55 try: |
50 mimetype = self.get_mimetype_for_format(fformat) | 56 outfile = self.convert(infile, fformat) |
57 except: | |
58 log.exception("Conversion error") | |
59 return "Conversion error", 500 | |
60 | |
51 return Response(outfile, mimetype=mimetype) | 61 return Response(outfile, mimetype=mimetype) |
52 | 62 |
53 def get(self): | 63 def get(self): |
54 return render_template("convert.html") | 64 return render_template("convert.html") |
55 | 65 |