diff --git a/3rd_party_apps/sendfile/__init__.py b/3rd_party_apps/sendfile/__init__.py deleted file mode 100644 index eea11a74d8..0000000000 --- a/3rd_party_apps/sendfile/__init__.py +++ /dev/null @@ -1,59 +0,0 @@ -VERSION = (0, 1, 1) -__version__ = '.'.join(map(str, VERSION)) - -import os.path -from mimetypes import guess_type - -from django.http import Http404 - -def _lazy_load(fn): - _cached = [] - def _decorated(): - if not _cached: - _cached.append(fn()) - return _cached[0] - return _decorated - - -@_lazy_load -def _get_sendfile(): - from django.utils.importlib import import_module - from django.conf import settings - from django.core.exceptions import ImproperlyConfigured - - backend = getattr(settings, 'SENDFILE_BACKEND', None) - if not backend: - raise ImproperlyConfigured('You must specify a valued for SENDFILE_BACKEND') - module = import_module(backend) - return module.sendfile - - - -def sendfile(request, filename, attachment=False, attachment_filename=None): - ''' - create a response to send file using backend configured in SENDFILE_BACKEND - - if attachment is True the content-disposition header will be set with either - the filename given or else the attachment_filename (of specified). This - will typically prompt the user to download the file, rather than view it. - ''' - _sendfile = _get_sendfile() - - if not os.path.exists(filename): - raise Http404('"%s" does not exist' % filename) - - mimetype, encoding = guess_type(filename) - if mimetype is None: - mimetype = 'application/octet-stream' - - response = _sendfile(request, filename, mimetype=mimetype) - if attachment: - attachment_filename = attachment_filename or os.path.basename(filename) - response['Content-Disposition'] = 'attachment; filename=%s' % attachment_filename - - response['Content-length'] = os.path.getsize(filename) - response['Content-Type'] = mimetype - if encoding: - response['Content-Encoding'] = encoding - - return response diff --git a/3rd_party_apps/sendfile/backends/__init__.py b/3rd_party_apps/sendfile/backends/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/3rd_party_apps/sendfile/backends/development.py b/3rd_party_apps/sendfile/backends/development.py deleted file mode 100644 index 08a7dd0a42..0000000000 --- a/3rd_party_apps/sendfile/backends/development.py +++ /dev/null @@ -1,15 +0,0 @@ -from django.views.static import serve - -import os.path - -def sendfile(request, filename, **kwargs): - ''' - 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) diff --git a/3rd_party_apps/sendfile/backends/mod_wsgi.py b/3rd_party_apps/sendfile/backends/mod_wsgi.py deleted file mode 100644 index b8bdfc58d1..0000000000 --- a/3rd_party_apps/sendfile/backends/mod_wsgi.py +++ /dev/null @@ -1,30 +0,0 @@ -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, **kwargs): - 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 - diff --git a/3rd_party_apps/sendfile/backends/simple.py b/3rd_party_apps/sendfile/backends/simple.py deleted file mode 100644 index e74eeeb8b0..0000000000 --- a/3rd_party_apps/sendfile/backends/simple.py +++ /dev/null @@ -1,56 +0,0 @@ -import os -import stat -import re -from email.Utils import parsedate_tz, mktime_tz - -from django.core.files.base import File -from django.http import HttpResponse, HttpResponseNotModified -from django.utils.http import http_date - -def sendfile(request, filename, **kwargs): - # Respect the If-Modified-Since header. - statobj = os.stat(filename) - mimetype = kwargs.get('mimetype', 'application/octet-stream') - - if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), - statobj[stat.ST_MTIME], statobj[stat.ST_SIZE]): - return HttpResponseNotModified(mimetype=mimetype) - - - response = HttpResponse(File(file(filename, 'rb'))) - - response["Last-Modified"] = http_date(statobj[stat.ST_MTIME]) - return response - -def was_modified_since(header=None, mtime=0, size=0): - """ - Was something modified since the user last downloaded it? - - header - This is the value of the If-Modified-Since header. If this is None, - I'll just return True. - - mtime - This is the modification time of the item we're talking about. - - size - This is the size of the item we're talking about. - """ - try: - if header is None: - raise ValueError - matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header, - re.IGNORECASE) - header_date = parsedate_tz(matches.group(1)) - if header_date is None: - raise ValueError - header_mtime = mktime_tz(header_date) - header_len = matches.group(3) - if header_len and int(header_len) != size: - raise ValueError - if mtime > header_mtime: - raise ValueError - except (AttributeError, ValueError, OverflowError): - return True - return False - diff --git a/3rd_party_apps/sendfile/backends/xsendfile.py b/3rd_party_apps/sendfile/backends/xsendfile.py deleted file mode 100644 index b0f2720f60..0000000000 --- a/3rd_party_apps/sendfile/backends/xsendfile.py +++ /dev/null @@ -1,8 +0,0 @@ -from django.http import HttpResponse - -def sendfile(request, filename, **kwargs): - response = HttpResponse() - response['X-Sendfile'] = filename - - return response - diff --git a/requirements/development.txt b/requirements/development.txt index 92c0733dcf..fe228d3604 100644 --- a/requirements/development.txt +++ b/requirements/development.txt @@ -18,3 +18,4 @@ django-grappelli==2.3.3 Pillow==1.7.4 cssmin==0.1.4 django-compressor==1.1 +-e git://github.com/rosarior/django-sendfile.git#egg=django-sendfile diff --git a/requirements/production.txt b/requirements/production.txt index e04e099d79..39948f87ba 100644 --- a/requirements/production.txt +++ b/requirements/production.txt @@ -15,3 +15,4 @@ django-grappelli==2.3.3 Pillow==1.7.4 cssmin==0.1.4 django-compressor==1.1 +-e git://github.com/rosarior/django-sendfile.git#egg=django-sendfile