treat symlinks as redirects

closes #7
This commit is contained in:
2021-04-15 21:43:29 -05:00
parent 71ead20f3f
commit c90f0a3a42
5 changed files with 44 additions and 1 deletions

View File

@@ -22,7 +22,8 @@ def display_page(path):
"""Get the file contents of the requested path and render the file."""
try:
resolved_path, render_type = request_path_to_instance_resource_path(path)
logger.debug("received request for path '%s', resolved to '%s'", path, resolved_path)
logger.debug("received request for path '%s', resolved to '%s', type '%s'",
path, resolved_path, render_type)
except PermissionError:
abort(400)
except IsADirectoryError:
@@ -32,6 +33,8 @@ def display_page(path):
if render_type == 'file':
return send_from_directory(app.instance_path, resolved_path)
elif render_type == 'symlink':
return redirect(instance_resource_path_to_request_path(resolved_path), code=301)
elif render_type == 'markdown':
try:
with app.open_instance_resource(resolved_path, 'r') as entry_file:
@@ -77,6 +80,15 @@ def request_path_to_instance_resource_path(path):
logger.warning("client tried to request a path '%s' outside of the base_dir!", path)
raise PermissionError
# if this is a (valid) symlink, find what it's pointed to and redirect the user
if os.path.islink(os.path.join(base_dir, path)):
logger.info("client requested a path '%s' that is actually a symlink to file '%s'", path, resolved_path)
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'symlink'
elif os.path.islink(os.path.join(base_dir, f'{path}.md')):
resolved_path = os.path.realpath(os.path.join(base_dir, f'{path}.md'))
logger.info("client requested a path '%s' that is actually a symlink to file '%s'", path, resolved_path)
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), 'symlink'
# if this is a file-like requset but actually a directory, redirect the user
if os.path.isdir(resolved_path) and not path.endswith('/'):
logger.info("client requested a path '%s' that is actually a directory", path)