Commit c35eb5b6 authored by Nicolas Delaby's avatar Nicolas Delaby

Enable image resizing in html_to_odt conversion

 * Use url_parse module to split URL
 * Detect parameters send in image url
 * Resize images according provided parameters


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31742 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2978105f
...@@ -5,6 +5,9 @@ from Products.ERP5Type.Document import newTempOOoDocument ...@@ -5,6 +5,9 @@ from Products.ERP5Type.Document import newTempOOoDocument
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
from Acquisition import aq_base from Acquisition import aq_base
from zope.interface import implements from zope.interface import implements
from OFS.Image import Image as OFSImage
from zLOG import LOG
try: try:
from Products.ERP5OOo.OOoUtils import OOoBuilder from Products.ERP5OOo.OOoUtils import OOoBuilder
import re import re
...@@ -13,9 +16,14 @@ try: ...@@ -13,9 +16,14 @@ try:
import_succeed = 1 import_succeed = 1
except ImportError: except ImportError:
import_succeed = 0 import_succeed = 0
from zLOG import LOG from urllib import unquote
from urlparse import urlparse
try:
# Python >= 2.6
from urlparse import parse_qsl
except ImportError:
from cgi import parse_qsl
REMOTE_URL_RE = re.compile('^((?P<protocol>http(s)?://)(?P<domain>[.a-zA-Z0-9]+)+)?(?P<port>:\d{4})?(?P<path>/?\S*)')
CLEAN_RELATIVE_PATH = re.compile('^../') CLEAN_RELATIVE_PATH = re.compile('^../')
class TransformError(Exception): class TransformError(Exception):
...@@ -82,24 +90,31 @@ class OOOdCommandTransform(commandtransform): ...@@ -82,24 +90,31 @@ class OOOdCommandTransform(commandtransform):
SVG_NAMESPACE = 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0' SVG_NAMESPACE = 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0'
XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink' XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink'
ratio_px_cm = 2.54 / 100. ratio_px_cm = 2.54 / 100.
# Flag to enable modification of OOoBuilder
odt_content_modified = False
for image_tag in image_tag_list: for image_tag in image_tag_list:
frame = image_tag.getparent() frame = image_tag.getparent()
#Try to get image file from ZODB #Try to get image file from ZODB
href_attribute_list = image_tag.xpath('.//@*[name() = "xlink:href"]') href_attribute_list = image_tag.xpath('.//@*[name() = "xlink:href"]')
url = href_attribute_list[0] url = href_attribute_list[0]
matching = REMOTE_URL_RE.match(url) parse_result = urlparse(unquote(url))
if matching is not None: # urlparse return a 6-tuple: scheme, netloc, path, params, query, fragment
path = matching.groupdict().get('path') path = parse_result[2]
if path:
# OOo corrupt relative Links inside HTML content during odt conversion # OOo corrupt relative Links inside HTML content during odt conversion
# <img src="REF.TO.IMAGE" ... /> become <draw:image xlink:href="../REF.TO.IMAGE" ... /> # <img src="REF.TO.IMAGE" ... /> become <draw:image xlink:href="../REF.TO.IMAGE" ... />
# So remove "../" added by OOo # So remove "../" added by OOo
path = CLEAN_RELATIVE_PATH.sub('', path) path = CLEAN_RELATIVE_PATH.sub('', path)
# retrieve http parameters and use them to convert image
query_parameter_string = parse_result[4]
image_parameter_dict = dict(parse_qsl(query_parameter_string))
try: try:
image = self.context.restrictedTraverse(path) image = self.context.restrictedTraverse(path)
except (AttributeError, KeyError): except (AttributeError, KeyError):
#Image not found, this image is probably not hosted by ZODB. Do nothing #Image not found, this image is probably not hosted by ZODB. Do nothing
image = None image = None
if image is not None: if image is not None:
odt_content_modified = True
content_type = image.getContentType() content_type = image.getContentType()
mimetype_list = getToolByName(self.context, mimetype_list = getToolByName(self.context,
'mimetypes_registry').lookup(content_type) 'mimetypes_registry').lookup(content_type)
...@@ -107,25 +122,29 @@ class OOOdCommandTransform(commandtransform): ...@@ -107,25 +122,29 @@ class OOOdCommandTransform(commandtransform):
format = 'png' format = 'png'
if mimetype_list: if mimetype_list:
format = mimetype_list[0].minor() format = mimetype_list[0].minor()
try: if getattr(image, 'meta_type', None) == 'ERP5 Image':
#ERP5 API #ERP5 API
data = image.getData() if 'format' in image_parameter_dict:
height = image.getHeight() format = image_parameter_dict.pop('format')
width = image.getWidth()
except (AttributeError, KeyError): # convert image according parameters
#OFS API mime, image_data = image.convert(format, **image_parameter_dict)
data = image.data image = OFSImage(image.getId(), image.getTitle(), image_data)
height = image.height
width = image.width # image should be OFSImage
data = image.data
width = image.width
height = image.height
if height: if height:
frame.attrib.update({'{%s}height' % SVG_NAMESPACE: '%.3fcm' % (height * ratio_px_cm)}) frame.attrib.update({'{%s}height' % SVG_NAMESPACE: '%.3fcm' % (height * ratio_px_cm)})
if width: if width:
frame.attrib.update({'{%s}width' % SVG_NAMESPACE: '%.3fcm' % (width * ratio_px_cm)}) frame.attrib.update({'{%s}width' % SVG_NAMESPACE: '%.3fcm' % (width * ratio_px_cm)})
new_path = builder.addImage(data, format=format) new_path = builder.addImage(data, format=format)
image_tag.attrib.update({'{%s}href' % XLINK_NAMESPACE: new_path}) image_tag.attrib.update({'{%s}href' % XLINK_NAMESPACE: new_path})
builder.replace('content.xml', etree.tostring(xml_doc, encoding='utf-8', if odt_content_modified:
xml_declaration=True, builder.replace('content.xml', etree.tostring(xml_doc, encoding='utf-8',
pretty_print=False)) xml_declaration=True,
pretty_print=False))
return builder.render() return builder.render()
def includeExternalCssList(self, data): def includeExternalCssList(self, data):
...@@ -145,9 +164,10 @@ class OOOdCommandTransform(commandtransform): ...@@ -145,9 +164,10 @@ class OOOdCommandTransform(commandtransform):
#Try to get css from ZODB #Try to get css from ZODB
href_attribute_list = css_link_tag.xpath('.//@href') href_attribute_list = css_link_tag.xpath('.//@href')
url = href_attribute_list[0] url = href_attribute_list[0]
matching = REMOTE_URL_RE.match(url) parse_result = urlparse(unquote(url))
if matching is not None: # urlparse return a 6-tuple: scheme, netloc, path, params, query, fragment
path = matching.groupdict().get('path') path = parse_result[2]
if path:
try: try:
css_object = self.context.restrictedTraverse(path) css_object = self.context.restrictedTraverse(path)
except (AttributeError, KeyError): except (AttributeError, KeyError):
......
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