diff --git a/app.py b/app.py index 2de3d22..e79b22f 100755 --- a/app.py +++ b/app.py @@ -9,7 +9,6 @@ import mimetypes from datetime import datetime, date, timedelta import diskcache import threading -import json import time from flask_socketio import SocketIO, emit import geoip2.database @@ -24,11 +23,7 @@ import auth import analytics as a import folder_secret_config_editor as fsce -with open("app_config.json", 'r') as file: - app_config = json.load(file) - -with open('folder_secret_config.json') as file: - folder_config = json.load(file) +app_config = auth.return_app_config() cache_audio = diskcache.Cache('./filecache_audio', size_limit= app_config['filecache_size_limit_audio'] * 1024**3) cache_image = diskcache.Cache('./filecache_image', size_limit= app_config['filecache_size_limit_image'] * 1024**3) @@ -55,7 +50,7 @@ app.add_url_rule('/searchcommand', view_func=search.searchcommand, methods=['POS app.add_url_rule('/songs_dashboard', view_func=a.songs_dashboard) app.add_url_rule('/admin/folder_secret_config_editor', view_func=auth.require_admin(fsce.folder_secret_config_editor), methods=['GET', 'POST']) -app.add_url_rule('/admin/folder_secret_config_editor/data', view_func=auth.require_admin(fsce.folder_secret_config_data)) +app.add_url_rule('/admin/folder_secret_config_editor/data', view_func=auth.require_admin(auth.load_folder_config)) app.add_url_rule('/admin/folder_secret_config_editor/action', view_func=auth.require_admin(fsce.folder_secret_config_action), methods=['POST']) # Grab the HOST_RULE environment variable @@ -104,6 +99,7 @@ def list_directory_contents(directory, subpath): """ directories = [] files = [] + folder_config = auth.return_folder_config() transcription_dir = os.path.join(directory, "Transkription") transcription_exists = os.path.isdir(transcription_dir) @@ -438,9 +434,10 @@ def get_transcript(subpath): def create_share(subpath): scheme = request.scheme # current scheme (http or https) host = request.host - if 'admin ' not in session and not session.get('admin'): + if 'admin' not in session and not session.get('admin'): return "Unauthorized", 403 + folder_config = auth.return_folder_config() paths = {} for item in folder_config: for folder in item['folders']: @@ -550,6 +547,7 @@ def handle_request_initial_data(): @app.route('/') @auth.require_secret def index(path): + app_config = auth.return_app_config() title_short = app_config.get('TITLE_SHORT', 'Default Title') title_long = app_config.get('TITLE_LONG' , 'Default Title') diff --git a/auth.py b/auth.py index f207c5b..01d27aa 100644 --- a/auth.py +++ b/auth.py @@ -12,29 +12,42 @@ import zlib import hmac import hashlib +FOLDER_CONFIG_FILENAME = 'folder_secret_config.json' +APP_CONFIG_FILENAME = 'app_config.json' - -with open("app_config.json", 'r') as file: +# initial read of the config files +with open(APP_CONFIG_FILENAME, 'r') as file: app_config = json.load(file) -with open('folder_secret_config.json') as file: +with open(FOLDER_CONFIG_FILENAME) as file: folder_config = json.load(file) +# functions to be used by other modules +def load_folder_config(): + global folder_config + with open(FOLDER_CONFIG_FILENAME) as file: + folder_config = json.load(file) + return folder_config + +def load_app_config(): + global app_config + with open(APP_CONFIG_FILENAME, 'r') as file: + app_config = json.load(file) + return app_config + +def return_folder_config(): + return folder_config + +def return_app_config(): + return app_config + + def is_admin(): """ Check if the user is an admin based on the session. """ return session.get('admin', False) -def require_admin(f): - @wraps(f) - @require_secret - def decorated_function(*args, **kwargs): - if is_admin(): - return f(*args, **kwargs) - else: - return "You don't have admin permission", 403 - return decorated_function def require_secret(f): @wraps(f) @@ -150,6 +163,24 @@ def require_secret(f): ) return decorated_function +def require_admin(f): + @wraps(f) + @require_secret + def decorated_function(*args, **kwargs): + if is_admin(): + return f(*args, **kwargs) + else: + return "You don't have admin permission", 403 + return decorated_function + +@require_admin +def save_folder_config(data): + global folder_config + folder_config = data + with open(FOLDER_CONFIG_FILENAME, 'w') as file: + json.dump(folder_config, file, indent=4) + return folder_config + @require_secret def mylinks(): scheme = request.scheme # current scheme (http or https) diff --git a/folder_secret_config_editor.py b/folder_secret_config_editor.py index de07349..7e93f2d 100644 --- a/folder_secret_config_editor.py +++ b/folder_secret_config_editor.py @@ -7,39 +7,18 @@ import string import auth -DATA_FILE = 'folder_secret_config.json' - # Secret alphabet ALPHABET = string.ascii_letters + string.digits -@auth.require_admin -def load_data(): - with open(DATA_FILE) as f: - try: - data = json.load(f) - print(f"Loaded {len(data)} records from {DATA_FILE}.") - return data - except: - print(f"Error loading {DATA_FILE}. File may be empty or corrupted.") - return [] - -@auth.require_admin -def save_data(data): - with open(DATA_FILE, 'w') as f: - json.dump(data, f, indent=4) @auth.require_admin def folder_secret_config_editor(): return render_template('folder_secret_config_editor.html', alphabet=ALPHABET, admin_enabled=auth.is_admin()) -@auth.require_admin -def folder_secret_config_data(): - return jsonify(load_data()) - @auth.require_admin def folder_secret_config_action(): p = request.get_json() - data = load_data() + data = auth.return_folder_config() action = p.get('action') if action == 'delete': data = [r for r in data if r['secret'] != p['secret']] @@ -58,6 +37,6 @@ def folder_secret_config_action(): 'validity': datetime.strptime(p['validity'], '%Y-%m-%d').strftime('%d.%m.%Y'), 'folders': p['folders'] }) - save_data(data) + auth.save_folder_config(data) return jsonify(success=True) diff --git a/templates/app.html b/templates/app.html index 00363f1..ab39453 100644 --- a/templates/app.html +++ b/templates/app.html @@ -45,12 +45,16 @@
{% if admin_enabled %} -
- App · - Meine Links · - Verbindungen · - Auswertung · - Folder Config Editor +
+ App + | + Meine Links + | + Verbindungen + | + Auswertung + | + Ordnerkonfiguration
{% endif %} diff --git a/templates/base.html b/templates/base.html index a882162..3a5edae 100644 --- a/templates/base.html +++ b/templates/base.html @@ -51,7 +51,7 @@ {% if admin_enabled %} - + {% endif %}
diff --git a/templates/folder_secret_config_editor.html b/templates/folder_secret_config_editor.html index 964be38..6eee2e6 100644 --- a/templates/folder_secret_config_editor.html +++ b/templates/folder_secret_config_editor.html @@ -2,10 +2,10 @@ {% extends 'base.html' %} {# page title #} -{% block title %}Edit Folder Config{% endblock %} +{% block title %}Ordnerkonfiguration{% endblock %} {# override navbar text: #} -{% block nav_brand %}Folder Config Editor{% endblock %} +{% block nav_brand %}Ordnerkonfiguration{% endblock %} {# page content #} {% block content %} diff --git a/templates/mylinks.html b/templates/mylinks.html index 7c534d3..b003fcf 100644 --- a/templates/mylinks.html +++ b/templates/mylinks.html @@ -27,7 +27,7 @@

Gültig bis: {{ secret_valid_to[secret] }}

- Link öffnen + Link öffnen + Link öffnen +

Gültig bis: