Use BytesIO when needed
authorThierry Florac <thierry.florac@onf.fr>
Wed, 22 Jul 2015 16:55:44 +0200
changeset 17 138d2a1faf82
parent 16 a29ce361f097
child 18 825d5e32641f
Use BytesIO when needed
src/pyams_file/file.py
--- a/src/pyams_file/file.py	Wed Jul 22 16:55:18 2015 +0200
+++ b/src/pyams_file/file.py	Wed Jul 22 16:55:44 2015 +0200
@@ -9,8 +9,6 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
-from pyramid.events import subscriber
-from zope.lifecycleevent.interfaces import IObjectAddedEvent
 
 __docformat__ = 'restructuredtext'
 
@@ -27,7 +25,7 @@
 from PIL import Image
 
 # import interfaces
-from pyams_file.interfaces import IFile, IImage, IVideo, IAudio, IFileInfo, FileModifiedEvent, IFileFieldContainer
+from pyams_file.interfaces import IFile, IImage, IVideo, IAudio, IFileInfo, FileModifiedEvent
 from zope.copy.interfaces import ICopyHook, ResumeCopy
 from zope.location.interfaces import IContained
 
@@ -145,6 +143,8 @@
     def _set_data(self, data):
         if isinstance(data, str):
             data = BytesIO(data.encode('utf-8'))
+        elif isinstance(data, bytes):
+            data = BytesIO(data)
         File._set_data(self, data)
         if hasattr(data, 'seek'):
             data.seek(0)
@@ -197,11 +197,11 @@
 
 def get_magic_content_type(input):
     """Get content-type based on magic library"""
-    if hasattr(input, 'seek'):
-        input.seek(0)
-    if hasattr(input, 'read'):
-        input = input.read()
     if magic is not None:
+        if hasattr(input, 'seek'):
+            input.seek(0)
+        if hasattr(input, 'read'):
+            input = input.read()
         return magic.from_buffer(input, mime=True)
     else:
         return None