Added check on content type when adding conversion
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 13:44:23 +0200
changeset 46 55d5fde71e8f
parent 45 bf32240cb696
child 47 44889107d310
Added check on content type when adding conversion
src/pyams_media/media.py
src/pyams_media/video.py
--- a/src/pyams_media/media.py	Mon Sep 11 13:43:22 2017 +0200
+++ b/src/pyams_media/media.py	Mon Sep 11 13:44:23 2017 +0200
@@ -84,10 +84,15 @@
         alsoProvides(target, IMediaConversion)
         if extension is None:
             extension = guess_extension(format)
-        target_name = '{name}{width}.{extension}'.format(name=target.content_type.decode().split('/', 1)[0]
-                                                             if target.content_type else 'media',
+        content_type = target.content_type
+        if isinstance(content_type, bytes):
+            content_type = content_type.decode()
+        target_name = '{name}{width}.{extension}'.format(name=content_type.split('/', 1)[0]
+                                                             if content_type else 'media',
                                                          width='-{0}'.format(width) if width else '',
                                                          extension=extension)
+        if target_name in self:
+            del self[target_name]
         target.filename = target_name
         self[target_name] = target
         return target
--- a/src/pyams_media/video.py	Mon Sep 11 13:43:22 2017 +0200
+++ b/src/pyams_media/video.py	Mon Sep 11 13:44:23 2017 +0200
@@ -191,7 +191,7 @@
 
     @property
     def video_type(self):
-        return b'video/flash'
+        return 'video/flash'
 
 
 @adapter_config(name='video_type', context=(IVideo, Interface, Interface), provides=ITALESExtension)
@@ -202,8 +202,14 @@
         if context is None:
             context = self.context
         registry = self.request.registry
-        adapter = registry.queryAdapter(context, IVideoType, name=context.content_type.decode())
+        content_type = context.content_type
+        if isinstance(content_type, bytes):
+            content_type = content_type.decode()
+        adapter = registry.queryAdapter(context, IVideoType, name=content_type)
         if adapter is None:
             adapter = registry.queryAdapter(context, IVideoType)
         if adapter is not None:
-            return adapter.video_type.decode()
+            video_type = adapter.video_type
+            if isinstance(video_type, bytes):
+                video_type = video_type.decode()
+            return video_type