From 7acf61684e17cefce83d3e4550cfd3d16ec66878 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 15 Sep 2010 18:38:05 +0200 Subject: [PATCH] better x-accel-redirect support --- oxdjango/http.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/oxdjango/http.py b/oxdjango/http.py index aadea00..ddf149d 100644 --- a/oxdjango/http.py +++ b/oxdjango/http.py @@ -12,11 +12,17 @@ def HttpFileResponse(path, content_type=None, filename=None): raise Http404 if not content_type: content_type = mimetypes.guess_type(path)[0] - if hasattr(settings, 'XACCELREDIRECT') and settings.XACCELREDIRECT: + if settings.XACCELREDIRECT: response = HttpResponse() - response['X-Accel-Redirect'] = path.replace(*settings.XACCELREDIRECT) - response['Content-Type'] = content_type response['Content-Length'] = os.stat(path).st_size + if not isinstance(settings.XACCELREDIRECT, bool): + path = path.replace(*settings.XACCELREDIRECT) + if path.startswith(settings.STATIC_ROOT): + path = settings.STATIC_URL + path[len(settings.STATIC_ROOT)+1:] + if path.startswith(settings.MEDIA_ROOT): + path = settings.MEDIA_URL + path[len(settings.MEDIA_ROOT)+1:] + response['X-Accel-Redirect'] = path + response['Content-Type'] = content_type elif settings.XSENDFILE: response = HttpResponse() response['X-Sendfile'] = path