PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/staticfiles/storage.py

https://github.com/insane/django
Python | 312 lines | 241 code | 24 blank | 47 comment | 39 complexity | 0667508bb3cd2f28f9cee68ceaa63742 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from __future__ import unicode_literals
  2. import hashlib
  3. import os
  4. import posixpath
  5. import re
  6. try:
  7. from urllib.parse import unquote, urlsplit, urlunsplit, urldefrag
  8. except ImportError: # Python 2
  9. from urllib import unquote
  10. from urlparse import urlsplit, urlunsplit, urldefrag
  11. from django.conf import settings
  12. from django.core.cache import (get_cache, InvalidCacheBackendError,
  13. cache as default_cache)
  14. from django.core.exceptions import ImproperlyConfigured
  15. from django.core.files.base import ContentFile
  16. from django.core.files.storage import FileSystemStorage, get_storage_class
  17. from django.utils.datastructures import SortedDict
  18. from django.utils.encoding import force_bytes, force_text
  19. from django.utils.functional import LazyObject
  20. from django.utils.importlib import import_module
  21. from django.utils._os import upath
  22. from django.contrib.staticfiles.utils import check_settings, matches_patterns
  23. class StaticFilesStorage(FileSystemStorage):
  24. """
  25. Standard file system storage for static files.
  26. The defaults for ``location`` and ``base_url`` are
  27. ``STATIC_ROOT`` and ``STATIC_URL``.
  28. """
  29. def __init__(self, location=None, base_url=None, *args, **kwargs):
  30. if location is None:
  31. location = settings.STATIC_ROOT
  32. if base_url is None:
  33. base_url = settings.STATIC_URL
  34. check_settings(base_url)
  35. super(StaticFilesStorage, self).__init__(location, base_url,
  36. *args, **kwargs)
  37. def path(self, name):
  38. if not self.location:
  39. raise ImproperlyConfigured("You're using the staticfiles app "
  40. "without having set the STATIC_ROOT "
  41. "setting to a filesystem path.")
  42. return super(StaticFilesStorage, self).path(name)
  43. class CachedFilesMixin(object):
  44. default_template = """url("%s")"""
  45. patterns = (
  46. ("*.css", (
  47. r"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""",
  48. (r"""(@import\s*["']\s*(.*?)["'])""", """@import url("%s")"""),
  49. )),
  50. )
  51. def __init__(self, *args, **kwargs):
  52. super(CachedFilesMixin, self).__init__(*args, **kwargs)
  53. try:
  54. self.cache = get_cache('staticfiles')
  55. except InvalidCacheBackendError:
  56. # Use the default backend
  57. self.cache = default_cache
  58. self._patterns = SortedDict()
  59. for extension, patterns in self.patterns:
  60. for pattern in patterns:
  61. if isinstance(pattern, (tuple, list)):
  62. pattern, template = pattern
  63. else:
  64. template = self.default_template
  65. compiled = re.compile(pattern, re.IGNORECASE)
  66. self._patterns.setdefault(extension, []).append((compiled, template))
  67. def file_hash(self, name, content=None):
  68. """
  69. Retuns a hash of the file with the given name and optional content.
  70. """
  71. if content is None:
  72. return None
  73. md5 = hashlib.md5()
  74. for chunk in content.chunks():
  75. md5.update(chunk)
  76. return md5.hexdigest()[:12]
  77. def hashed_name(self, name, content=None):
  78. parsed_name = urlsplit(unquote(name))
  79. clean_name = parsed_name.path.strip()
  80. opened = False
  81. if content is None:
  82. if not self.exists(clean_name):
  83. raise ValueError("The file '%s' could not be found with %r." %
  84. (clean_name, self))
  85. try:
  86. content = self.open(clean_name)
  87. except IOError:
  88. # Handle directory paths and fragments
  89. return name
  90. opened = True
  91. try:
  92. file_hash = self.file_hash(clean_name, content)
  93. finally:
  94. if opened:
  95. content.close()
  96. path, filename = os.path.split(clean_name)
  97. root, ext = os.path.splitext(filename)
  98. if file_hash is not None:
  99. file_hash = ".%s" % file_hash
  100. hashed_name = os.path.join(path, "%s%s%s" %
  101. (root, file_hash, ext))
  102. unparsed_name = list(parsed_name)
  103. unparsed_name[2] = hashed_name
  104. # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
  105. # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
  106. if '?#' in name and not unparsed_name[3]:
  107. unparsed_name[2] += '?'
  108. return urlunsplit(unparsed_name)
  109. def cache_key(self, name):
  110. return 'staticfiles:%s' % hashlib.md5(force_bytes(name)).hexdigest()
  111. def url(self, name, force=False):
  112. """
  113. Returns the real URL in DEBUG mode.
  114. """
  115. if settings.DEBUG and not force:
  116. hashed_name, fragment = name, ''
  117. else:
  118. clean_name, fragment = urldefrag(name)
  119. if urlsplit(clean_name).path.endswith('/'): # don't hash paths
  120. hashed_name = name
  121. else:
  122. cache_key = self.cache_key(name)
  123. hashed_name = self.cache.get(cache_key)
  124. if hashed_name is None:
  125. hashed_name = self.hashed_name(clean_name).replace('\\', '/')
  126. # set the cache if there was a miss
  127. # (e.g. if cache server goes down)
  128. self.cache.set(cache_key, hashed_name)
  129. final_url = super(CachedFilesMixin, self).url(hashed_name)
  130. # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
  131. # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
  132. query_fragment = '?#' in name # [sic!]
  133. if fragment or query_fragment:
  134. urlparts = list(urlsplit(final_url))
  135. if fragment and not urlparts[4]:
  136. urlparts[4] = fragment
  137. if query_fragment and not urlparts[3]:
  138. urlparts[2] += '?'
  139. final_url = urlunsplit(urlparts)
  140. return unquote(final_url)
  141. def url_converter(self, name, template=None):
  142. """
  143. Returns the custom URL converter for the given file name.
  144. """
  145. if template is None:
  146. template = self.default_template
  147. def converter(matchobj):
  148. """
  149. Converts the matched URL depending on the parent level (`..`)
  150. and returns the normalized and hashed URL using the url method
  151. of the storage.
  152. """
  153. matched, url = matchobj.groups()
  154. # Completely ignore http(s) prefixed URLs,
  155. # fragments and data-uri URLs
  156. if url.startswith(('#', 'http:', 'https:', 'data:', '//')):
  157. return matched
  158. name_parts = name.split(os.sep)
  159. # Using posix normpath here to remove duplicates
  160. url = posixpath.normpath(url)
  161. url_parts = url.split('/')
  162. parent_level, sub_level = url.count('..'), url.count('/')
  163. if url.startswith('/'):
  164. sub_level -= 1
  165. url_parts = url_parts[1:]
  166. if parent_level or not url.startswith('/'):
  167. start, end = parent_level + 1, parent_level
  168. else:
  169. if sub_level:
  170. if sub_level == 1:
  171. parent_level -= 1
  172. start, end = parent_level, 1
  173. else:
  174. start, end = 1, sub_level - 1
  175. joined_result = '/'.join(name_parts[:-start] + url_parts[end:])
  176. hashed_url = self.url(unquote(joined_result), force=True)
  177. file_name = hashed_url.split('/')[-1:]
  178. relative_url = '/'.join(url.split('/')[:-1] + file_name)
  179. # Return the hashed version to the file
  180. return template % unquote(relative_url)
  181. return converter
  182. def post_process(self, paths, dry_run=False, **options):
  183. """
  184. Post process the given SortedDict of files (called from collectstatic).
  185. Processing is actually two separate operations:
  186. 1. renaming files to include a hash of their content for cache-busting,
  187. and copying those files to the target storage.
  188. 2. adjusting files which contain references to other files so they
  189. refer to the cache-busting filenames.
  190. If either of these are performed on a file, then that file is considered
  191. post-processed.
  192. """
  193. # don't even dare to process the files if we're in dry run mode
  194. if dry_run:
  195. return
  196. # where to store the new paths
  197. hashed_paths = {}
  198. # build a list of adjustable files
  199. matches = lambda path: matches_patterns(path, self._patterns.keys())
  200. adjustable_paths = [path for path in paths if matches(path)]
  201. # then sort the files by the directory level
  202. path_level = lambda name: len(name.split(os.sep))
  203. for name in sorted(paths.keys(), key=path_level, reverse=True):
  204. # use the original, local file, not the copied-but-unprocessed
  205. # file, which might be somewhere far away, like S3
  206. storage, path = paths[name]
  207. with storage.open(path) as original_file:
  208. # generate the hash with the original content, even for
  209. # adjustable files.
  210. hashed_name = self.hashed_name(name, original_file)
  211. # then get the original's file content..
  212. if hasattr(original_file, 'seek'):
  213. original_file.seek(0)
  214. hashed_file_exists = self.exists(hashed_name)
  215. processed = False
  216. # ..to apply each replacement pattern to the content
  217. if name in adjustable_paths:
  218. content = original_file.read().decode(settings.FILE_CHARSET)
  219. for patterns in self._patterns.values():
  220. for pattern, template in patterns:
  221. converter = self.url_converter(name, template)
  222. try:
  223. content = pattern.sub(converter, content)
  224. except ValueError as exc:
  225. yield name, None, exc
  226. if hashed_file_exists:
  227. self.delete(hashed_name)
  228. # then save the processed result
  229. content_file = ContentFile(force_bytes(content))
  230. saved_name = self._save(hashed_name, content_file)
  231. hashed_name = force_text(saved_name.replace('\\', '/'))
  232. processed = True
  233. else:
  234. # or handle the case in which neither processing nor
  235. # a change to the original file happened
  236. if not hashed_file_exists:
  237. processed = True
  238. saved_name = self._save(hashed_name, original_file)
  239. hashed_name = force_text(saved_name.replace('\\', '/'))
  240. # and then set the cache accordingly
  241. hashed_paths[self.cache_key(name.replace('\\', '/'))] = hashed_name
  242. yield name, hashed_name, processed
  243. # Finally set the cache
  244. self.cache.set_many(hashed_paths)
  245. class CachedStaticFilesStorage(CachedFilesMixin, StaticFilesStorage):
  246. """
  247. A static file system storage backend which also saves
  248. hashed copies of the files it saves.
  249. """
  250. pass
  251. class AppStaticStorage(FileSystemStorage):
  252. """
  253. A file system storage backend that takes an app module and works
  254. for the ``static`` directory of it.
  255. """
  256. prefix = None
  257. source_dir = 'static'
  258. def __init__(self, app, *args, **kwargs):
  259. """
  260. Returns a static file storage if available in the given app.
  261. """
  262. # app is the actual app module
  263. mod = import_module(app)
  264. mod_path = os.path.dirname(upath(mod.__file__))
  265. location = os.path.join(mod_path, self.source_dir)
  266. super(AppStaticStorage, self).__init__(location, *args, **kwargs)
  267. class ConfiguredStorage(LazyObject):
  268. def _setup(self):
  269. self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
  270. staticfiles_storage = ConfiguredStorage()