Refactor OAuth provider linking and unlinking logic into a dedicated service; enhance error handling and logging throughout the application; improve sound management and scanning services with better file handling and unique naming; implement centralized error and logging services for consistent API responses and application-wide logging configuration.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
"""Admin sound management routes."""
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
from app.models.sound import Sound
|
||||
from app.services.sound_scanner_service import SoundScannerService
|
||||
from app.services.sound_normalizer_service import SoundNormalizerService
|
||||
from flask import Blueprint, request
|
||||
|
||||
from app.services.decorators import require_admin
|
||||
from app.services.error_handling_service import ErrorHandlingService
|
||||
from app.services.sound_normalizer_service import SoundNormalizerService
|
||||
from app.services.sound_scanner_service import SoundScannerService
|
||||
|
||||
bp = Blueprint("admin_sounds", __name__, url_prefix="/api/admin/sounds")
|
||||
|
||||
@@ -13,29 +14,19 @@ bp = Blueprint("admin_sounds", __name__, url_prefix="/api/admin/sounds")
|
||||
@require_admin
|
||||
def scan_sounds():
|
||||
"""Manually trigger sound scanning."""
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
directory = data.get("directory")
|
||||
|
||||
result = SoundScannerService.scan_soundboard_directory(directory)
|
||||
|
||||
if result["success"]:
|
||||
return jsonify(result), 200
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
return ErrorHandlingService.wrap_service_call(
|
||||
SoundScannerService.scan_soundboard_directory,
|
||||
request.get_json().get("directory") if request.get_json() else None,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/scan/status", methods=["GET"])
|
||||
@require_admin
|
||||
def get_scan_status():
|
||||
"""Get current scan statistics and status."""
|
||||
try:
|
||||
stats = SoundScannerService.get_scan_statistics()
|
||||
return jsonify(stats), 200
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
return ErrorHandlingService.wrap_service_call(
|
||||
SoundScannerService.get_scan_statistics,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/normalize", methods=["POST"])
|
||||
@@ -52,18 +43,21 @@ def normalize_sounds():
|
||||
if sound_id:
|
||||
# Normalize specific sound
|
||||
result = SoundNormalizerService.normalize_sound(
|
||||
sound_id, overwrite, two_pass
|
||||
sound_id,
|
||||
overwrite,
|
||||
two_pass,
|
||||
)
|
||||
else:
|
||||
# Normalize all sounds
|
||||
result = SoundNormalizerService.normalize_all_sounds(
|
||||
overwrite, limit, two_pass
|
||||
overwrite,
|
||||
limit,
|
||||
two_pass,
|
||||
)
|
||||
|
||||
if result["success"]:
|
||||
return jsonify(result), 200
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
@@ -94,125 +88,26 @@ def check_ffmpeg():
|
||||
@require_admin
|
||||
def list_sounds():
|
||||
"""Get detailed list of all sounds with normalization status."""
|
||||
try:
|
||||
sound_type = request.args.get("type", "SDB")
|
||||
page = int(request.args.get("page", 1))
|
||||
per_page = int(request.args.get("per_page", 50))
|
||||
from app.services.sound_management_service import SoundManagementService
|
||||
|
||||
# Validate sound type
|
||||
if sound_type not in ["SDB", "SAY", "STR"]:
|
||||
return jsonify({"error": "Invalid sound type"}), 400
|
||||
|
||||
# Get paginated results
|
||||
sounds_query = Sound.query.filter_by(type=sound_type)
|
||||
total = sounds_query.count()
|
||||
|
||||
sounds = (
|
||||
sounds_query.offset((page - 1) * per_page).limit(per_page).all()
|
||||
)
|
||||
|
||||
# Convert to detailed dict format
|
||||
sounds_data = []
|
||||
for sound in sounds:
|
||||
sound_dict = sound.to_dict()
|
||||
# Add file existence status
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
original_path = os.path.join(
|
||||
"sounds", sound.type.lower(), sound.filename
|
||||
)
|
||||
sound_dict["original_exists"] = os.path.exists(original_path)
|
||||
|
||||
if sound.is_normalized and sound.normalized_filename:
|
||||
normalized_path = os.path.join(
|
||||
"sounds",
|
||||
"normalized",
|
||||
sound.type.lower(),
|
||||
sound.normalized_filename,
|
||||
)
|
||||
sound_dict["normalized_exists"] = os.path.exists(
|
||||
normalized_path
|
||||
)
|
||||
else:
|
||||
sound_dict["normalized_exists"] = False
|
||||
|
||||
sounds_data.append(sound_dict)
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"sounds": sounds_data,
|
||||
"pagination": {
|
||||
"page": page,
|
||||
"per_page": per_page,
|
||||
"total": total,
|
||||
"pages": (total + per_page - 1) // per_page,
|
||||
},
|
||||
"type": sound_type,
|
||||
}
|
||||
), 200
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
return ErrorHandlingService.wrap_service_call(
|
||||
SoundManagementService.get_sounds_with_file_status,
|
||||
request.args.get("type", "SDB"),
|
||||
int(request.args.get("page", 1)),
|
||||
int(request.args.get("per_page", 50)),
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/<int:sound_id>", methods=["DELETE"])
|
||||
@require_admin
|
||||
def delete_sound(sound_id: int):
|
||||
"""Delete a sound and its files."""
|
||||
try:
|
||||
sound = Sound.query.get(sound_id)
|
||||
if not sound:
|
||||
return jsonify({"error": "Sound not found"}), 404
|
||||
from app.services.sound_management_service import SoundManagementService
|
||||
|
||||
if not sound.is_deletable:
|
||||
return jsonify({"error": "Sound is not deletable"}), 403
|
||||
|
||||
# Delete normalized file if exists
|
||||
if sound.is_normalized and sound.normalized_filename:
|
||||
import os
|
||||
|
||||
normalized_path = os.path.join(
|
||||
"sounds",
|
||||
"normalized",
|
||||
sound.type.lower(),
|
||||
sound.normalized_filename,
|
||||
)
|
||||
if os.path.exists(normalized_path):
|
||||
try:
|
||||
os.remove(normalized_path)
|
||||
except Exception as e:
|
||||
return jsonify(
|
||||
{"error": f"Failed to delete normalized file: {e}"}
|
||||
), 500
|
||||
|
||||
# Delete original file
|
||||
import os
|
||||
|
||||
original_path = os.path.join(
|
||||
"sounds", sound.type.lower(), sound.filename
|
||||
)
|
||||
if os.path.exists(original_path):
|
||||
try:
|
||||
os.remove(original_path)
|
||||
except Exception as e:
|
||||
return jsonify(
|
||||
{"error": f"Failed to delete original file: {e}"}
|
||||
), 500
|
||||
|
||||
# Delete database record
|
||||
from app.database import db
|
||||
|
||||
db.session.delete(sound)
|
||||
db.session.commit()
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"message": f"Sound '{sound.name}' deleted successfully",
|
||||
"sound_id": sound_id,
|
||||
}
|
||||
), 200
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
return ErrorHandlingService.wrap_service_call(
|
||||
SoundManagementService.delete_sound_with_files,
|
||||
sound_id,
|
||||
)
|
||||
|
||||
|
||||
@bp.route("/<int:sound_id>/normalize", methods=["POST"])
|
||||
@@ -220,17 +115,20 @@ def delete_sound(sound_id: int):
|
||||
def normalize_single_sound(sound_id: int):
|
||||
"""Normalize a specific sound."""
|
||||
try:
|
||||
from app.services.sound_management_service import SoundManagementService
|
||||
|
||||
data = request.get_json() or {}
|
||||
overwrite = data.get("overwrite", False)
|
||||
two_pass = data.get("two_pass", True)
|
||||
|
||||
result = SoundNormalizerService.normalize_sound(
|
||||
sound_id, overwrite, two_pass
|
||||
result = SoundManagementService.normalize_sound(
|
||||
sound_id,
|
||||
overwrite,
|
||||
two_pass,
|
||||
)
|
||||
|
||||
if result["success"]:
|
||||
return jsonify(result), 200
|
||||
else:
|
||||
return jsonify(result), 400
|
||||
return jsonify(result), 400
|
||||
except Exception as e:
|
||||
return jsonify({"error": str(e)}), 500
|
||||
|
||||
Reference in New Issue
Block a user