diff --git a/flaskpages/__init__.py b/flaskpages/__init__.py index 7e8d6a0..a98e11f 100644 --- a/flaskpages/__init__.py +++ b/flaskpages/__init__.py @@ -10,9 +10,10 @@ def create_app(test_config=None): app.config.from_mapping( # a default secret that should be overridden by instance config SECRET_KEY="dev", - URL_PREFIX = "/" - ) + URL_PREFIX = "/", + ) + if not test_config: # load the instance config, if it exists, when not testing app.config.from_pyfile("cfg.py") diff --git a/flaskpages/pages.py b/flaskpages/pages.py index e0834e7..d5a58f2 100644 --- a/flaskpages/pages.py +++ b/flaskpages/pages.py @@ -1,5 +1,5 @@ -from flask import Flask, Blueprint,render_template, send_from_directory,jsonify, url_for +from flask import Flask, Blueprint,render_template, send_from_directory,jsonify, url_for, abort from flatpages_index import FlatPagesIndex import flatpages_index import os @@ -21,6 +21,7 @@ def create_bp(app): def thumb(size=64,name=''): pass + @page_blueprint.route('//') @page_blueprint.route('/') def page(name='index'): @@ -32,12 +33,19 @@ def create_bp(app): page.links.endpoint='pages.page' return render_template(page["template"], post=page, pth=page["dirpath"]) - - if os.path.exists(os.path.join(app.config['FLATPAGES_ROOT'],name)): - return send_from_directory(app.config['FLATPAGES_ROOT'],name) - else: - return send_from_directory('static',name) - + app.logger.debug(f"delivering static file: {name}") + + # search different loctions for static file + paths = [ app.config['FLATPAGES_ROOT'], + os.path.abspath(app.config['STATIC_ROOT']) + ] + + for p in paths: + if os.path.exists(os.path.join(p,name)): + return send_from_directory(p,name) + app.logger.debug(f"Static file {name} not found in : {paths}") + return abort(404) + @page_blueprint.route('/.json',strict_slashes=False) def pagejson(name='index'): @@ -48,10 +56,16 @@ def create_bp(app): # page.links.file_url=lambda n: url_for('intern.post', name=n) return jsonify(page=dict(page)) - if os.path.exists(u'{}/{}'.format(app.config['FLATPAGES_ROOT'],path)): - return send_from_directory(app.config['FLATPAGES_ROOT'],path) - else: - return send_from_directory('static',path) + # search different loctions for static file + paths = [ app.config['FLATPAGES_ROOT'], + os.path.abspath(app.config['STATIC_ROOT']) + ] + + for p in paths: + if os.path.exists(os.path.join(p,name)): + return send_from_directory(p,name) + app.logger.debug(f"Static file {name} not found in : {paths}") + return abort(404) return page_blueprint \ No newline at end of file