-from zlib import compress, decompress
+from __future__ import unicode_literals
+import os
from django.core.files.base import ContentFile, File
from django.core.files.storage import FileSystemStorage
+try:
+ from gzip import compress, decompress
+except ImportError:
+ # Python < 3.2
+ from gzip import GzipFile
+ from StringIO import StringIO
+
+ def compress(data):
+ compressed = StringIO()
+ GzipFile(fileobj=compressed, mode="wb").write(data)
+ return compressed.getvalue()
+
+ def decompress(data):
+ return GzipFile(fileobj=StringIO(data)).read()
+
class GzipFileSystemStorage(FileSystemStorage):
def _open(self, name, mode='rb'):
def _save(self, name, content):
content = ContentFile(compress(content.read()))
-
return super(GzipFileSystemStorage, self)._save(name, content)
+
+ def get_available_name(self, name):
+ if self.exists(name):
+ self.delete(name)
+ return name
\ No newline at end of file