Sendfile: add a sendfile module based on django-sendfile2

This commit is contained in:
Tom Hacohen
2020-12-14 16:00:48 +02:00
parent 070abfcdd8
commit a19a982b1c
10 changed files with 232 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
import os.path
from django.views.static import serve
def sendfile(request, filename, **kwargs):
"""
Send file using Django dev static file server.
.. warning::
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,17 @@
from __future__ import absolute_import
from django.http import HttpResponse
from ..utils import _convert_file_to_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: ''
request.build_absolute_uri = lambda location: location
return response

View File

@@ -0,0 +1,12 @@
from __future__ import absolute_import
from django.http import HttpResponse
from ..utils import _convert_file_to_url
def sendfile(request, filename, **kwargs):
response = HttpResponse()
response['X-Accel-Redirect'] = _convert_file_to_url(filename)
return response

View File

@@ -0,0 +1,60 @@
from email.utils import mktime_tz, parsedate_tz
import re
from django.core.files.base import File
from django.http import HttpResponse, HttpResponseNotModified
from django.utils.http import http_date
def sendfile(request, filepath, **kwargs):
'''Use the SENDFILE_ROOT value composed with the path arrived as argument
to build an absolute path with which resolve and return the file contents.
If the path points to a file out of the root directory (should cover both
situations with '..' and symlinks) then a 404 is raised.
'''
statobj = filepath.stat()
# Respect the If-Modified-Since header.
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj.st_mtime, statobj.st_size):
return HttpResponseNotModified()
with File(filepath.open('rb')) as f:
response = HttpResponse(f.chunks())
response["Last-Modified"] = http_date(statobj.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

View File

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