From 7f34b221b113a96a89cfbed1461881ab614c6dea Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 16 Feb 2010 17:59:05 +0530 Subject: [PATCH] add HttpFileResponse --- oxdjango/http.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 oxdjango/http.py diff --git a/oxdjango/http.py b/oxdjango/http.py new file mode 100644 index 0000000..2b7c0e7 --- /dev/null +++ b/oxdjango/http.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 +import os +import mimetypes + +from django.http import HttpResponse, Http404 +from django.conf import settings + + +def HttpFileResponse(path, content_type=None, filename=None): + if not os.path.exists(path): + raise Http404 + if not content_type: + content_type = mimetypes.guess_type(path)[0] + if settings.XSENDFILE: + response = HttpResponse() + response['X-Sendfile'] = path + response['Content-Type'] = content_type + response['Content-Length'] = os.stat(path).st_size + else: + response = HttpResponse(open(path), content_type=content_type) + if filename: + response['Content-Disposition'] = 'attachment; filename="%s"' % filename + return response +