Add check to know if content_type is defined as bytes or string
authorThierry Florac <thierry.florac@onf.fr>
Wed, 07 Jun 2017 09:28:15 +0200
changeset 41 509aefe2f45c
parent 40 0ee62085ea82
child 42 04a238453198
Add check to know if content_type is defined as bytes or string
src/pyams_media/media.py
src/pyams_media/utility.py
--- a/src/pyams_media/media.py	Tue May 16 11:16:15 2017 +0200
+++ b/src/pyams_media/media.py	Wed Jun 07 09:28:15 2017 +0200
@@ -52,9 +52,12 @@
 @adapter_config(context=IFile, provides=IMediaInfo)
 def MediaInfoFactory(context):
     """Media info adapter"""
-    if not (context.content_type.startswith(b'audio/') or
-            context.content_type.startswith(b'video/') or
-            context.content_type in (CUSTOM_AUDIO_TYPES + CUSTOM_VIDEO_TYPES)):
+    content_type = context.content_type
+    if isinstance(content_type, bytes):
+        content_type = content_type.decode()
+    if not (content_type.startswith('audio/') or
+            content_type.startswith('video/') or
+            content_type in (CUSTOM_AUDIO_TYPES + CUSTOM_VIDEO_TYPES)):
         return None
     annotations = IAnnotations(context)
     info = annotations.get(MEDIA_INFO_KEY)
@@ -161,7 +164,9 @@
     # Don't convert images or already converted files!
     if IMediaConversion.providedBy(media):
         return
-    content_type = media.content_type.decode() if media.content_type else None
+    content_type = media.content_type
+    if isinstance(content_type, bytes):
+        content_type = content_type.decode()
     if (not content_type) or content_type.startswith('image/'):
         return
     # Try to use FFMpeg if content type is unknown...
--- a/src/pyams_media/utility.py	Tue May 16 11:16:15 2017 +0200
+++ b/src/pyams_media/utility.py	Wed Jun 07 09:28:15 2017 +0200
@@ -50,7 +50,9 @@
 
     def check_media_conversion(self, media):
         """Check if conversion is needed for given media"""
-        content_type = media.content_type.decode() if media.content_type else None
+        content_type = media.content_type
+        if isinstance(content_type, bytes):
+            content_type = content_type.decode()
         if self.audio_formats and \
            (content_type.startswith('audio/') or (content_type in CUSTOM_AUDIO_TYPES)):
             requested_formats = self.audio_formats