#18655: Static and media files should be served using storages
-------------------------------------+--------------------
Reporter: mitar | Owner: nobody
Type: Uncategorized | Status: new
Component: contrib.staticfiles | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+--------------------
Currently static and media files using `django.views.static.serve`
directly access given path for serving files, for example
`settings.MEDIA_ROOT`. Much better would be that it would use storage API,
so it could be used to serve in development also files from other storage
classes, if Django is defined as such.
One such `serve` function I wrote:
{{{
def serve(request, path):
"""
Serve files from default storage.
"""
if not settings.DEBUG:
raise exceptions.ImproperlyConfigured("The view can only be used
in debug mode.")
normalized_path = posixpath.normpath(urllib.unquote(path)).lstrip('/')
if not storage.default_storage.exists(normalized_path):
if path.endswith('/') or path == '':
raise http.Http404("Directory indexes are not allowed here.")
raise http.Http404("'%s' could not be found" % path)
try:
mimetype = storage.default_storage.mimetype(normalized_path) or
'application/octet-stream'
except (NotImplementedError, AttributeError):
mimetype = 'application/octet-stream'
try:
modified_time =
time.mktime(storage.default_storage.modified_time(normalized_path).timetuple())
except (NotImplementedError, AttributeError):
modified_time = None
size = storage.default_storage.size(normalized_path)
if modified_time is not None and not
static.was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
modified_time, size):
return http.HttpResponseNotModified(mimetype=mimetype)
f = storage.default_storage.open(normalized_path, 'rb')
try:
response = http.HttpResponse(f.read(), mimetype=mimetype)
finally:
f.close()
response['Content-Length'] = size
if modified_time is not None:
response['Last-Modified'] = http_utils.http_date(modified_time)
return response
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/18655>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit https://groups.google.com/groups/opt_out.