2010-02-16 17:59:05 +05:30
|
|
|
# -*- 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]
|
2010-09-13 18:23:53 +02:00
|
|
|
if hasattr(settings, 'XACCELREDIRECT') and 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
|
|
|
|
elif settings.XSENDFILE:
|
2010-02-16 17:59:05 +05:30
|
|
|
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
|
|
|
|
|