Commit 6731ccd8 authored by Ivan Tyagov's avatar Ivan Tyagov

Include only local images.

Fix url calculation.
Pass only applicable arguments to conversion API.
parent d9f83777
......@@ -2021,7 +2021,8 @@ return 1
def test_convertWebPageWithEmbeddedZODBImageToImageOnTraversal(self):
"""
Test Web Page using embedded Images into ZODB case (in its HTML body)
Test Web Page conversion to image using embedded Images into its HTML body.
Test various dumb ways to include an image (relative to instance or external ones).
"""
display= 'thumbnail'
convert_kw = {'display':display,
......@@ -2037,36 +2038,39 @@ return 1
web_page_image_size, web_page_file_size = self.getURLSizeList(web_page_document_url, **convert_kw)
self.assertTrue(max(preffered_size_for_display) - max(web_page_image_size) <= 1)
# XXX: how to simulate the case when web page contains (through reference) link to document for which based conversion failed?
# XXX: how to fix case when web page contains (through reference) link to itself (causes infinite recursion)
# images from same instance accessed by reference and wrong arguments (dispay NOT display)
# images from same instance accessed by reference and wrong conversion arguments (dispay NOT display)
# code should be more resilient
upload_file = makeFileUpload('cmyk_sample.jpg')
image = self.portal.image_module.newContent(portal_type='Image',
reference='Embedded-XXX',
version='001',
language='en')
image.setData(upload_file.read())
image.publish()
convert_kw['quality'] = 99 # to not get cached
web_page_document = self.portal.web_page_module.newContent(portal_type="Web Page")
web_page_document.setTextContent('''<b> test </b><img src="Embedded-XXX?format=jpeg&amp;dispay=medium"/>''')
web_page_document.setTextContent('''<b> test </b><img src="Embedded-XXX?format=jpeg&amp;dispay=medium&amp;quality=50"/>''')
self.stepTic()
web_page_document_url = '%s/%s' %(self.portal.absolute_url(), web_page_document.getRelativeUrl())
web_page_image_size, web_page_file_size = self.getURLSizeList(web_page_document_url, **convert_kw)
self.assertTrue(max(preffered_size_for_display) - max(web_page_image_size) <= 1)
# external images
convert_kw['quality'] = 98 # to not get cached
convert_kw['quality'] = 98
web_page_document = self.portal.web_page_module.newContent(portal_type="Web Page")
web_page_document.setTextContent('''<b> test </b><img src="http://www.erp5.com/images/favourite.png"/>
<img style="width: 26px; height: 26px;" src="http://www.erp5.com//images/save2.png" />
<img style="width: 26px; height: 26px;" src="http:////www.erp5.com//images/save2.png" />
<img style="width: 26px; height: 26px;" src="http://www.erp5.com/./images/save2.png" />
''')
self.stepTic()
web_page_document_url = '%s/%s' %(self.portal.absolute_url(), web_page_document.getRelativeUrl())
web_page_image_size, web_page_file_size = self.getURLSizeList(web_page_document_url, **convert_kw)
self.assertTrue(max(preffered_size_for_display) - max(web_page_image_size) <= 1)
# XXX: how to simulate the case when web page contains (through reference) link to document for which based conversion failed?
# XXX: how to fix case when web page contains (through reference) link to itself (causes infinite recursion)
def test_convertToImageOnTraversal(self):
"""
......
......@@ -110,8 +110,10 @@ class OOOdCommandTransform(commandtransform):
url = href_attribute_list[0]
parse_result = urlparse(unquote(url))
# urlparse return a 6-tuple: scheme, netloc, path, params, query, fragment
netloc = parse_result[1]
path = parse_result[2]
if path:
if path and netloc in ('', None):
# it makes sense to include only relative to current site images not remote ones which can be taken by OOo
# OOo corrupt relative Links inside HTML content during odt conversion
# <img src="REF.TO.IMAGE" ... /> become <draw:image xlink:href="../REF.TO.IMAGE" ... />
# So remove "../" added by OOo
......@@ -119,6 +121,9 @@ class OOOdCommandTransform(commandtransform):
# in some cases like Web Page content "/../" can be contained in image URL which will break
# restrictedTraverse calls, our best guess is to remove it
path = path.replace('/../', '')
# remove occurencies of '//' or '///' in path (happens with web pages) and leave
# a traversable relative URL
path = '/'.join([x for x in path.split('/') if x.strip()!=''])
# retrieve http parameters and use them to convert image
query_parameter_string = parse_result[4]
image_parameter_dict = dict(parse_qsl(query_parameter_string))
......@@ -131,6 +136,10 @@ class OOOdCommandTransform(commandtransform):
odt_content_modified = True
content_type = image.getContentType()
format = image_parameter_dict.pop('format', None)
# convert API accepts only a certail range of arguments
for key, value in image_parameter_dict.items():
if key not in ('format', 'display', 'quality', 'resolution',):
image_parameter_dict.pop(key)
if getattr(image, 'convert', None) is not None:
# The document support conversion so perform conversion
# according given parameters
......
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