Added 3rd party sendfile app

This commit is contained in:
Roberto Rosario
2011-03-24 16:40:12 -04:00
parent 8f82c82825
commit 278d8bcf10
7 changed files with 123 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
from django.http import HttpResponse
from django.core.files.uploadedfile import SimpleUploadedFile
def sendfile(request, filename):
return = HttpResponse(IterFile(filename))
class IterFile(object):
def __init__(self, filename):
self.file = SimpleUploadedFile(name=filename, content=open(filename).read())
def __iter__(self):
return self.file.chunks()

View File

@@ -0,0 +1,15 @@
from django.views.static import serve
import os.path
def sendfile(request, filename):
'''
Send file using django dev static file server.
DO NOT USE IN PRODUCTION
this is only to be used when developing and is provided
for convenience only
'''
dirname = os.path.dirname(filename)
basename = os.path.basename(filename)
return serve(request, basename, dirname)

View File

@@ -0,0 +1,30 @@
from django.http import HttpResponse
from django.conf import settings
import os.path
def _convert_file_to_url(filename):
# CURRENTLY NOT WORKING
# mod_wsgi wants a relative URL not a filename
# so apache does an internal redirect
relpath = os.path.relpath(filename, settings.SENDFILE_ROOT)
url = [settings.SENDFILE_URL]
while relpath:
relpath, head = os.path.split(relpath)
url.insert(1, head)
return u''.join(url)
def sendfile(request, filename):
response = HttpResponse()
response['Location'] = _convert_file_to_url(filename)
# need to destroy get_host() to stop django
# rewriting our location to include http, so that
# mod_wsgi is able to do the internal redirect
request.get_host = lambda: ''
return response

View File

@@ -0,0 +1,6 @@
from django.core.servers.basehttp import FileWrapper
from django.http import HttpResponse
def sendfile(request, filename):
wrapper = FileWrapper(file(filename))
return HttpResponse(wrapper)

View File

@@ -0,0 +1,8 @@
from django.http import HttpResponse
def sendfile(request, filename):
response = HttpResponse()
response['X-Sendfile'] = filename
return response