feat: Add sounds routes for serving audio and thumbnail files

This commit is contained in:
JSC
2025-07-08 12:57:17 +02:00
parent 96ab2bdf77
commit 193bd5ebf4
3 changed files with 118 additions and 3 deletions

View File

@@ -6,7 +6,7 @@ import time
from typing import Any, Optional
import vlc
from flask import current_app
from flask import current_app, request
from app.models.playlist import Playlist
from app.models.sound import Sound
@@ -125,6 +125,20 @@ class MusicPlayerService:
logger.error(f"Error loading playlist {playlist_id}: {e}")
return False
def _build_thumbnail_url(self, sound_type: str, thumbnail_filename: str) -> str:
"""Build absolute thumbnail URL."""
try:
# Try to get base URL from current request context
if request:
base_url = request.url_root.rstrip('/')
else:
# Fallback to localhost if no request context
base_url = "http://localhost:5000"
return f"{base_url}/api/sounds/{sound_type.lower()}/thumbnails/{thumbnail_filename}"
except Exception:
# Fallback if request context is not available
return f"http://localhost:5000/api/sounds/{sound_type.lower()}/thumbnails/{thumbnail_filename}"
def _load_playlist_with_context(self, playlist) -> bool:
"""Load playlist with database context already established."""
try:
@@ -402,7 +416,7 @@ class MusicPlayerService:
"artist": None, # Could be extracted from metadata
"duration": sound.duration or 0,
"thumbnail": (
f"/api/sounds/{sound.type.lower()}/thumbnails/{sound.thumbnail}"
self._build_thumbnail_url(sound.type, sound.thumbnail)
if sound.thumbnail
else None
),
@@ -437,7 +451,7 @@ class MusicPlayerService:
"artist": None,
"duration": sound.duration or 0,
"thumbnail": (
f"/api/sounds/{sound.type.lower()}/thumbnails/{sound.thumbnail}"
self._build_thumbnail_url(sound.type, sound.thumbnail)
if sound.thumbnail
else None
),