add oxdjango to ox package
This commit is contained in:
parent
0eeecabbd2
commit
0262ae50ca
11 changed files with 355 additions and 4 deletions
41
ox/django/utils.py
Normal file
41
ox/django/utils.py
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
from django.http import HttpResponse,Http404
|
||||
from django.core.servers.basehttp import FileWrapper
|
||||
from django.conf import settings
|
||||
|
||||
import mimetypes
|
||||
import os
|
||||
|
||||
def basic_sendfile(fname,download_name=None):
|
||||
if not os.path.exists(fname):
|
||||
raise Http404
|
||||
|
||||
wrapper = FileWrapper(open(fname,"r"))
|
||||
|
||||
content_type = mimetypes.guess_type(fname)[0]
|
||||
response = HttpResponse(wrapper, content_type=content_type)
|
||||
response['Content-Length'] = os.path.getsize(fname)
|
||||
|
||||
if download_name:
|
||||
response['Content-Disposition'] = "attachment; filename=%s"%download_name
|
||||
|
||||
return response
|
||||
|
||||
def x_sendfile(fname,download_name=None):
|
||||
if not os.path.exists(fname):
|
||||
raise Http404
|
||||
|
||||
content_type = mimetypes.guess_type(fname)[0]
|
||||
response = HttpResponse('', content_type=content_type)
|
||||
response['Content-Length'] = os.path.getsize(fname)
|
||||
response['X-Sendfile'] = fname
|
||||
|
||||
if download_name:
|
||||
response['Content-Disposition'] = "attachment; filename=%s"%download_name
|
||||
|
||||
return response
|
||||
|
||||
if getattr(settings,'SENDFILE',False) == 'x_sendfile':
|
||||
sendfile = x_sendfile
|
||||
else:
|
||||
sendfile = basic_sendfile
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue