diff --git a/app/routes/admin.py b/app/routes/admin.py index 6b25bb5..2689337 100644 --- a/app/routes/admin.py +++ b/app/routes/admin.py @@ -121,55 +121,63 @@ def _slugify(name: str) -> str: @admin_bp.route('/create_config', methods=['POST']) def admin_create_config(): - data = request.get_json() or {} - name = data.get('name') or 'Neue Lok' - hub_id = int(data.get('hub_id', 0)) - hub_type = data.get('hub_type', '4channel') - image = data.get('image') + try: + data = request.get_json(force=True, silent=True) or {} + name = data.get('name') or 'Neue Lok' + hub_id = int(data.get('hub_id', 0)) + hub_type = data.get('hub_type', '4channel') + image = data.get('image') - filename = _slugify(name) + '.json' - path = os.path.join(current_app.config['CONFIG_DIR'], filename) - if os.path.exists(path): - return jsonify({"success": False, "message": "Datei existiert bereits"}), 400 + filename = _slugify(name) + '.json' + path = os.path.join(current_app.config['CONFIG_DIR'], filename) + if os.path.exists(path): + return jsonify({"success": False, "message": "Datei existiert bereits"}), 400 - template = { - "name": name, - "image": image or "", - "hub_id": hub_id, - "hub_type": hub_type, - "channels": [], - "sounds": [] - } - os.makedirs(current_app.config['CONFIG_DIR'], exist_ok=True) - with open(path, 'w', encoding='utf-8') as f: - json.dump(template, f, ensure_ascii=False, indent=2) - logger.info(f"Neue Config angelegt: {filename}") - return jsonify({"success": True, "filename": filename}) + template = { + "name": name, + "image": image or "", + "hub_id": hub_id, + "hub_type": hub_type, + "channels": [], + "sounds": [] + } + os.makedirs(current_app.config['CONFIG_DIR'], exist_ok=True) + with open(path, 'w', encoding='utf-8') as f: + json.dump(template, f, ensure_ascii=False, indent=2) + logger.info(f"Neue Config angelegt: {filename}") + return jsonify({"success": True, "filename": filename}) + except Exception as e: + logger.exception("create_config fehlgeschlagen") + return jsonify({"success": False, "message": str(e)}), 500 @admin_bp.route('/create_theme', methods=['POST']) def admin_create_theme(): - data = request.get_json() or {} - name = data.get('name') or 'Neues Theme' - folder = data.get('folder') or _slugify(name) - filename = data.get('filename') or 'theme.json' + try: + data = request.get_json(force=True, silent=True) or {} + name = data.get('name') or 'Neues Theme' + folder = data.get('folder') or _slugify(name) + filename = data.get('filename') or 'theme.json' - base_dir = current_app.config['SOUNDBOARD_CONFIG_DIR'] - theme_dir = os.path.join(base_dir, folder) - os.makedirs(theme_dir, exist_ok=True) - path = os.path.join(theme_dir, filename) - if os.path.exists(path): - return jsonify({"success": False, "message": "Theme-Datei existiert bereits"}), 400 + base_dir = current_app.config['SOUNDBOARD_CONFIG_DIR'] + theme_dir = os.path.join(base_dir, folder) + os.makedirs(theme_dir, exist_ok=True) + path = os.path.join(theme_dir, filename) + if os.path.exists(path): + return jsonify({"success": False, "message": "Theme-Datei existiert bereits"}), 400 - template = { - "name": name, - "backgrounds": [], - "sounds": [], - "random_pool": [], - "random_limit_per_hour": 2, - "random_window_minutes": 60 - } - with open(path, 'w', encoding='utf-8') as f: - json.dump(template, f, ensure_ascii=False, indent=2) - logger.info(f"Neues Theme angelegt: {os.path.relpath(path, base_dir)}") - return jsonify({"success": True, "path": os.path.relpath(path, base_dir)}) + template = { + "name": name, + "backgrounds": [], + "sounds": [], + "random_pool": [], + "random_limit_per_hour": 2, + "random_window_minutes": 60 + } + with open(path, 'w', encoding='utf-8') as f: + json.dump(template, f, ensure_ascii=False, indent=2) + logger.info(f"Neues Theme angelegt: {os.path.relpath(path, base_dir)}") + return jsonify({"success": True, "path": os.path.relpath(path, base_dir)}) + except Exception as e: + logger.exception("create_theme fehlgeschlagen") + return jsonify({"success": False, "message": str(e)}), 500 diff --git a/configs/moc-130550-sierra-railway-no-3-locomotivelesdiylesdiy-286276.webp b/configs/moc-130550-sierra-railway-no-3-locomotivelesdiylesdiy-286276.webp new file mode 100644 index 0000000..524f5e5 Binary files /dev/null and b/configs/moc-130550-sierra-railway-no-3-locomotivelesdiylesdiy-286276.webp differ diff --git a/templates/admin.html b/templates/admin.html index 5cddd03..5dc2b03 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -212,7 +212,9 @@ headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); - const json = await res.json(); + const text = await res.text(); + let json; + try { json = JSON.parse(text); } catch (_) { throw new Error('Serverantwort ist kein JSON:\n'+text); } if (!json.success) throw new Error(json.message || 'Fehler'); alert('Konfiguration angelegt: ' + json.filename); location.reload(); @@ -233,7 +235,9 @@ headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data) }); - const json = await res.json(); + const text = await res.text(); + let json; + try { json = JSON.parse(text); } catch (_) { throw new Error('Serverantwort ist kein JSON:\n'+text); } if (!json.success) throw new Error(json.message || 'Fehler'); alert('Theme angelegt: ' + json.path); location.reload();