2007-10-14 12:10:02 +08:00
|
|
|
"""
|
2007-11-30 13:14:09 +08:00
|
|
|
Views and functions for serving static files. These are only to be used
|
2007-10-14 12:10:02 +08:00
|
|
|
during development, and SHOULD NOT be used in a production setting.
|
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
import mimetypes
|
2005-09-26 02:45:01 +08:00
|
|
|
import posixpath
|
2006-03-03 12:53:04 +08:00
|
|
|
import re
|
2017-07-22 04:33:26 +08:00
|
|
|
from pathlib import Path
|
2005-09-26 02:45:01 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.http import (
|
|
|
|
FileResponse, Http404, HttpResponse, HttpResponseNotModified,
|
|
|
|
)
|
2015-02-23 02:14:05 +08:00
|
|
|
from django.template import Context, Engine, TemplateDoesNotExist, loader
|
2017-03-15 00:33:15 +08:00
|
|
|
from django.utils._os import safe_join
|
2011-03-01 22:28:06 +08:00
|
|
|
from django.utils.http import http_date, parse_http_date
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext as _, gettext_lazy
|
2007-10-14 12:10:02 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-02-14 09:42:26 +08:00
|
|
|
def serve(request, path, document_root=None, show_indexes=False):
|
2005-09-26 02:45:01 +08:00
|
|
|
"""
|
|
|
|
Serve static files below a given point in the directory structure.
|
2005-11-07 05:54:37 +08:00
|
|
|
|
|
|
|
To use, put a URL pattern such as::
|
|
|
|
|
2014-08-12 22:54:42 +08:00
|
|
|
from django.views.static import serve
|
|
|
|
|
2019-05-14 00:30:51 +08:00
|
|
|
path('<path:path>', serve, {'document_root': '/path/to/my/files/'})
|
2005-11-07 05:54:37 +08:00
|
|
|
|
|
|
|
in your URLconf. You must provide the ``document_root`` param. You may
|
2005-09-26 02:45:01 +08:00
|
|
|
also set ``show_indexes`` to ``True`` if you'd like to serve a basic index
|
|
|
|
of the directory. This index view will use the template hardcoded below,
|
|
|
|
but if you'd like to override it, you can create a template called
|
2008-12-05 03:33:28 +08:00
|
|
|
``static/directory_index.html``.
|
2005-09-26 02:45:01 +08:00
|
|
|
"""
|
2017-03-15 00:33:15 +08:00
|
|
|
path = posixpath.normpath(path).lstrip('/')
|
2017-07-22 04:33:26 +08:00
|
|
|
fullpath = Path(safe_join(document_root, path))
|
|
|
|
if fullpath.is_dir():
|
2011-02-14 09:42:26 +08:00
|
|
|
if show_indexes:
|
2017-03-15 00:33:15 +08:00
|
|
|
return directory_index(path, fullpath)
|
2012-06-08 00:08:47 +08:00
|
|
|
raise Http404(_("Directory indexes are not allowed here."))
|
2017-07-22 04:33:26 +08:00
|
|
|
if not fullpath.exists():
|
2019-06-28 00:39:47 +08:00
|
|
|
raise Http404(_('“%(path)s” does not exist') % {'path': fullpath})
|
2011-02-14 09:42:26 +08:00
|
|
|
# Respect the If-Modified-Since header.
|
2017-07-22 04:33:26 +08:00
|
|
|
statobj = fullpath.stat()
|
2011-02-14 09:42:26 +08:00
|
|
|
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
|
2011-03-02 06:49:10 +08:00
|
|
|
statobj.st_mtime, statobj.st_size):
|
2012-08-23 00:06:03 +08:00
|
|
|
return HttpResponseNotModified()
|
2017-07-22 04:33:26 +08:00
|
|
|
content_type, encoding = mimetypes.guess_type(str(fullpath))
|
2013-01-24 18:01:12 +08:00
|
|
|
content_type = content_type or 'application/octet-stream'
|
2017-07-22 04:33:26 +08:00
|
|
|
response = FileResponse(fullpath.open('rb'), content_type=content_type)
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers["Last-Modified"] = http_date(statobj.st_mtime)
|
2011-02-14 09:42:26 +08:00
|
|
|
if encoding:
|
2020-07-14 19:32:24 +08:00
|
|
|
response.headers["Content-Encoding"] = encoding
|
2011-02-14 09:42:26 +08:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
|
2012-02-05 05:01:11 +08:00
|
|
|
{% load i18n %}
|
2011-04-21 01:40:53 +08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
2011-02-14 09:42:26 +08:00
|
|
|
<head>
|
2018-01-21 15:09:10 +08:00
|
|
|
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
|
|
|
<meta http-equiv="Content-Language" content="en-us">
|
|
|
|
<meta name="robots" content="NONE,NOARCHIVE">
|
2019-09-23 23:31:21 +08:00
|
|
|
<title>{% blocktranslate %}Index of {{ directory }}{% endblocktranslate %}</title>
|
2011-02-14 09:42:26 +08:00
|
|
|
</head>
|
|
|
|
<body>
|
2019-09-23 23:31:21 +08:00
|
|
|
<h1>{% blocktranslate %}Index of {{ directory }}{% endblocktranslate %}</h1>
|
2011-02-14 09:42:26 +08:00
|
|
|
<ul>
|
2015-08-07 19:18:55 +08:00
|
|
|
{% if directory != "/" %}
|
2011-02-14 09:42:26 +08:00
|
|
|
<li><a href="../">../</a></li>
|
2015-08-07 19:18:55 +08:00
|
|
|
{% endif %}
|
2011-02-14 09:42:26 +08:00
|
|
|
{% for f in file_list %}
|
|
|
|
<li><a href="{{ f|urlencode }}">{{ f }}</a></li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
2017-01-27 03:58:33 +08:00
|
|
|
template_translatable = gettext_lazy("Index of %(directory)s")
|
2011-02-14 09:42:26 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-02-14 09:42:26 +08:00
|
|
|
def directory_index(path, fullpath):
|
|
|
|
try:
|
2015-02-23 02:14:05 +08:00
|
|
|
t = loader.select_template([
|
|
|
|
'static/directory_index.html',
|
|
|
|
'static/directory_index',
|
|
|
|
])
|
2011-02-14 09:42:26 +08:00
|
|
|
except TemplateDoesNotExist:
|
2016-08-10 23:27:03 +08:00
|
|
|
t = Engine(libraries={'i18n': 'django.templatetags.i18n'}).from_string(DEFAULT_DIRECTORY_INDEX_TEMPLATE)
|
2017-04-25 23:01:21 +08:00
|
|
|
c = Context()
|
|
|
|
else:
|
|
|
|
c = {}
|
2011-02-14 09:42:26 +08:00
|
|
|
files = []
|
2017-07-22 04:33:26 +08:00
|
|
|
for f in fullpath.iterdir():
|
|
|
|
if not f.name.startswith('.'):
|
|
|
|
url = str(f.relative_to(fullpath))
|
|
|
|
if f.is_dir():
|
|
|
|
url += '/'
|
|
|
|
files.append(url)
|
2017-04-25 23:01:21 +08:00
|
|
|
c.update({
|
2013-10-27 09:27:42 +08:00
|
|
|
'directory': path + '/',
|
|
|
|
'file_list': files,
|
2011-02-14 09:42:26 +08:00
|
|
|
})
|
|
|
|
return HttpResponse(t.render(c))
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2011-02-14 09:42:26 +08:00
|
|
|
def was_modified_since(header=None, mtime=0, size=0):
|
|
|
|
"""
|
|
|
|
Was something modified since the user last downloaded it?
|
|
|
|
|
|
|
|
header
|
|
|
|
This is the value of the If-Modified-Since header. If this is None,
|
|
|
|
I'll just return True.
|
|
|
|
|
|
|
|
mtime
|
|
|
|
This is the modification time of the item we're talking about.
|
|
|
|
|
|
|
|
size
|
|
|
|
This is the size of the item we're talking about.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
if header is None:
|
|
|
|
raise ValueError
|
|
|
|
matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header,
|
|
|
|
re.IGNORECASE)
|
2020-05-11 04:03:39 +08:00
|
|
|
header_mtime = parse_http_date(matches[1])
|
|
|
|
header_len = matches[3]
|
2011-02-14 09:42:26 +08:00
|
|
|
if header_len and int(header_len) != size:
|
|
|
|
raise ValueError
|
2012-09-27 03:12:56 +08:00
|
|
|
if int(mtime) > header_mtime:
|
2011-02-14 09:42:26 +08:00
|
|
|
raise ValueError
|
|
|
|
except (AttributeError, ValueError, OverflowError):
|
|
|
|
return True
|
|
|
|
return False
|