static file path

This commit is contained in:
2021-11-28 21:30:55 +00:00
parent 993d8ec69b
commit 713488c453
2 changed files with 28 additions and 13 deletions

View File

@@ -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")

View File

@@ -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('/<path:name>/')
@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('/<path:name>.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