From 8ea3ea935add4404a00e894e87bfdc0d44dba22f Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Fri, 3 Jun 2016 15:41:37 +0200
Subject: [PATCH] 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.
---
 product/ERP5/Document/BusinessTemplate.py     | 20 +++++++--------
 .../testBusinessTemplateTwoFileExport.py      | 25 ++++++-------------
 2 files changed, 17 insertions(+), 28 deletions(-)

diff --git a/product/ERP5/Document/BusinessTemplate.py b/product/ERP5/Document/BusinessTemplate.py
index 1798219447..abcbcf70ac 100644
--- a/product/ERP5/Document/BusinessTemplate.py
+++ b/product/ERP5/Document/BusinessTemplate.py
@@ -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.
 
diff --git a/product/ERP5/tests/testBusinessTemplateTwoFileExport.py b/product/ERP5/tests/testBusinessTemplateTwoFileExport.py
index f8f3db7572..3f74ac28c3 100644
--- a/product/ERP5/tests/testBusinessTemplateTwoFileExport.py
+++ b/product/ERP5/tests/testBusinessTemplateTwoFileExport.py
@@ -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')
 
-- 
2.30.9