Commits

David Larlet committed f47f169

Add ImageStorage and OverwriteStorage from djangosnippets

  • Participants
  • Parent commits 6978cfd

Comments (0)

Files changed (2)

File ImageStorage.py

+
+import os
+from PIL import ImageFile as PILImageFile
+from django.core.files.storage import FileSystemStorage
+
+
+class ImageStorage(FileSystemStorage):
+    """
+    A FileSystemStorage which normalizes extensions for images.
+    
+    Comes from http://www.djangosnippets.org/snippets/965/
+    """
+    
+    def find_extension(self, format):
+        """Normalizes PIL-returned format into a standard, lowercase extension."""
+        format = format.lower()
+        
+        if format == 'jpeg':
+            format = 'jpg'
+        
+        return format
+    
+    def save(self, name, content):
+        dirname = os.path.dirname(name)
+        basename = os.path.basename(name)
+        
+        # Use PIL to determine filetype
+        
+        p = PILImageFile.Parser()
+        while 1:
+            data = content.read(1024)
+            if not data:
+                break
+            p.feed(data)
+            if p.image:
+                im = p.image
+                break
+        
+        extension = self.find_extension(im.format)
+        
+        # Does the basename already have an extension? If so, replace it.
+        # bare as in without extension
+        bare_basename = basename if '.' not in basename else basename[:basename.rindex('.')]
+        basename = bare_basename + '.' + extension
+        
+        name = os.path.join(dirname, basename)
+        return super(ImageStorage, self).save(name, content)
+    

File OverwriteStorage.py

+import os
+
+from django.conf import settings
+from django.core.files.storage import FileSystemStorage
+
+class OverwriteStorage(FileSystemStorage):
+    
+    def get_available_name(self, name):
+        """
+        Returns a filename that's free on the target storage system, and
+        available for new content to be written to.
+        
+        Comes from http://www.djangosnippets.org/snippets/976/
+        (even if it already exists in S3Storage for ages)
+        """
+        # If the filename already exists, remove it as if it was a true file system
+        if self.exists(name):
+            os.remove(os.path.join(settings.MEDIA_ROOT, name))
+        return name