Files
triviathang/backend/services/audio_service.py
2025-12-22 14:47:25 -05:00

40 lines
1021 B
Python

import os
import uuid
def allowed_audio_file(filename, allowed_extensions):
"""Check if file has allowed audio extension"""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in allowed_extensions
def get_audio_path(unique_filename):
"""Get URL path for serving audio"""
return f"/static/audio/{unique_filename}"
def generate_audio_filename(extension='mp3'):
"""Generate unique audio filename"""
return f"{uuid.uuid4()}.{extension}"
def delete_audio(audio_path, base_folder):
"""
Delete an audio file
Args:
audio_path: Relative path to audio (e.g., /static/audio/abc123.mp3)
base_folder: Base folder for the application
"""
if not audio_path:
return
relative_path = audio_path.lstrip('/')
full_path = os.path.join(base_folder, relative_path)
try:
if os.path.exists(full_path):
os.remove(full_path)
except Exception as e:
print(f"Error deleting audio {full_path}: {str(e)}")