Ticket #5444: image_sniffing_#5444.patch

File image_sniffing_#5444.patch, 2.2 KB (added by Ben Sturmfels, 4 years ago)

Quick hack to allow upload of image files without file extensions. Doesn't fully address this ticket.

  • mediagoblin/media_types/image/processing.py

    From 2e642ec7439aab1848e21813ba76d978867edc66 Mon Sep 17 00:00:00 2001
    From: Ben Sturmfels <ben@sturm.com.au>
    Date: Fri, 1 Nov 2019 17:58:33 +1100
    Subject: [PATCH] Identify image format using Imaging library, rather than by
     file extension.
    
    ---
     mediagoblin/media_types/image/processing.py | 24 ++++++++++-----------
     1 file changed, 11 insertions(+), 13 deletions(-)
    
    diff --git a/mediagoblin/media_types/image/processing.py b/mediagoblin/media_types/image/processing.py
    index 7ddf3f35..d2397348 100644
    a b def resize_image(entry, resized, keyname, target_name, new_size,  
    7777
    7878    # Copy the new file to the conversion subdir, then remotely.
    7979    tmp_resized_filename = os.path.join(workdir, target_name)
    80     resized.save(tmp_resized_filename, quality=quality)
     80    # TODO: Currently does not add a file extension if one wasn't provided in
     81    # the upload, eg. myfile.medium. The "format" parameter is required below
     82    # for this reason. Should probably add a default extension.
     83    resized.save(tmp_resized_filename, format=resized.format, quality=quality)
    8184    store_public(entry, keyname, tmp_resized_filename, target_name)
    8285
    8386    # store the thumb/medium info
    def _skip_resizing(entry, keyname, size, quality, filter):  
    150153    return skip
    151154
    152155
    153 SUPPORTED_FILETYPES = ['png', 'gif', 'jpg', 'jpeg', 'tiff']
    154 
    155 
    156156def sniff_handler(media_file, filename):
    157157    _log.info('Sniffing {0}'.format(MEDIA_TYPE))
    158     name, ext = os.path.splitext(filename)
    159     clean_ext = ext[1:].lower()  # Strip the . from ext and make lowercase
    160158
    161     if clean_ext in SUPPORTED_FILETYPES:
    162         _log.info('Found file extension in supported filetypes')
    163         return MEDIA_TYPE
     159    try:
     160        Image.open(media_file)
     161    except:
     162        _log.debug('Media present, could not be opened by imaging library')
     163        return None
    164164    else:
    165         _log.debug('Media present, extension not found in {0}'.format(
    166                 SUPPORTED_FILETYPES))
    167 
    168     return None
     165        _log.info('Media was identified by imaging library')
     166        return MEDIA_TYPE
    169167
    170168
    171169class CommonImageProcessor(MediaProcessor):