2017-01-31 01:04:51 +00:00
|
|
|
from mimetypes import guess_type
|
2016-10-24 09:21:06 +01:00
|
|
|
from os import path
|
|
|
|
from re import sub
|
|
|
|
from time import strftime, gmtime
|
2016-12-10 11:16:37 +00:00
|
|
|
from urllib.parse import unquote
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
from aiofiles.os import stat
|
|
|
|
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.exceptions import (
|
|
|
|
ContentRangeError,
|
|
|
|
FileNotFound,
|
|
|
|
HeaderNotFound,
|
|
|
|
InvalidUsage,
|
|
|
|
)
|
|
|
|
from sanic.handlers import ContentRangeHandler
|
|
|
|
from sanic.response import file, HTTPResponse
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
def register(app, uri, file_or_directory, pattern,
|
|
|
|
use_modified_since, use_content_range):
|
|
|
|
# TODO: Though sanic is not a file server, I feel like we should at least
|
2016-10-24 09:21:06 +01:00
|
|
|
# make a good effort here. Modified-since is nice, but we could
|
|
|
|
# also look into etags, expires, and caching
|
|
|
|
"""
|
2017-02-14 19:10:19 +00:00
|
|
|
Register a static directory handler with Sanic by adding a route to the
|
2016-10-24 09:21:06 +01:00
|
|
|
router and registering a handler.
|
2016-12-25 09:43:45 +00:00
|
|
|
|
2016-10-24 09:21:06 +01:00
|
|
|
:param app: Sanic
|
|
|
|
:param file_or_directory: File or directory path to serve from
|
|
|
|
:param uri: URL to serve from
|
|
|
|
:param pattern: regular expression used to match files in the URL
|
|
|
|
:param use_modified_since: If true, send file modified time, and return
|
2016-12-25 09:43:45 +00:00
|
|
|
not modified if the browser's matches the
|
|
|
|
server's
|
2017-01-31 01:04:51 +00:00
|
|
|
:param use_content_range: If true, process header for range requests
|
|
|
|
and sends the file part that is requested
|
2016-10-24 09:21:06 +01:00
|
|
|
"""
|
|
|
|
# If we're not trying to match a file directly,
|
|
|
|
# serve from the folder
|
|
|
|
if not path.isfile(file_or_directory):
|
|
|
|
uri += '<file_uri:' + pattern + '>'
|
|
|
|
|
|
|
|
async def _handler(request, file_uri=None):
|
|
|
|
# Using this to determine if the URL is trying to break out of the path
|
|
|
|
# served. os.path.realpath seems to be very slow
|
|
|
|
if file_uri and '../' in file_uri:
|
|
|
|
raise InvalidUsage("Invalid URL")
|
|
|
|
# Merge served directory and requested file if provided
|
|
|
|
# Strip all / that in the beginning of the URL to help prevent python
|
|
|
|
# from herping a derp and treating the uri as an absolute path
|
2017-04-13 20:38:55 +01:00
|
|
|
root_path = file_path = file_or_directory
|
2016-12-25 02:11:12 +00:00
|
|
|
if file_uri:
|
|
|
|
file_path = path.join(
|
|
|
|
file_or_directory, sub('^[/]*', '', file_uri))
|
2016-12-10 11:16:37 +00:00
|
|
|
|
2016-12-13 09:41:39 +00:00
|
|
|
# URL decode the path sent by the browser otherwise we won't be able to
|
2016-12-13 00:10:24 +00:00
|
|
|
# match filenames which got encoded (filenames with spaces etc)
|
2017-04-13 19:55:39 +01:00
|
|
|
file_path = path.abspath(unquote(file_path))
|
|
|
|
if not file_path.startswith(root_path):
|
|
|
|
raise FileNotFound('File not found',
|
|
|
|
path=file_or_directory,
|
|
|
|
relative_url=file_uri)
|
2016-10-24 09:21:06 +01:00
|
|
|
try:
|
|
|
|
headers = {}
|
|
|
|
# Check if the client has been sent this file before
|
|
|
|
# and it has not been modified since
|
2017-01-31 01:04:51 +00:00
|
|
|
stats = None
|
2016-10-24 09:21:06 +01:00
|
|
|
if use_modified_since:
|
|
|
|
stats = await stat(file_path)
|
2017-01-31 01:04:51 +00:00
|
|
|
modified_since = strftime(
|
|
|
|
'%a, %d %b %Y %H:%M:%S GMT', gmtime(stats.st_mtime))
|
2016-10-24 09:21:06 +01:00
|
|
|
if request.headers.get('If-Modified-Since') == modified_since:
|
|
|
|
return HTTPResponse(status=304)
|
|
|
|
headers['Last-Modified'] = modified_since
|
2017-01-31 01:04:51 +00:00
|
|
|
_range = None
|
|
|
|
if use_content_range:
|
|
|
|
_range = None
|
|
|
|
if not stats:
|
|
|
|
stats = await stat(file_path)
|
|
|
|
headers['Accept-Ranges'] = 'bytes'
|
|
|
|
headers['Content-Length'] = str(stats.st_size)
|
|
|
|
if request.method != 'HEAD':
|
|
|
|
try:
|
|
|
|
_range = ContentRangeHandler(request, stats)
|
|
|
|
except HeaderNotFound:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
del headers['Content-Length']
|
|
|
|
for key, value in _range.headers.items():
|
|
|
|
headers[key] = value
|
|
|
|
if request.method == 'HEAD':
|
|
|
|
return HTTPResponse(
|
|
|
|
headers=headers,
|
|
|
|
content_type=guess_type(file_path)[0] or 'text/plain')
|
|
|
|
else:
|
|
|
|
return await file(file_path, headers=headers, _range=_range)
|
|
|
|
except ContentRangeError:
|
|
|
|
raise
|
|
|
|
except Exception:
|
2016-10-24 09:21:06 +01:00
|
|
|
raise FileNotFound('File not found',
|
|
|
|
path=file_or_directory,
|
|
|
|
relative_url=file_uri)
|
|
|
|
|
2017-01-31 01:04:51 +00:00
|
|
|
app.route(uri, methods=['GET', 'HEAD'])(_handler)
|