Update the notice.
[redakcja.git] / src / dvcs / storage.py
1 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from zlib import compress, decompress
5
6 from django.core.files.base import ContentFile, File
7 from django.core.files.storage import FileSystemStorage
8 from django.utils.deconstruct import deconstructible
9 from django.utils.encoding import force_bytes
10
11
12 @deconstructible
13 class GzipFileSystemStorage(FileSystemStorage):
14     def _open(self, name, mode='rb'):
15         """TODO: This is good for reading; what about writing?"""
16         f = open(self.path(name), 'rb')
17         text = f.read()
18         f.close()
19         return ContentFile(decompress(text))
20
21     def _save(self, name, content):
22         data = force_bytes(content.read())
23         content = ContentFile(compress(data))
24
25         return super(GzipFileSystemStorage, self)._save(name, content)