This commit is contained in:
thematdev 2022-07-15 14:08:34 +03:00
commit 98a8fbdc0c
2 changed files with 17 additions and 4 deletions

View File

@ -1,7 +1,7 @@
from flask import Flask, url_for, render_template, abort
import os
import json
from app.config import PANDOC_LINK, PANDOC_PATH
from app.config import PANDOC_LINK, PANDOC_PATH, WHITELIST_PATH
from app.hash_manager import hash_file_sha512
import subprocess
@ -27,6 +27,11 @@ def dated_url_for(endpoint, **values):
def render_page(page):
path = f'{PANDOC_PATH}/{page}'
whitelist = WHITELIST_PATH
with open(whitelist, 'r') as f:
lines = f.read().splitlines()
if page not in lines:
raise Exception("Page doesn't exist!")
if not os.path.exists(f'{path}'):
raise Exception("Page doesn't exist!")
in_filename = f'{path}/main.md'
@ -39,12 +44,19 @@ def render_page(page):
@app.route(f'/{PANDOC_LINK}/<page>')
def get_pandoc_page(page):
path = f'{PANDOC_PATH}/{page}'
whitelist = WHITELIST_PATH
with open(whitelist, 'r') as f:
lines = f.read().splitlines()
if page not in lines:
print(f'Access to page not in list {page}')
return 'This page does not exist'
if not os.path.exists(f'{path}'):
abort(404)
# TODO: Add 404 handler
return 'This page does not exist'
with open(f'{path}/config.json') as f:
data = json.loads(f.read())
if not os.path.exists(f'{path}/render.html') or not os.path.exists(f'{path}/render.html.lock'):
print('Rendered page or lockfile for {page} does not exist! Rendering {page}')
print(f'Rendered page or lockfile for {page} does not exist! Rendering {page}')
render_page(page)
else:
with open(f'{path}/render.html.lock', 'r') as f:
@ -52,7 +64,7 @@ def get_pandoc_page(page):
current_hash = hash_file_sha512(f'{path}/main.md')
if rendered_hash != current_hash:
print(f'CURRENT: {current_hash}, RENDERED: {rendered_hash}')
print('Integrity test failed, rendering {page}!')
print(f'Integrity test failed, rendering {page}!')
render_page(page)
template = data['template']
with open(f'{path}/render.html', 'r') as f:

View File

@ -1,2 +1,3 @@
PANDOC_LINK = 'page'
PANDOC_PATH = '/home/thematdev/pandoc_pages'
WHITELIST_PATH = f'{PANDOC_PATH}/pages'