Skip to content

Commit

Permalink
Merge pull request #990 from karlcow/710/3
Browse files Browse the repository at this point in the history
Fixes #710 - Add optimization for Image formats
  • Loading branch information
Mike Taylor committed Apr 1, 2016
2 parents 98e88a1 + 3240856 commit 44e64bc
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions webcompat/api/uploads.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def to_image_object(self, imagedata):
# Chop off 'data:image/.+;base64,' before decoding
imagedata = re.sub('^data:image/.+;base64,', '', imagedata)
return Image.open(BytesIO(base64.b64decode(imagedata)))
raise IOError('Not a valid image format')
except IOError as e:
raise TypeError('TypeError: Not a valid image format')
except TypeError:
# Not a valid format
abort(415)

Expand All @@ -72,6 +72,7 @@ def get_url(self):

def save(self):
'''Check that the file is allowed, then save to filesystem.'''
save_parameters = {}
if self.file_ext not in self.ALLOWED_FORMATS:
raise TypeError('Image file format not allowed')

Expand All @@ -82,7 +83,15 @@ def save(self):
dest_dir = os.path.dirname(file_dest)
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
self.image_object.save(file_dest)
# Optimize further the image compression for these formats
if self.image_object.format in ['JPEG', 'JPG', 'JPE', 'PNG']:
save_parameters['optimize'] = True
# If animated GIF, aka duration > 0, add save_all parameter
if (self.image_object.format == 'GIF' and
self.image_object.info['duration'] > 0):
save_parameters['save_all'] = True
# unpacking save_parameters
self.image_object.save(file_dest, **save_parameters)


@uploads.route('/', methods=['POST'])
Expand Down

0 comments on commit 44e64bc

Please sign in to comment.