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