Implement sendfile.
parent
c7f09d3fef
commit
f0a8689712
@ -1,17 +0,0 @@
|
|||||||
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)
|
|
@ -1,17 +0,0 @@
|
|||||||
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
|
|
@ -1,12 +0,0 @@
|
|||||||
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
|
|
@ -1,60 +0,0 @@
|
|||||||
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
|
|
@ -1,9 +0,0 @@
|
|||||||
from django.http import HttpResponse
|
|
||||||
|
|
||||||
|
|
||||||
def sendfile(request, filename, **kwargs):
|
|
||||||
filename = str(filename)
|
|
||||||
response = HttpResponse()
|
|
||||||
response['X-Sendfile'] = filename
|
|
||||||
|
|
||||||
return response
|
|
@ -0,0 +1,9 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from fastapi import Response
|
||||||
|
|
||||||
|
from ..utils import _convert_file_to_url
|
||||||
|
|
||||||
|
|
||||||
|
def sendfile(filename, **kwargs):
|
||||||
|
return Response(headers={"Location": _convert_file_to_url(filename)})
|
@ -0,0 +1,9 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
from fastapi import Response
|
||||||
|
|
||||||
|
from ..utils import _convert_file_to_url
|
||||||
|
|
||||||
|
|
||||||
|
def sendfile(filename, **kwargs):
|
||||||
|
return Response(headers={"X-Accel-Redirect": _convert_file_to_url(filename)})
|
@ -0,0 +1,12 @@
|
|||||||
|
from fastapi.responses import FileResponse
|
||||||
|
|
||||||
|
|
||||||
|
def sendfile(filename, mimetype, **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.
|
||||||
|
"""
|
||||||
|
|
||||||
|
return FileResponse(filename, media_type=mimetype)
|
@ -0,0 +1,6 @@
|
|||||||
|
from fastapi import Response
|
||||||
|
|
||||||
|
|
||||||
|
def sendfile(filename, **kwargs):
|
||||||
|
filename = str(filename)
|
||||||
|
return Response(headers={"X-Sendfile": filename})
|
Loading…
Reference in New Issue