a57d759fc778bd6307235030d64cbcc851167ae3
[redakcja.git] / src / dvcs / storage.py
1 from zlib import compress, decompress
2
3 from django.core.files.base import ContentFile, File
4 from django.core.files.storage import FileSystemStorage
5 from django.utils.deconstruct import deconstructible
6 from django.utils.encoding import force_bytes
7
8
9 @deconstructible
10 class GzipFileSystemStorage(FileSystemStorage):
11     def _open(self, name, mode='rb'):
12         """TODO: This is good for reading; what about writing?"""
13         f = open(self.path(name), 'rb')
14         text = f.read()
15         f.close()
16         return ContentFile(decompress(text))
17
18     def _save(self, name, content):
19         data = force_bytes(content.read())
20         content = ContentFile(compress(data))
21
22         return super(GzipFileSystemStorage, self)._save(name, content)