Fallback to simple sendfile backend

If SENDFILE_BACKEND is not set
This commit is contained in:
Karl Hobley 2015-04-23 12:12:16 +01:00
parent 6e0a448f11
commit b3348534cc
3 changed files with 14 additions and 5 deletions

View file

@ -158,7 +158,5 @@ try:
except ImportError:
pass
# Sendfile dev backend, do NOT use in production https://github.com/johnsensible/django-sendfile#django-sendfile
SENDFILE_BACKEND = 'sendfile.backends.development'
WAGTAIL_SITE_NAME = "Test Site"

View file

@ -1,3 +1,7 @@
# Copied from django-sendfile 0.3.6 and tweaked to allow a backend to be passed
# to sendfile()
# See: https://github.com/johnsensible/django-sendfile/pull/33
VERSION = (0, 3, 6)
__version__ = '.'.join(map(str, VERSION))
@ -32,7 +36,7 @@ def _get_sendfile():
def sendfile(request, filename, attachment=False, attachment_filename=None, mimetype=None, encoding=None):
def sendfile(request, filename, attachment=False, attachment_filename=None, mimetype=None, encoding=None, backend=None):
'''
create a response to send file using backend configured in SENDFILE_BACKEND
@ -48,7 +52,7 @@ def sendfile(request, filename, attachment=False, attachment_filename=None, mime
If no mimetype or encoding are specified, then they will be guessed via the
filename (using the standard python mimetypes module)
'''
_sendfile = _get_sendfile()
_sendfile = backend or _get_sendfile()
if not os.path.exists(filename):
from django.http import Http404

View file

@ -1,4 +1,5 @@
from django.shortcuts import get_object_or_404
from django.conf import settings
from wagtail.utils.sendfile import sendfile
@ -11,4 +12,10 @@ def serve(request, document_id, document_filename):
# Send document_served signal
document_served.send(sender=Document, instance=doc, request=request)
return sendfile(request, doc.file.path, attachment=True, attachment_filename=doc.filename)
if hasattr(settings, 'SENDFILE_BACKEND'):
return sendfile(request, doc.file.path, attachment=True, attachment_filename=doc.filename)
else:
# Fallback to simple backend if user hasn't specified SENDFILE_BACKEND (will crash by default)
from sendfile.backends.simple import sendfile as simple_sendfile_backend
return sendfile(request, doc.file.path, attachment=True, attachment_filename=doc.filename, backend=simple_sendfile_backend)