2010-11-12 05:43:49 +08:00
|
|
|
from django.conf import settings
|
2013-09-07 22:25:51 +08:00
|
|
|
from django.core.handlers.wsgi import get_path_info, WSGIHandler
|
2013-09-06 03:38:59 +08:00
|
|
|
from django.utils.six.moves.urllib.parse import urlparse
|
|
|
|
from django.utils.six.moves.urllib.request import url2pathname
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
from django.contrib.staticfiles import utils
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.contrib.staticfiles.views import serve
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
class StaticFilesHandler(WSGIHandler):
|
|
|
|
"""
|
|
|
|
WSGI middleware that intercepts calls to the static files directory, as
|
2010-11-17 23:36:26 +08:00
|
|
|
defined by the STATIC_URL setting, and serves those files.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2013-12-02 03:12:48 +08:00
|
|
|
def __init__(self, application):
|
2010-10-20 09:33:24 +08:00
|
|
|
self.application = application
|
2010-11-17 23:36:26 +08:00
|
|
|
self.base_url = urlparse(self.get_base_url())
|
2010-11-12 05:43:49 +08:00
|
|
|
super(StaticFilesHandler, self).__init__()
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
def get_base_url(self):
|
2011-02-01 22:57:10 +08:00
|
|
|
utils.check_settings()
|
2010-11-17 23:36:26 +08:00
|
|
|
return settings.STATIC_URL
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
def _should_handle(self, path):
|
|
|
|
"""
|
|
|
|
Checks if the path should be handled. Ignores the path if:
|
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
* the host is provided as part of the base_url
|
2010-11-12 05:43:49 +08:00
|
|
|
* the request's path isn't under the media path (or equal)
|
|
|
|
"""
|
2011-07-05 00:19:54 +08:00
|
|
|
return path.startswith(self.base_url[2]) and not self.base_url[1]
|
2010-11-12 05:43:49 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
def file_path(self, url):
|
|
|
|
"""
|
|
|
|
Returns the relative path to the media file on disk for the given URL.
|
|
|
|
"""
|
2010-11-17 23:36:26 +08:00
|
|
|
relative_url = url[len(self.base_url[2]):]
|
2012-07-20 21:36:52 +08:00
|
|
|
return url2pathname(relative_url)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
def serve(self, request):
|
|
|
|
"""
|
|
|
|
Actually serves the request path.
|
|
|
|
"""
|
|
|
|
return serve(request, self.file_path(request.path), insecure=True)
|
|
|
|
|
|
|
|
def get_response(self, request):
|
|
|
|
from django.http import Http404
|
|
|
|
|
|
|
|
if self._should_handle(request.path):
|
|
|
|
try:
|
|
|
|
return self.serve(request)
|
2012-04-29 00:09:37 +08:00
|
|
|
except Http404 as e:
|
2010-11-12 05:43:49 +08:00
|
|
|
if settings.DEBUG:
|
|
|
|
from django.views import debug
|
|
|
|
return debug.technical_404_response(request, e)
|
|
|
|
return super(StaticFilesHandler, self).get_response(request)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
def __call__(self, environ, start_response):
|
2012-12-17 17:49:26 +08:00
|
|
|
if not self._should_handle(get_path_info(environ)):
|
2010-10-20 09:33:24 +08:00
|
|
|
return self.application(environ, start_response)
|
2010-11-12 05:43:49 +08:00
|
|
|
return super(StaticFilesHandler, self).__call__(environ, start_response)
|