26 lines
757 B
Python
26 lines
757 B
Python
from pathlib import Path
|
|
from uuid import uuid4
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
|
AUDIO_DIR = BASE_DIR / "data" / "audio"
|
|
AUDIO_DIR.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
class AudioManager:
|
|
|
|
@staticmethod
|
|
def save_audio(audio_bytes: bytes, extension: str = "mp3") -> str:
|
|
ext = extension.lstrip(".")
|
|
audio_id = f"aud_{uuid4().hex[:12]}"
|
|
file_path = AUDIO_DIR / f"{audio_id}.{ext}"
|
|
file_path.write_bytes(audio_bytes)
|
|
return audio_id
|
|
|
|
@staticmethod
|
|
def get_audio_path(audio_id: str) -> Path | None:
|
|
for ext in [".wav", ".mp3", ".webm", ".m4a", ".ogg"]:
|
|
p = AUDIO_DIR / f"{audio_id}{ext}"
|
|
if p.exists():
|
|
return p
|
|
return None
|