feat: Add stream URL generation and service URL retrieval for sounds in music player service

This commit is contained in:
JSC
2025-07-18 22:38:40 +02:00
parent d0bda6c930
commit 39b7e14ae9

View File

@@ -167,6 +167,20 @@ class MusicPlayerService:
# Fallback if request context is not available
return f"http://localhost:5000/api/sounds/{sound_type.lower()}/thumbnails/{thumbnail_filename}"
def _build_stream_url(self, sound_type: str, filename: str) -> str:
"""Build absolute stream 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()}/audio/{filename}"
except Exception:
# Fallback if request context is not available
return f"http://localhost:5000/api/sounds/{sound_type.lower()}/audio/{filename}"
def _load_playlist_with_context(
self, playlist, reload: bool = False
) -> bool:
@@ -499,6 +513,12 @@ class MusicPlayerService:
sound = current_playlist_sound.sound
if sound:
# Get the service URL from the associated stream
service_url = None
if sound.streams:
# Get the first stream's URL if available
service_url = sound.streams[0].url
return {
"id": sound.id,
"title": sound.name,
@@ -511,6 +531,8 @@ class MusicPlayerService:
if sound.thumbnail
else None
),
"file_url": self._build_stream_url(sound.type, sound.filename),
"service_url": service_url,
"type": sound.type,
}
return None
@@ -535,6 +557,12 @@ class MusicPlayerService:
):
sound = playlist_sound.sound
if sound:
# Get the service URL from the associated stream
service_url = None
if sound.streams:
# Get the first stream's URL if available
service_url = sound.streams[0].url
tracks.append(
{
"id": sound.id,
@@ -548,6 +576,8 @@ class MusicPlayerService:
if sound.thumbnail
else None
),
"file_url": self._build_stream_url(sound.type, sound.filename),
"service_url": service_url,
"type": sound.type,
}
)