feat: persist story generation states and cache audio
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

This commit is contained in:
2026-04-17 17:14:09 +08:00
parent 145be0e67b
commit a97a2fe005
17 changed files with 2045 additions and 849 deletions

View File

@@ -0,0 +1,38 @@
"""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)