PageRenderTime 29ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 17ms app.codeStats 0ms

/secdownload_storage/__init__.py

https://bitbucket.org/ionelmc/django-secdownload-storage
Python | 29 lines | 24 code | 5 blank | 0 comment | 4 complexity | f77fc5c6eeeee3218ddf9c8a0e72acd6 MD5 | raw file
 1import time
 2import hashlib
 3import urlparse
 4
 5from django.core.files.storage import FileSystemStorage, filepath_to_uri
 6from django.conf import settings
 7from django.core.exceptions import ImproperlyConfigured
 8
 9ENABLED = settings.SEC_DOWNLOAD_ENABLED
10
11class SecDownloadFileSystemStorage(FileSystemStorage):
12    def __init__(self, location=None, base_url=None, secret_key=None):
13        if ENABLED:
14            if base_url is None:
15                base_url = settings.SEC_DOWNLOAD_MEDIA_URL
16            if secret_key is None:
17                secret_key = settings.SEC_DOWNLOAD_SECRET_KEY
18
19            self.secret_key = secret_key
20        super(self.__class__, self).__init__(location=location, base_url=base_url)
21
22    def url(self, name):
23        if ENABLED:
24            file_path = filepath_to_uri(name)
25            hextime = "%08x" % time.time()
26            token = hashlib.md5(self.secret_key + '/' + file_path + hextime).hexdigest()
27            return urlparse.urljoin(self.base_url, '/'.join((token, hextime, file_path)))
28        else:
29            return super(self.__class__, self).url(name)