2011-08-12 08:29:34 +08:00
|
|
|
from __future__ import with_statement
|
2011-08-11 22:07:39 +08:00
|
|
|
import hashlib
|
2010-10-20 09:33:24 +08:00
|
|
|
import os
|
2011-08-11 22:07:39 +08:00
|
|
|
import posixpath
|
|
|
|
import re
|
2011-11-03 18:51:02 +08:00
|
|
|
from urllib import unquote
|
|
|
|
from urlparse import urlsplit, urlunsplit
|
2011-08-11 22:07:39 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.conf import settings
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.core.cache import (get_cache, InvalidCacheBackendError,
|
|
|
|
cache as default_cache)
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
from django.core.files.storage import FileSystemStorage, get_storage_class
|
2011-12-28 06:49:24 +08:00
|
|
|
from django.utils.encoding import force_unicode, smart_str
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.utils.functional import LazyObject
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.utils.importlib import import_module
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.utils.datastructures import SortedDict
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.contrib.staticfiles.utils import check_settings, matches_patterns
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class StaticFilesStorage(FileSystemStorage):
|
|
|
|
"""
|
2011-01-05 20:41:40 +08:00
|
|
|
Standard file system storage for static files.
|
2011-02-01 22:57:10 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
The defaults for ``location`` and ``base_url`` are
|
2010-11-17 23:36:26 +08:00
|
|
|
``STATIC_ROOT`` and ``STATIC_URL``.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, location=None, base_url=None, *args, **kwargs):
|
|
|
|
if location is None:
|
2010-11-17 23:36:26 +08:00
|
|
|
location = settings.STATIC_ROOT
|
2010-10-20 09:33:24 +08:00
|
|
|
if base_url is None:
|
2010-11-17 23:36:26 +08:00
|
|
|
base_url = settings.STATIC_URL
|
2011-08-15 02:15:04 +08:00
|
|
|
check_settings(base_url)
|
2011-08-11 22:07:39 +08:00
|
|
|
super(StaticFilesStorage, self).__init__(location, base_url,
|
|
|
|
*args, **kwargs)
|
|
|
|
|
2011-08-15 02:15:04 +08:00
|
|
|
def path(self, name):
|
|
|
|
if not self.location:
|
|
|
|
raise ImproperlyConfigured("You're using the staticfiles app "
|
|
|
|
"without having set the STATIC_ROOT "
|
|
|
|
"setting to a filesystem path.")
|
|
|
|
return super(StaticFilesStorage, self).path(name)
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
|
|
|
|
class CachedFilesMixin(object):
|
|
|
|
patterns = (
|
|
|
|
("*.css", (
|
|
|
|
r"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""",
|
|
|
|
r"""(@import\s*["']\s*(.*?)["'])""",
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(CachedFilesMixin, self).__init__(*args, **kwargs)
|
|
|
|
try:
|
|
|
|
self.cache = get_cache('staticfiles')
|
|
|
|
except InvalidCacheBackendError:
|
|
|
|
# Use the default backend
|
|
|
|
self.cache = default_cache
|
|
|
|
self._patterns = SortedDict()
|
|
|
|
for extension, patterns in self.patterns:
|
|
|
|
for pattern in patterns:
|
|
|
|
compiled = re.compile(pattern)
|
|
|
|
self._patterns.setdefault(extension, []).append(compiled)
|
|
|
|
|
|
|
|
def hashed_name(self, name, content=None):
|
2011-11-03 18:51:02 +08:00
|
|
|
parsed_name = urlsplit(unquote(name))
|
|
|
|
clean_name = parsed_name.path
|
2011-08-11 22:07:39 +08:00
|
|
|
if content is None:
|
2011-11-03 18:51:02 +08:00
|
|
|
if not self.exists(clean_name):
|
2011-08-11 22:07:39 +08:00
|
|
|
raise ValueError("The file '%s' could not be found with %r." %
|
2011-11-03 18:51:02 +08:00
|
|
|
(clean_name, self))
|
2011-08-11 22:07:39 +08:00
|
|
|
try:
|
2011-11-03 18:51:02 +08:00
|
|
|
content = self.open(clean_name)
|
2011-08-11 22:07:39 +08:00
|
|
|
except IOError:
|
|
|
|
# Handle directory paths
|
|
|
|
return name
|
2011-11-03 18:51:02 +08:00
|
|
|
path, filename = os.path.split(clean_name)
|
2011-08-11 22:07:39 +08:00
|
|
|
root, ext = os.path.splitext(filename)
|
|
|
|
# Get the MD5 hash of the file
|
|
|
|
md5 = hashlib.md5()
|
|
|
|
for chunk in content.chunks():
|
|
|
|
md5.update(chunk)
|
|
|
|
md5sum = md5.hexdigest()[:12]
|
2011-12-28 06:49:24 +08:00
|
|
|
hashed_name = os.path.join(path, u"%s.%s%s" %
|
|
|
|
(root, md5sum, ext))
|
2011-11-03 18:51:02 +08:00
|
|
|
unparsed_name = list(parsed_name)
|
|
|
|
unparsed_name[2] = hashed_name
|
2011-12-28 06:49:24 +08:00
|
|
|
# Special casing for a @font-face hack, like url(myfont.eot?#iefix")
|
|
|
|
# http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
|
|
|
|
if '?#' in name and not unparsed_name[3]:
|
|
|
|
unparsed_name[2] += '?'
|
2011-11-03 18:51:02 +08:00
|
|
|
return urlunsplit(unparsed_name)
|
2011-08-11 22:07:39 +08:00
|
|
|
|
|
|
|
def cache_key(self, name):
|
|
|
|
return u'staticfiles:cache:%s' % name
|
|
|
|
|
|
|
|
def url(self, name, force=False):
|
|
|
|
"""
|
|
|
|
Returns the real URL in DEBUG mode.
|
|
|
|
"""
|
|
|
|
if settings.DEBUG and not force:
|
2011-11-03 01:45:32 +08:00
|
|
|
hashed_name = name
|
|
|
|
else:
|
|
|
|
cache_key = self.cache_key(name)
|
|
|
|
hashed_name = self.cache.get(cache_key)
|
|
|
|
if hashed_name is None:
|
2011-12-26 03:44:20 +08:00
|
|
|
hashed_name = self.hashed_name(name).replace('\\', '/')
|
2011-12-28 06:49:24 +08:00
|
|
|
# set the cache if there was a miss
|
|
|
|
# (e.g. if cache server goes down)
|
2011-11-03 01:45:32 +08:00
|
|
|
self.cache.set(cache_key, hashed_name)
|
2011-11-03 18:51:02 +08:00
|
|
|
return unquote(super(CachedFilesMixin, self).url(hashed_name))
|
2011-08-11 22:07:39 +08:00
|
|
|
|
|
|
|
def url_converter(self, name):
|
|
|
|
"""
|
|
|
|
Returns the custom URL converter for the given file name.
|
|
|
|
"""
|
|
|
|
def converter(matchobj):
|
|
|
|
"""
|
|
|
|
Converts the matched URL depending on the parent level (`..`)
|
|
|
|
and returns the normalized and hashed URL using the url method
|
|
|
|
of the storage.
|
|
|
|
"""
|
|
|
|
matched, url = matchobj.groups()
|
|
|
|
# Completely ignore http(s) prefixed URLs
|
2011-12-28 06:49:24 +08:00
|
|
|
if url.startswith(('#', 'http', 'https', 'data:')):
|
2011-08-11 22:07:39 +08:00
|
|
|
return matched
|
2011-12-26 03:44:20 +08:00
|
|
|
name_parts = name.split(os.sep)
|
2011-08-11 22:07:39 +08:00
|
|
|
# Using posix normpath here to remove duplicates
|
2011-09-21 23:58:21 +08:00
|
|
|
url = posixpath.normpath(url)
|
|
|
|
url_parts = url.split('/')
|
|
|
|
parent_level, sub_level = url.count('..'), url.count('/')
|
|
|
|
if url.startswith('/'):
|
|
|
|
sub_level -= 1
|
|
|
|
url_parts = url_parts[1:]
|
|
|
|
if parent_level or not url.startswith('/'):
|
|
|
|
start, end = parent_level + 1, parent_level
|
|
|
|
else:
|
|
|
|
if sub_level:
|
|
|
|
if sub_level == 1:
|
|
|
|
parent_level -= 1
|
|
|
|
start, end = parent_level, sub_level - 1
|
|
|
|
else:
|
|
|
|
start, end = 1, sub_level - 1
|
|
|
|
joined_result = '/'.join(name_parts[:-start] + url_parts[end:])
|
2011-11-03 18:51:02 +08:00
|
|
|
hashed_url = self.url(unquote(joined_result), force=True)
|
2011-08-11 22:07:39 +08:00
|
|
|
# Return the hashed and normalized version to the file
|
2011-11-03 18:51:02 +08:00
|
|
|
return 'url("%s")' % unquote(hashed_url)
|
2011-08-11 22:07:39 +08:00
|
|
|
return converter
|
|
|
|
|
|
|
|
def post_process(self, paths, dry_run=False, **options):
|
|
|
|
"""
|
|
|
|
Post process the given list of files (called from collectstatic).
|
|
|
|
"""
|
|
|
|
processed_files = []
|
|
|
|
# don't even dare to process the files if we're in dry run mode
|
|
|
|
if dry_run:
|
|
|
|
return processed_files
|
|
|
|
|
|
|
|
# delete cache of all handled paths
|
|
|
|
self.cache.delete_many([self.cache_key(path) for path in paths])
|
|
|
|
|
|
|
|
# only try processing the files we have patterns for
|
|
|
|
matches = lambda path: matches_patterns(path, self._patterns.keys())
|
|
|
|
processing_paths = [path for path in paths if matches(path)]
|
|
|
|
|
|
|
|
# then sort the files by the directory level
|
|
|
|
path_level = lambda name: len(name.split(os.sep))
|
|
|
|
for name in sorted(paths, key=path_level, reverse=True):
|
|
|
|
|
|
|
|
# first get a hashed name for the given file
|
|
|
|
hashed_name = self.hashed_name(name)
|
|
|
|
|
|
|
|
with self.open(name) as original_file:
|
|
|
|
# then get the original's file content
|
|
|
|
content = original_file.read()
|
|
|
|
|
|
|
|
# to apply each replacement pattern on the content
|
|
|
|
if name in processing_paths:
|
|
|
|
converter = self.url_converter(name)
|
|
|
|
for patterns in self._patterns.values():
|
|
|
|
for pattern in patterns:
|
|
|
|
content = pattern.sub(converter, content)
|
|
|
|
|
|
|
|
# then save the processed result
|
|
|
|
if self.exists(hashed_name):
|
|
|
|
self.delete(hashed_name)
|
|
|
|
|
2011-12-28 06:49:24 +08:00
|
|
|
content_file = ContentFile(smart_str(content))
|
|
|
|
saved_name = self._save(hashed_name, content_file)
|
2011-08-11 22:07:39 +08:00
|
|
|
hashed_name = force_unicode(saved_name.replace('\\', '/'))
|
|
|
|
processed_files.append(hashed_name)
|
|
|
|
|
|
|
|
# and then set the cache accordingly
|
|
|
|
self.cache.set(self.cache_key(name), hashed_name)
|
|
|
|
|
|
|
|
return processed_files
|
|
|
|
|
|
|
|
|
|
|
|
class CachedStaticFilesStorage(CachedFilesMixin, StaticFilesStorage):
|
|
|
|
"""
|
|
|
|
A static file system storage backend which also saves
|
|
|
|
hashed copies of the files it saves.
|
|
|
|
"""
|
|
|
|
pass
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
class AppStaticStorage(FileSystemStorage):
|
|
|
|
"""
|
|
|
|
A file system storage backend that takes an app module and works
|
|
|
|
for the ``static`` directory of it.
|
|
|
|
"""
|
2011-01-16 07:38:00 +08:00
|
|
|
prefix = None
|
2010-10-20 09:33:24 +08:00
|
|
|
source_dir = 'static'
|
|
|
|
|
|
|
|
def __init__(self, app, *args, **kwargs):
|
|
|
|
"""
|
|
|
|
Returns a static file storage if available in the given app.
|
|
|
|
"""
|
2011-01-16 07:38:00 +08:00
|
|
|
# app is the actual app module
|
2011-06-30 17:06:19 +08:00
|
|
|
mod = import_module(app)
|
2011-01-16 07:38:00 +08:00
|
|
|
mod_path = os.path.dirname(mod.__file__)
|
|
|
|
location = os.path.join(mod_path, self.source_dir)
|
|
|
|
super(AppStaticStorage, self).__init__(location, *args, **kwargs)
|
2011-08-11 22:07:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
class ConfiguredStorage(LazyObject):
|
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
|
|
|
|
|
|
|
|
staticfiles_storage = ConfiguredStorage()
|