Opened 13 years ago
Last modified 12 years ago
#18655 new New feature
Media files should be served using file storage API
Reported by: | Mitar | Owned by: | nobody |
---|---|---|---|
Component: | File uploads/storage | Version: | 1.4 |
Severity: | Normal | Keywords: | |
Cc: | mmitar@…, jason@… | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Pull Requests: | How to create a pull request | ||
Description ¶
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
According to the ticket's flags, the next step(s) to move this issue forward are:
- To provide a patch by sending a pull request. Claim the ticket when you start working so that someone else doesn't duplicate effort. Before sending a pull request, review your work against the patch review checklist. Check the "Has patch" flag on the ticket after sending a pull request and include a link to the pull request in the ticket comment when making that update. The usual format is:
[https://github.com/django/django/pull/#### PR]
.
Change History (3)
comment:1 by , 12 years ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:2 by , 12 years ago
Type: | Uncategorized → New feature |
---|
comment:3 by , 12 years ago
Component: | contrib.staticfiles → File uploads/storage |
---|---|
Summary: | Static and media files should be served using storages → Media files should be served using file storage API |
Triage Stage: | Design decision needed → Accepted |
Note:
See TracTickets
for help on using tickets.
For static files:
django.contrib.staticfiles
is enabledFor media files:
I'm restricting the scope of this ticket to media files, please provide a patch so we can better evaluate your idea.