Generic'ifying the submit code to not rely on the werkzeug FileStorage datastructure
authorChristopher Allan Webber <cwebber@dustycloud.org>
Thu, 14 Nov 2013 01:39:18 +0000 (19:39 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Thu, 14 Nov 2013 01:40:13 +0000 (19:40 -0600)
Important, because that only makes sense for wsgi! :)

This commit sponsored by Geoff Lehr.  Thank you!

mediagoblin/media_types/__init__.py
mediagoblin/media_types/ascii/processing.py
mediagoblin/media_types/audio/processing.py
mediagoblin/media_types/image/processing.py
mediagoblin/media_types/pdf/processing.py
mediagoblin/media_types/stl/processing.py
mediagoblin/media_types/video/processing.py
mediagoblin/submit/lib.py

index 134157dcb0ca7054fdf5043bef82548137ef7373..80ae633ba47c0f2d630afca4ac2bf61995b75c35 100644 (file)
@@ -50,23 +50,23 @@ class MediaManagerBase(object):
         return hasattr(self, i)
 
 
-def sniff_media(media):
+def sniff_media(media_file, filename):
     '''
     Iterate through the enabled media types and find those suited
     for a certain file.
     '''
 
     try:
-        return get_media_type_and_manager(media.filename)
+        return get_media_type_and_manager(filename)
     except FileTypeNotSupported:
         _log.info('No media handler found by file extension. Doing it the expensive way...')
         # Create a temporary file for sniffers suchs as GStreamer-based
         # Audio video
-        media_file = tempfile.NamedTemporaryFile()
-        media_file.write(media.stream.read())
-        media.stream.seek(0)
+        tmp_media_file = tempfile.NamedTemporaryFile()
+        tmp_media_file.write(media_file.read())
+        tmp_media_file.seek(0)
 
-        media_type = hook_handle('sniff_handler', media_file, media=media)
+        media_type = hook_handle('sniff_handler', tmp_media_file, filename)
         if media_type:
             _log.info('{0} accepts the file'.format(media_type))
             return media_type, hook_handle(('media_manager', media_type))
index f898355054d5f6b23a564ed0f365f9615b3248f4..84030362fda9e786244098e356440c3d0009256f 100644 (file)
@@ -36,14 +36,14 @@ SUPPORTED_EXTENSIONS = ['txt', 'asc', 'nfo']
 MEDIA_TYPE = 'mediagoblin.media_types.ascii'
 
 
-def sniff_handler(media_file, **kw):
+def sniff_handler(media_file, filename):
     _log.info('Sniffing {0}'.format(MEDIA_TYPE))
-    if kw.get('media') is not None:
-        name, ext = os.path.splitext(kw['media'].filename)
-        clean_ext = ext[1:].lower()
 
-        if clean_ext in SUPPORTED_EXTENSIONS:
-            return MEDIA_TYPE
+    name, ext = os.path.splitext(filename)
+    clean_ext = ext[1:].lower()
+
+    if clean_ext in SUPPORTED_EXTENSIONS:
+        return MEDIA_TYPE
 
     return None
 
index 883d3ffc2d65916bb90be3488f44c891a2d85e27..f12f231e9d7ef8f20d0f28b03c3f87a95688fa0d 100644 (file)
@@ -33,7 +33,7 @@ _log = logging.getLogger(__name__)
 MEDIA_TYPE = 'mediagoblin.media_types.audio'
 
 
-def sniff_handler(media_file, **kw):
+def sniff_handler(media_file, filename):
     _log.info('Sniffing {0}'.format(MEDIA_TYPE))
     try:
         transcoder = AudioTranscoder()
index a0ad2ce81d36e0781d9885f2940d825b332adf79..1db82ee79667024a88c8c09d88a26aa86efe99f0 100644 (file)
@@ -149,21 +149,17 @@ def _skip_resizing(entry, keyname, size, quality, filter):
 SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg', 'tiff']
 
 
-def sniff_handler(media_file, **kw):
+def sniff_handler(media_file, filename):
     _log.info('Sniffing {0}'.format(MEDIA_TYPE))
-    if kw.get('media') is not None:  # That's a double negative!
-        name, ext = os.path.splitext(kw['media'].filename)
-        clean_ext = ext[1:].lower()  # Strip the . from ext and make lowercase
-
-        if clean_ext in SUPPORTED_FILETYPES:
-            _log.info('Found file extension in supported filetypes')
-            return MEDIA_TYPE
-        else:
-            _log.debug('Media present, extension not found in {0}'.format(
-                    SUPPORTED_FILETYPES))
+    name, ext = os.path.splitext(filename)
+    clean_ext = ext[1:].lower()  # Strip the . from ext and make lowercase
+
+    if clean_ext in SUPPORTED_FILETYPES:
+        _log.info('Found file extension in supported filetypes')
+        return MEDIA_TYPE
     else:
