1 # -*- coding: utf-8 -*-
 
   3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
 
   4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   6 from __future__ import unicode_literals
 
   8 from django.core.files.base import ContentFile
 
   9 from django.core.files.storage import FileSystemStorage
 
  12     from gzip import compress, decompress
 
  15     from gzip import GzipFile
 
  16     from StringIO import StringIO
 
  19         compressed = StringIO()
 
  20         GzipFile(fileobj=compressed, mode="wb").write(data)
 
  21         return compressed.getvalue()
 
  24         return GzipFile(fileobj=StringIO(data)).read()
 
  27 class GzipFileSystemStorage(FileSystemStorage):
 
  28     def _open(self, name, mode='rb'):
 
  29         """TODO: This is good for reading; what about writing?"""
 
  30         f = open(self.path(name), 'rb')
 
  33         return ContentFile(decompress(text))
 
  35     def _save(self, name, content):
 
  36         content = ContentFile(compress(content.read()))
 
  37         return super(GzipFileSystemStorage, self)._save(name, content)
 
  39     def get_available_name(self, name):