Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize for alternate storage backends #329

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions easy_thumbnails/optimize/post_processor.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import logging
import os
import subprocess
from imghdr import what as determinetype
from django.core.files.base import ContentFile
Expand Down Expand Up @@ -37,12 +38,19 @@ def check_output(*popenargs, **kwargs):
def optimize_thumbnail(thumbnail):
'''Optimize thumbnail images by removing unnecessary data'''
try:
optimize_command = settings.THUMBNAIL_OPTIMIZE_COMMAND[
determinetype(thumbnail.path)]
file_type = determinetype(thumbnail.path)
except NotImplementedError:
# System is using an alternative storage backend
file_type = os.path.splitext(thumbnail.url)[1][1:]
if file_type == 'jpg':
file_type = 'jpeg'
except (TypeError, KeyError):
return
finally:
optimize_command = settings.THUMBNAIL_OPTIMIZE_COMMAND[file_type]
if not optimize_command:
return
except (TypeError, KeyError, NotImplementedError):
return

storage = thumbnail.storage
try:
with NamedTemporaryFile() as temp_file:
Expand All @@ -59,7 +67,12 @@ def optimize_thumbnail(thumbnail):
logger.info('{0} returned nothing'.format(optimize_command))
with open(temp_file.name, 'rb') as f:
thumbnail.file = ContentFile(f.read())
storage.delete(thumbnail.path)
storage.save(thumbnail.path, thumbnail)
try:
storage.delete(thumbnail.path)
storage.save(thumbnail.path, thumbnail)
except NotImplementedError:
# Alternative object-based storage backends use name
storage.delete(thumbnail.name)
storage.save(thumbnail.name, thumbnail)
except Exception as e:
logger.error(e)