From 301da9cabad61c8968a6e70df673dc26f1fea102 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Wed, 13 Nov 2013 19:39:18 -0600 Subject: [PATCH] Generic'ifying the submit code to not rely on the werkzeug FileStorage datastructure Important, because that only makes sense for wsgi! :) This commit sponsored by Geoff Lehr. Thank you! --- mediagoblin/media_types/__init__.py | 12 +++++------ mediagoblin/media_types/ascii/processing.py | 12 +++++------ mediagoblin/media_types/audio/processing.py | 2 +- mediagoblin/media_types/image/processing.py | 22 +++++++++------------ mediagoblin/media_types/pdf/processing.py | 12 +++++------ mediagoblin/media_types/stl/processing.py | 21 +++++++++----------- mediagoblin/media_types/video/processing.py | 5 ++--- mediagoblin/submit/lib.py | 4 ++-- 8 files changed, 40 insertions(+), 50 deletions(-) diff --git a/mediagoblin/media_types/__init__.py b/mediagoblin/media_types/__init__.py index 134157dc..80ae633b 100644 --- a/mediagoblin/media_types/__init__.py +++ b/mediagoblin/media_types/__init__.py @@ -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)) diff --git a/mediagoblin/media_types/ascii/processing.py b/mediagoblin/media_types/ascii/processing.py index f8983550..84030362 100644 --- a/mediagoblin/media_types/ascii/processing.py +++ b/mediagoblin/media_types/ascii/processing.py @@ -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 diff --git a/mediagoblin/media_types/audio/processing.py b/mediagoblin/media_types/audio/processing.py index 883d3ffc..f12f231e 100644 --- a/mediagoblin/media_types/audio/processing.py +++ b/mediagoblin/media_types/audio/processing.py @@ -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() diff --git a/mediagoblin/media_types/image/processing.py b/mediagoblin/media_types/image/processing.py index a0ad2ce8..1db82ee7 100644 --- a/mediagoblin/media_types/image/processing.py +++ b/mediagoblin/media_types/image/processing.py @@ -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 diff --git a/mediagoblin/media_types/pdf/processing.py b/mediagoblin/media_types/pdf/processing.py index b60495eb..6fb0b782 100644 --- a/mediagoblin/media_types/pdf/processing.py +++ b/mediagoblin/media_types/pdf/processing.py @@ -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 diff --git a/mediagoblin/media_types/stl/processing.py b/mediagoblin/media_types/stl/processing.py index 39949b59..65a86234 100644 --- a/mediagoblin/media_types/stl/processing.py +++ b/mediagoblin/media_types/stl/processing.py @@ -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 diff --git a/mediagoblin/media_types/video/processing.py b/mediagoblin/media_types/video/processing.py index 5ef9b854..eb5a062c 100644 --- a/mediagoblin/media_types/video/processing.py +++ b/mediagoblin/media_types/video/processing.py @@ -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: diff --git a/mediagoblin/submit/lib.py b/mediagoblin/submit/lib.py index 1df05978..848dd35c 100644 --- a/mediagoblin/submit/lib.py +++ b/mediagoblin/submit/lib.py @@ -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( -- 2.25.1