﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18655	Media files should be served using file storage API	Mitar	nobody	"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  
}}}"	New feature	new	File uploads/storage	1.4	Normal			mmitar@… jason@…	Accepted	0	0	0	0	0	0