-        _log.warning('Need additional information (keyword argument \'media\')'
-                     ' to be able to handle sniffing')
+        _log.debug('Media present, extension not found in {0}'.format(
+                SUPPORTED_FILETYPES))
 
     return None
 
index b60495ebb9d692932a5625d2babe46482ea5ada5..6fb0b78295a6385d269198c047c4dd951538f9b6 100644 (file)
@@ -168,18 +168,16 @@ def check_prerequisites():
         return False
     return True
 
-def sniff_handler(media_file, **kw):
+def sniff_handler(media_file, filename):
     _log.info('Sniffing {0}'.format(MEDIA_TYPE))
     if not check_prerequisites():
         return None
-    if kw.get('media') is not None:
-        name, ext = os.path.splitext(kw['media'].filename)
-        clean_ext = ext[1:].lower()
 
-        if clean_ext in supported_extensions():
-            return MEDIA_TYPE
+    name, ext = os.path.splitext(filename)
+    clean_ext = ext[1:].lower()
 
-    return None
+    if clean_ext in supported_extensions():
+        return MEDIA_TYPE
 
 def create_pdf_thumb(original, thumb_filename, width, height):
     # Note: pdftocairo adds '.png', remove it
index 39949b596d8c305d063b6f560ca6795399135566..65a86234c1f399774504c493b67bd530262d8506 100644 (file)
@@ -47,21 +47,18 @@ BLEND_SCRIPT = pkg_resources.resource_filename(
         'blender_render.py'))
 
 
-def sniff_handler(media_file, **kw):
+def sniff_handler(media_file, filename):
     _log.info('Sniffing {0}'.format(MEDIA_TYPE))
-    if kw.get('media') is not None:
-        name, ext = os.path.splitext(kw['media'].filename)
-        clean_ext = ext[1:].lower()
 
-        if clean_ext in SUPPORTED_FILETYPES:
-            _log.info('Found file extension in supported filetypes')
-            return MEDIA_TYPE
-        else:
-            _log.debug('Media present, extension not found in {0}'.format(
-                    SUPPORTED_FILETYPES))
+    name, ext = os.path.splitext(filename)
+    clean_ext = ext[1:].lower()
+
+    if clean_ext in SUPPORTED_FILETYPES:
+        _log.info('Found file extension in supported filetypes')
+        return MEDIA_TYPE
     else:
-        _log.warning('Need additional information (keyword argument \'media\')'
-                     ' to be able to handle sniffing')
+        _log.debug('Media present, extension not found in {0}'.format(
+                SUPPORTED_FILETYPES))
 
     return None
 
index 5ef9b854a0870b2bf932f374c5245ce6d3e24b92..eb5a062cf729d28e130be3f22e04011079a28a6a 100644 (file)
@@ -44,7 +44,7 @@ class VideoTranscodingFail(BaseProcessingFail):
     general_message = _(u'Video transcoding failed')
 
 
-def sniff_handler(media_file, **kw):
+def sniff_handler(media_file, filename):
     transcoder = transcoders.VideoTranscoder()
     data = transcoder.discover(media_file.name)
 
@@ -52,8 +52,7 @@ def sniff_handler(media_file, **kw):
     _log.debug('Discovered: {0}'.format(data))
 
     if not data:
-        _log.error('Could not discover {0}'.format(
-            kw.get('media')))
+        _log.error('Could not discover {0}'.format(filename))
         return None
 
     if data['is_video'] is True:
index 1df05978358780b4a2087616705ae51a75948685..848dd35cb19b469109d03c868c62e7983d7c1c9a 100644 (file)
@@ -115,7 +115,7 @@ def submit_media(mg_app, user, submitted_file, filename,
 
     # Sniff the submitted media to determine which
     # media plugin should handle processing
-    media_type, media_manager = sniff_media(submitted_file)
+    media_type, media_manager = sniff_media(submitted_file, filename)
 
     # create entry and save in database
     entry = new_upload_entry(user)
@@ -135,7 +135,7 @@ def submit_media(mg_app, user, submitted_file, filename,
     queue_file = prepare_queue_task(mg_app, entry, filename)
 
     with queue_file:
-        queue_file.write(submitted_file.stream.read())
+        queue_file.write(submitted_file.read())
 
     # Get file size and round to 2 decimal places
     file_size = mg_app.queue_store.get_file_size(