allow for serving files directly inside pages/
This commit is contained in:
@@ -6,7 +6,7 @@ import re
|
||||
|
||||
from flask import Blueprint, Markup, abort
|
||||
from flask import current_app as app
|
||||
from flask import redirect, request
|
||||
from flask import redirect, request, send_from_directory
|
||||
from tzlocal import get_localzone
|
||||
|
||||
from incorporealcms.lib import get_meta_str, init_md, render
|
||||
@@ -21,7 +21,7 @@ bp = Blueprint('pages', __name__, url_prefix='/')
|
||||
def display_page(path):
|
||||
"""Get the file contents of the requested path and render the file."""
|
||||
try:
|
||||
resolved_path = request_path_to_instance_resource_path(path)
|
||||
resolved_path, render_md = request_path_to_instance_resource_path(path)
|
||||
logger.debug("received request for path '%s', resolved to '%s'", path, resolved_path)
|
||||
except PermissionError:
|
||||
abort(400)
|
||||
@@ -30,6 +30,9 @@ def display_page(path):
|
||||
except FileNotFoundError:
|
||||
abort(404)
|
||||
|
||||
if not render_md:
|
||||
return send_from_directory(app.instance_path, resolved_path)
|
||||
|
||||
try:
|
||||
with app.open_instance_resource(resolved_path, 'r') as entry_file:
|
||||
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(entry_file.name), get_localzone())
|
||||
@@ -76,21 +79,23 @@ def request_path_to_instance_resource_path(path):
|
||||
logger.info("client requested a path '%s' that is actually a directory", path)
|
||||
raise IsADirectoryError
|
||||
|
||||
# derive the proper markdown file depending on if this is a dir or file
|
||||
# derive the proper markdown or actual file depending on if this is a dir or file
|
||||
if os.path.isdir(resolved_path):
|
||||
absolute_resource = os.path.join(resolved_path, 'index.md')
|
||||
elif os.path.exists(resolved_path):
|
||||
logger.info("final DIRECT path = '%s' for request '%s'", resolved_path, path)
|
||||
return resolved_path.replace(f'{app.instance_path}{os.path.sep}', ''), False
|
||||
else:
|
||||
absolute_resource = f'{resolved_path}.md'
|
||||
|
||||
logger.info("final path = '%s' for request '%s'", absolute_resource, path)
|
||||
|
||||
# does the final file actually exist?
|
||||
if not os.path.exists(absolute_resource):
|
||||
logger.warning("requested final path '%s' does not exist!", absolute_resource)
|
||||
raise FileNotFoundError
|
||||
|
||||
logger.info("final path = '%s' for request '%s'", absolute_resource, path)
|
||||
# we checked that the file exists via absolute path, but now we need to give the path relative to instance dir
|
||||
return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', '')
|
||||
return absolute_resource.replace(f'{app.instance_path}{os.path.sep}', ''), True
|
||||
|
||||
|
||||
def instance_resource_path_to_request_path(path):
|
||||
|
||||
Reference in New Issue
Block a user