Commit 8ea3ea93 authored by Julien Muchembled's avatar Julien Muchembled

BT: when guessing extension from content, do not try do decode as base64

Such images don't seem to exist.
On the other side, it may be useful to test raw data.

Note that the code to get data duplicates what's done later in export().
One should problably guess extension at the last moment.
parent 34f77c3b
......@@ -103,8 +103,6 @@ except TypeError:
pass
cache_database = threading.local()
from Products.MimetypesRegistry.common import MimeTypeException
import base64
import binascii
import imghdr
# those attributes from CatalogMethodTemplateItem are kept for
......@@ -763,16 +761,16 @@ class ObjectTemplateItem(BaseTemplateItem):
yield getattr(document_base, "reference", None)
# Try to guess the extension based on the title of the document
yield getattr(document_base, "title", None)
# Try to guess from content
if exported_property_type == 'data':
# XXX maybe decoding the whole file just for the extension is an overkill
data = str(document.__dict__.get('data'))
try:
decoded_data = base64.decodestring(data)
except binascii.Error:
LOG('BusinessTemplate ', 0, 'data is not base 64')
else:
data = getattr(document_base, exported_property_type, None)
if data:
try:
data = str(data)
except Exception:
return
for test in imghdr.tests:
extension = test(decoded_data, None)
extension = test(data, None)
if extension:
yield 'x.' + extension
......@@ -783,7 +781,7 @@ class ObjectTemplateItem(BaseTemplateItem):
1. Try to guess extension by the id of the document
2. Try to guess extension by the title of the document
3. Try to guess extension by the reference of the document
4. In the case of an image, try to guess extension by Base64 representation
4. Try to guess from content (only image data is tested)
If there's a content type, we only return an extension that matches.
......
......@@ -267,34 +267,29 @@ class TestBusinessTemplateTwoFileExport(ERP5TypeTestCase):
self.assertEqual(image_page.getProperty(property_id), property_value)
def test_twoFileImportExportForImageIdentifyingTypeByBase64(self):
png_data = """iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==""".decode("base64")
def test_twoFileImportExportForImageIdentifyingTypeByContent(self):
"""
Test Business Template Import And Export With Image In Image Module
where extension is found by Base64 representation
"""
image_data = """iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg=="""
self._checkTwoFileImportExportForImageInImageModule(dict(
title = "foo",
data = image_data,
data = self.png_data,
portal_type = "Image",
), '.png')
def test_twoFileImportExportForImageIdentifyingTypeByContentType(self):
"""
Test Business Template Import And Export With Image In Image Module
where extension (.jpg) is found by content_type
"""
image_data = """MalformedBase64HereiVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg=="""
self._checkTwoFileImportExportForImageInImageModule(dict(
title = "foo",
data = image_data,
data = self.png_data, # just to check priorities
content_type = "image/jpeg",
portal_type = "Image",
), '.jpg')
......@@ -304,13 +299,9 @@ AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
Test Business Template Import And Export With Image In Image Module
where extension is not identified, so it is exported as '.bin'
"""
image_data = """MalformedBase64HereiVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg=="""
self._checkTwoFileImportExportForImageInImageModule(dict(
title = "foo",
data = image_data,
data = "malformed data",
portal_type = "Image",
), '.bin')
......
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