Some checks failed
Build and Push Docker Images / changes (push) Has been cancelled
Build and Push Docker Images / build-backend (push) Has been cancelled
Build and Push Docker Images / build-frontend (push) Has been cancelled
Build and Push Docker Images / build-admin-frontend (push) Has been cancelled
39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
"""Story audio cache storage helpers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
def build_story_audio_path(story_id: int) -> str:
|
|
"""Build the cache path for a story audio file."""
|
|
|
|
return str(Path(settings.story_audio_cache_dir) / f"story-{story_id}.mp3")
|
|
|
|
|
|
def audio_cache_exists(audio_path: str | None) -> bool:
|
|
"""Whether the cached audio file exists on disk."""
|
|
|
|
return bool(audio_path) and Path(audio_path).is_file()
|
|
|
|
|
|
def read_audio_cache(audio_path: str) -> bytes:
|
|
"""Read cached story audio bytes."""
|
|
|
|
return Path(audio_path).read_bytes()
|
|
|
|
|
|
def write_story_audio_cache(story_id: int, audio_data: bytes) -> str:
|
|
"""Persist story audio and return the saved file path."""
|
|
|
|
final_path = Path(build_story_audio_path(story_id))
|
|
final_path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
temp_path = final_path.with_suffix(".tmp")
|
|
temp_path.write_bytes(audio_data)
|
|
temp_path.replace(final_path)
|
|
|
|
return str(final_path)
|