- Author:
- dcwatson
- Posted:
- September 29, 2009
- Language:
- Python
- Version:
- 1.1
- Score:
- 2 (after 2 ratings)
GzipFileSystemStorage
is a FileSystemStorage
subclass that transparently compresses files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | from django.conf import settings
from django.core.files import File
from django.core.files.storage import FileSystemStorage
import gzip
import os
class DjangoGzipFile (File):
def chunks( self, chunk_size=None ):
if hasattr( self, 'seek' ):
self.seek( 0 )
yield self.read()
def multiple_chunks( self, chunk_size=None ):
return False
class GzipFileSystemStorage (FileSystemStorage):
nocompress = ('.z','.gz','.zip','.tgz','.jpg','.png','.gif')
def should_compress( self, name ):
dir_name, file_name = os.path.split( name )
file_root, file_ext = os.path.splitext( file_name )
return file_ext.lower() not in self.nocompress
def path( self, name, for_dir=False ):
p = super( GzipFileSystemStorage, self ).path( name )
if self.should_compress(name) and not for_dir:
p = p + '.gz'
return p
def _open( self, name, mode='rb' ):
if self.should_compress( name ):
full_path = self.path( name )
gzip_file = gzip.GzipFile( full_path, mode )
# File._get_size chokes unless self.file has a name attribute.
setattr( gzip_file, 'name', full_path )
return DjangoGzipFile( gzip_file )
else:
return super( GzipFileSystemStorage, self )._open( name, mode )
def _save( self, name, content ):
if not self.should_compress( name ):
return super( GzipFileSystemStorage, self )._save( name, content )
full_path = self.path( self.get_available_name(name) )
dir_name, file_name = os.path.split( full_path )
if not os.path.exists( dir_name ):
os.makedirs( dir_name )
elif not os.path.isdir( dir_name ):
raise IOError( "%s exists and is not a directory." % dir_name )
fd = open( full_path, 'wb' )
gzip_file = gzip.GzipFile( filename=file_name, fileobj=fd, mode='wb' )
for chunk in content.chunks():
gzip_file.write( chunk )
gzip_file.close()
fd.close()
if settings.FILE_UPLOAD_PERMISSIONS is not None:
os.chmod( full_path, settings.FILE_UPLOAD_PERMISSIONS )
return name
def listdir( self, path ):
path = self.path( path, for_dir=True )
directories, files = [], []
for entry in os.listdir( path ):
if os.path.isdir( os.path.join(path,entry) ):
directories.append( entry )
else:
files.append( entry )
return directories, files
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.