X-Git-Url: https://git.mdrn.pl/redakcja.git/blobdiff_plain/ce8d791a5298e0cb2569034aec4c8b57afac97b2..6d4d8644ae79d264dda4e9d3cb0322962a182463:/apps/dvcs/storage.py?ds=sidebyside diff --git a/apps/dvcs/storage.py b/apps/dvcs/storage.py index 6bb5b595..045b780a 100755 --- a/apps/dvcs/storage.py +++ b/apps/dvcs/storage.py @@ -1,8 +1,28 @@ -from zlib import compress, decompress +# -*- coding: utf-8 -*- +# +# This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. +# +from __future__ import unicode_literals -from django.core.files.base import ContentFile, File +from django.core.files.base import ContentFile 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'): @@ -14,5 +34,9 @@ class GzipFileSystemStorage(FileSystemStorage): def _save(self, name, content): content = ContentFile(compress(content.read())) - return super(GzipFileSystemStorage, self)._save(name, content) + + def get_available_name(self, name, max_length=None): + if self.exists(name): + self.delete(name) + return name