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
|
2018-10-18 05:20:16 +01:00
|
|
|
from time import gmtime, strftime
|
2016-12-10 11:16:37 +00:00
|
|
|
from urllib.parse import unquote
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2020-01-20 16:29:06 +00:00
|
|
|
from sanic.compat import stat_async
|
2017-02-16 02:54:00 +00:00
|
|
|
from sanic.exceptions import (
|
|
|
|
ContentRangeError,
|
|
|
|
FileNotFound,
|
|
|
|
HeaderNotFound,
|
|
|
|
InvalidUsage,
|
|
|
|
)
|
|
|
|
from sanic.handlers import ContentRangeHandler
|
2018-10-18 05:20:16 +01:00
|
|
|
from sanic.response import HTTPResponse, file, file_stream
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
def register(
|
|
|
|
app,
|
|
|
|
uri,
|
|
|
|
file_or_directory,
|
|
|
|
pattern,
|
|
|
|
use_modified_since,
|
|
|
|
use_content_range,
|
|
|
|
stream_large_files,
|
|
|
|
name="static",
|
|
|
|
host=None,
|
|
|
|
strict_slashes=None,
|
|
|
|
content_type=None,
|
|
|
|
):
|
2017-01-31 01:04:51 +00:00
|
|
|
# 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
|
2017-05-18 09:30:54 +01:00
|
|
|
:param stream_large_files: If true, use the file_stream() handler rather
|
|
|
|
than the file() handler to send the file
|
|
|
|
If this is an integer, this represents the
|
|
|
|
threshold size to switch to file_stream()
|
2017-09-06 12:17:52 +01:00
|
|
|
:param name: user defined name used for url_for
|
2018-07-21 06:31:15 +01:00
|
|
|
:param content_type: user defined content type for header
|
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):
|
2018-10-14 01:55:33 +01:00
|
|
|
uri += "<file_uri:" + pattern + ">"
|
2016-10-24 09:21:06 +01:00
|
|
|
|
|
|
|
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
|
2018-10-14 01:55:33 +01:00
|
|
|
if file_uri and "../" in file_uri:
|
2016-10-24 09:21:06 +01:00
|
|
|
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(
|
2018-10-14 01:55:33 +01:00
|
|
|
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))
|
2017-04-17 05:58:10 +01:00
|
|
|
if not file_path.startswith(path.abspath(unquote(root_path))):
|
2018-10-14 01:55:33 +01:00
|
|
|
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:
|
2020-01-20 16:29:06 +00:00
|
|
|
stats = await stat_async(file_path)
|
2017-01-31 01:04:51 +00:00
|
|
|
modified_since = strftime(
|
2018-10-14 01:55:33 +01:00
|
|
|
"%a, %d %b %Y %H:%M:%S GMT", gmtime(stats.st_mtime)
|
|
|
|
)
|
|
|
|
if request.headers.get("If-Modified-Since") == modified_since:
|
2016-10-24 09:21:06 +01:00
|
|
|
return HTTPResponse(status=304)
|
2018-10-14 01:55:33 +01:00
|
|
|
headers["Last-Modified"] = modified_since
|
2017-01-31 01:04:51 +00:00
|
|
|
_range = None
|
|
|
|
if use_content_range:
|
|
|
|
_range = None
|
|
|
|
if not stats:
|
2020-01-20 16:29:06 +00:00
|
|
|
stats = await stat_async(file_path)
|
2018-10-14 01:55:33 +01:00
|
|
|
headers["Accept-Ranges"] = "bytes"
|
|
|
|
headers["Content-Length"] = str(stats.st_size)
|
|
|
|
if request.method != "HEAD":
|
2017-01-31 01:04:51 +00:00
|
|
|
try:
|
|
|
|
_range = ContentRangeHandler(request, stats)
|
|
|
|
except HeaderNotFound:
|
|
|
|
pass
|
|
|
|
else:
|
2018-10-14 01:55:33 +01:00
|
|
|
del headers["Content-Length"]
|
2017-01-31 01:04:51 +00:00
|
|
|
for key, value in _range.headers.items():
|
|
|
|
headers[key] = value
|
2018-10-14 01:55:33 +01:00
|
|
|
headers["Content-Type"] = (
|
|
|
|
content_type or guess_type(file_path)[0] or "text/plain"
|
|
|
|
)
|
|
|
|
if request.method == "HEAD":
|
2018-07-21 06:31:15 +01:00
|
|
|
return HTTPResponse(headers=headers)
|
2017-01-31 01:04:51 +00:00
|
|
|
else:
|
2017-05-18 09:30:54 +01:00
|
|
|
if stream_large_files:
|
2018-11-20 04:28:00 +00:00
|
|
|
if type(stream_large_files) == int:
|
2017-05-18 09:30:54 +01:00
|
|
|
threshold = stream_large_files
|
|
|
|
else:
|
2017-11-03 13:37:01 +00:00
|
|
|
threshold = 1024 * 1024
|
2017-05-18 09:30:54 +01:00
|
|
|
|
|
|
|
if not stats:
|
2020-01-20 16:29:06 +00:00
|
|
|
stats = await stat_async(file_path)
|
2017-05-18 09:30:54 +01:00
|
|
|
if stats.st_size >= threshold:
|
2018-10-14 01:55:33 +01:00
|
|
|
return await file_stream(
|
|
|
|
file_path, headers=headers, _range=_range
|
|
|
|
)
|
2017-01-31 01:04:51 +00:00
|
|
|
return await file(file_path, headers=headers, _range=_range)
|
|
|
|
except ContentRangeError:
|
|
|
|
raise
|
|
|
|
except Exception:
|
2018-10-14 01:55:33 +01:00
|
|
|
raise FileNotFound(
|
|
|
|
"File not found", path=file_or_directory, relative_url=file_uri
|
|
|
|
)
|
2016-10-24 09:21:06 +01:00
|
|
|
|
2017-09-06 12:17:52 +01:00
|
|
|
# special prefix for static files
|
2018-10-14 01:55:33 +01:00
|
|
|
if not name.startswith("_static_"):
|
|
|
|
name = "_static_{}".format(name)
|
2017-09-06 12:17:52 +01:00
|
|
|
|
2018-10-14 01:55:33 +01:00
|
|
|
app.route(
|
|
|
|
uri,
|
|
|
|
methods=["GET", "HEAD"],
|
|
|
|
name=name,
|
|
|
|
host=host,
|
|
|
|
strict_slashes=strict_slashes,
|
|
|
|
)(_handler)
|