better x-accel-redirect support

This commit is contained in:
j 2010-09-15 18:38:05 +02:00
parent 309f889670
commit 7acf61684e

View file

@ -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