static file path
This commit is contained in:
@@ -10,9 +10,10 @@ def create_app(test_config=None):
|
|||||||
app.config.from_mapping(
|
app.config.from_mapping(
|
||||||
# a default secret that should be overridden by instance config
|
# a default secret that should be overridden by instance config
|
||||||
SECRET_KEY="dev",
|
SECRET_KEY="dev",
|
||||||
URL_PREFIX = "/"
|
URL_PREFIX = "/",
|
||||||
)
|
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
if not test_config:
|
if not test_config:
|
||||||
# load the instance config, if it exists, when not testing
|
# load the instance config, if it exists, when not testing
|
||||||
app.config.from_pyfile("cfg.py")
|
app.config.from_pyfile("cfg.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
|
from flatpages_index import FlatPagesIndex
|
||||||
import flatpages_index
|
import flatpages_index
|
||||||
import os
|
import os
|
||||||
@@ -21,6 +21,7 @@ def create_bp(app):
|
|||||||
def thumb(size=64,name=''):
|
def thumb(size=64,name=''):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@page_blueprint.route('/<path:name>/')
|
@page_blueprint.route('/<path:name>/')
|
||||||
@page_blueprint.route('/')
|
@page_blueprint.route('/')
|
||||||
def page(name='index'):
|
def page(name='index'):
|
||||||
@@ -32,12 +33,19 @@ def create_bp(app):
|
|||||||
page.links.endpoint='pages.page'
|
page.links.endpoint='pages.page'
|
||||||
return render_template(page["template"], post=page,
|
return render_template(page["template"], post=page,
|
||||||
pth=page["dirpath"])
|
pth=page["dirpath"])
|
||||||
|
app.logger.debug(f"delivering static file: {name}")
|
||||||
if os.path.exists(os.path.join(app.config['FLATPAGES_ROOT'],name)):
|
|
||||||
return send_from_directory(app.config['FLATPAGES_ROOT'],name)
|
# search different loctions for static file
|
||||||
else:
|
paths = [ app.config['FLATPAGES_ROOT'],
|
||||||
return send_from_directory('static',name)
|
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)
|
@page_blueprint.route('/<path:name>.json',strict_slashes=False)
|
||||||
def pagejson(name='index'):
|
def pagejson(name='index'):
|
||||||
@@ -48,10 +56,16 @@ def create_bp(app):
|
|||||||
# page.links.file_url=lambda n: url_for('intern.post', name=n)
|
# page.links.file_url=lambda n: url_for('intern.post', name=n)
|
||||||
return jsonify(page=dict(page))
|
return jsonify(page=dict(page))
|
||||||
|
|
||||||
if os.path.exists(u'{}/{}'.format(app.config['FLATPAGES_ROOT'],path)):
|
# search different loctions for static file
|
||||||
return send_from_directory(app.config['FLATPAGES_ROOT'],path)
|
paths = [ app.config['FLATPAGES_ROOT'],
|
||||||
else:
|
os.path.abspath(app.config['STATIC_ROOT'])
|
||||||
return send_from_directory('static',path)
|
]
|
||||||
|
|
||||||
|
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
|
return page_blueprint
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user