def specific_params(name=None): if name in list_converters(): converter = converters.get(name) output = { "id": converter.name, "title": converter.title, "convert_url": "convert/%s" % (converter.name), "ct_input": converter.ct_input, "ct_output": converter.ct_output, "ct_schema": converter.ct_schema, "ct_extraparams": converter.extraparams, "description": converter.description, "suffix": "", } return flask.jsonify(output)
def convert(name): start = time.time() document = getattr(flask.request.files.get("file", ""), "stream", None) if not document: import StringIO document = StringIO.StringIO(flask.request.data) with tempfile.NamedTemporaryFile(delete=False) as tmp: chunk = True while chunk: chunk = document.read(10) tmp.file.write(chunk) tmp.file.flush() tmp.file.seek(0) file_size = os.path.getsize(tmp.name) extra_params = flask.request.form.values() if not extra_params: extra_params = flask.request.args.values() needs_additional_files = getattr(converters.get(name), "additional_files", False) try: if needs_additional_files: shx_doc = getattr(flask.request.files.get("shx"), "stream", None) dbf_doc = getattr(flask.request.files.get("dbf"), "stream", None) tmp_shx = tempfile.NamedTemporaryFile() tmp_dbf = tempfile.NamedTemporaryFile() tmp_shx.file.write(shx_doc.read()) tmp_dbf.file.write(dbf_doc.read()) tmp_shx.file.flush() tmp_dbf.file.flush() tmp_shx.file.seek(0) tmp_dbf.file.seek(0) extra_params += [tmp_shx.name, tmp_dbf.name] response = call(name, tmp.name, list(extra_params)) tmp_shx.close() tmp_dbf.close() else: response = call(name, tmp.name, list(extra_params)) except ConversionError as exp: response = base64.b64encode(exp.output) status = 500 content_type = converters.get(name).ct_output except NotImplementedError as exp: response = "" status = 404 content_type = "text/plain" else: status = 200 content_type = converters.get(name).ct_output finally: os.remove(tmp.name) duration = time.time() - start log_details = {"name": name, "size": file_size, "mime": content_type, "duration": duration} logger.info( ( "\nPost conversion details:\n" "\tConverter: {name}\n" "\tFile size: {size} bytes\n" "\tMime-type: {mime}\n" "\tDuration: {duration:.4f} seconds" ).format(**log_details) ) return flask.Response(response, status=status, content_type=content_type)