import hashlib
from django.core.files.storage import FileSystemStorage
import os
def md5sum(file):
"""Calculate the md5 checksum of a file-like object without reading its
whole content in memory.
>>> from StringIO import StringIO
>>> md5sum(StringIO('file content to hash'))
'784406af91dd5a54fbb9c84c2236595a'
"""
m = hashlib.md5()
while 1:
d = file.read(8096)
if not d:
break
m.update(d)
return m.hexdigest()
class PoolStorage(FileSystemStorage):
"""
Saves file in file pool ;)
"""
def _save(self, name, content):
oname = name.split('/')
key = md5sum(content)
name = os.path.join(oname[0], key[0:2], oname[1])
return super(PoolStorage, self)._save(name, content)