Commit b3328e6b authored by Tristan Cavelier's avatar Tristan Cavelier

+1

parent c827410b
......@@ -37,19 +37,22 @@ from base64 import b64decode
def keyNameToOption(key_name, prefix=""):
return "--" + prefix + key_name.replace("_", "-")
def hackMimetypeToFormatTRISTAN(mimetype):
if mimetype in ("application/x-pdf-report", "application/pdf"): return "pdf"
if mimetype in ("text/html",): return "html"
return mimetype
class Handler(object):
"""ImageMagic Handler is used to handler images."""
implements(IHandler)
def __init__(self, base_folder_url, data, source_format, **kw):
def mimetypeToFormat(self, mimetype):
if mimetype == "application/pdf": return "pdf"
if mimetype == "text/html": return "html"
if "/" in mimetype:
raise ValueError("Unhandled mimetype %r" % mimetype)
return mimetype
def __init__(self, base_folder_url, data, source_mimetype, **kw):
""" Load pdf document """
source_format = hackMimetypeToFormatTRISTAN(source_format)
self.source_mimetype = source_mimetype
source_format = self.mimetypeToFormat(source_mimetype)
self.base_folder_url = base_folder_url
self.file = File(base_folder_url, data, source_format)
self.environment = kw.get("env", {})
......@@ -66,10 +69,10 @@ class Handler(object):
return "file://" + path
raise ValueError("path %r is not absolute" % path)
def convert(self, destination_format=None, **kw):
def convert(self, destination_mimetype=None, **kw):
"""Convert a image"""
destination_format = hackMimetypeToFormatTRISTAN(destination_format)
logger.debug("wkhtmltopdf convert: %s > %s" % (self.file.source_format, destination_format))
logger.debug("wkhtmltopdf convert: %r > %r" % (self.source_mimetype, destination_mimetype))
destination_format = self.mimetypeToFormat(destination_mimetype)
output_path = self.makeTempFile(destination_format)
command = self.makeWkhtmltopdfCommandList(
self.convertPathToUrl(self.file.getUrl()),
......@@ -94,7 +97,7 @@ class Handler(object):
"""Returns a dictionary with all metadata of document.
along with the metadata.
"""
return NotImplementedError
raise NotImplementedError
def setMetadata(self, metadata={}):
"""Returns image with new metadata.
......
......@@ -78,27 +78,27 @@ class Manager(object):
self.mimetype_registry = self.kw.pop("mimetype_registry")
self.handler_dict = self.kw.pop("handler_dict")
def convertFile(self, file, source_format, destination_format, zip=False,
def convertFile(self, file, source_mimetype, destination_mimetype, zip=False,
refresh=False, conversion_kw={}):
"""Returns the converted file in the given format.
Keywords arguments:
file -- File as string in base64
source_format -- Format of original file as string
destination_format -- Target format as string
source_mimetype -- Mimetype of original file as string
destination_mimetype -- Target mimetype as string
zip -- Boolean Attribute. If true, returns the file in the form of a
zip archive
"""
self.kw['zip'] = zip
self.kw['refresh'] = refresh
handler_class = getHandlerClass(source_format,
destination_format,
handler_class = getHandlerClass(source_mimetype,
destination_mimetype,
self.mimetype_registry,
self.handler_dict)
handler = handler_class(self._path_tmp_dir,
decodestring(file),
source_format,
source_mimetype,
**self.kw)
decode_data = handler.convert(destination_format, **conversion_kw)
decode_data = handler.convert(destination_mimetype, **conversion_kw)
return encodestring(decode_data)
def updateFileMetadata(self, file, source_format, metadata_dict):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment