gcharanteja commited on
Commit ·
1d51b72
1
Parent(s): 6514091
minio fix forthe streaming
Browse files- main.py +58 -19
- minio_client.py +6 -4
main.py
CHANGED
|
@@ -242,17 +242,29 @@ async def create_song(
|
|
| 242 |
await track_analytics_event("song_created", {"song_id": song["id"], "title": title})
|
| 243 |
|
| 244 |
# Determine the public base URL for the hosted environment
|
| 245 |
-
#
|
| 246 |
public_base = os.getenv("PUBLIC_BASE_URL")
|
|
|
|
| 247 |
if not public_base:
|
| 248 |
-
#
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
public_base = f"
|
| 253 |
else:
|
| 254 |
-
#
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
response = {"status": "success", "song": song}
|
| 258 |
if minio_info:
|
|
@@ -295,11 +307,20 @@ async def stream_song(song_id: int):
|
|
| 295 |
NOTE: Redirecting to a MinIO presigned URL may point at an internal hostname (e.g. 127.0.0.1)
|
| 296 |
in hosted environments like Hugging Face Spaces, which browsers cannot reach.
|
| 297 |
"""
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
if not file_data:
|
| 300 |
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 301 |
|
| 302 |
-
song = get_song_by_id(song_id)
|
| 303 |
filename = f"{song.get('title', 'song')}_{song_id}.mp3" if song else f"song_{song_id}.mp3"
|
| 304 |
|
| 305 |
return StreamingResponse(
|
|
@@ -312,13 +333,22 @@ async def stream_song(song_id: int):
|
|
| 312 |
@app.get("/songs/{song_id}/download")
|
| 313 |
async def download_song(song_id: int):
|
| 314 |
"""Download an MP3 file from MinIO."""
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 318 |
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
filename = f"{song.get('title', 'song')}_{song_id}.mp3" if song else f"song_{song_id}.mp3"
|
| 321 |
-
|
| 322 |
return StreamingResponse(
|
| 323 |
io.BytesIO(file_data),
|
| 324 |
media_type="audio/mpeg",
|
|
@@ -360,14 +390,23 @@ async def upload_audio(request: Request, song_id: int, audio_file: UploadFile =
|
|
| 360 |
await invalidate_list_cache("all_songs")
|
| 361 |
|
| 362 |
# Determine the public base URL for the hosted environment
|
|
|
|
| 363 |
public_base = os.getenv("PUBLIC_BASE_URL")
|
| 364 |
if not public_base:
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
public_base = f"{forwarded_proto}://{forwarded_host}"
|
| 369 |
else:
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
|
| 372 |
return {
|
| 373 |
"status": "success",
|
|
|
|
| 242 |
await track_analytics_event("song_created", {"song_id": song["id"], "title": title})
|
| 243 |
|
| 244 |
# Determine the public base URL for the hosted environment
|
| 245 |
+
# Priority: 1) Env var, 2) HF Space detection, 3) Origin header, 4) Forwarded headers, 5) Fallback
|
| 246 |
public_base = os.getenv("PUBLIC_BASE_URL")
|
| 247 |
+
|
| 248 |
if not public_base:
|
| 249 |
+
# Check if running on Hugging Face Spaces
|
| 250 |
+
hf_space = os.getenv("SPACE_ID")
|
| 251 |
+
if hf_space:
|
| 252 |
+
# Extract space name from SPACE_ID (e.g., "username/space-name")
|
| 253 |
+
public_base = f"https://{hf_space.replace('/', '-')}.hf.space"
|
| 254 |
else:
|
| 255 |
+
# Try Origin header (usually present in browser requests)
|
| 256 |
+
origin = request.headers.get("origin")
|
| 257 |
+
if origin:
|
| 258 |
+
public_base = origin.rstrip("/")
|
| 259 |
+
else:
|
| 260 |
+
# Try to derive from request headers (works with proxies like HF Spaces)
|
| 261 |
+
forwarded_proto = request.headers.get("x-forwarded-proto")
|
| 262 |
+
forwarded_host = request.headers.get("x-forwarded-host")
|
| 263 |
+
if forwarded_proto and forwarded_host:
|
| 264 |
+
public_base = f"{forwarded_proto}://{forwarded_host}"
|
| 265 |
+
else:
|
| 266 |
+
# Fallback to request base URL
|
| 267 |
+
public_base = str(request.base_url).rstrip("/")
|
| 268 |
|
| 269 |
response = {"status": "success", "song": song}
|
| 270 |
if minio_info:
|
|
|
|
| 307 |
NOTE: Redirecting to a MinIO presigned URL may point at an internal hostname (e.g. 127.0.0.1)
|
| 308 |
in hosted environments like Hugging Face Spaces, which browsers cannot reach.
|
| 309 |
"""
|
| 310 |
+
# Get song info to find the audio file path
|
| 311 |
+
song = get_song_by_id(song_id)
|
| 312 |
+
if not song:
|
| 313 |
+
raise HTTPException(status_code=404, detail="Song not found")
|
| 314 |
+
|
| 315 |
+
# Get the stored audio file path from the database
|
| 316 |
+
object_name = song.get("audio_file_path")
|
| 317 |
+
if not object_name:
|
| 318 |
+
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 319 |
+
|
| 320 |
+
file_data = download_mp3_file(song_id, object_name=object_name)
|
| 321 |
if not file_data:
|
| 322 |
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 323 |
|
|
|
|
| 324 |
filename = f"{song.get('title', 'song')}_{song_id}.mp3" if song else f"song_{song_id}.mp3"
|
| 325 |
|
| 326 |
return StreamingResponse(
|
|
|
|
| 333 |
@app.get("/songs/{song_id}/download")
|
| 334 |
async def download_song(song_id: int):
|
| 335 |
"""Download an MP3 file from MinIO."""
|
| 336 |
+
# Get song info to find the audio file path
|
| 337 |
+
song = get_song_by_id(song_id)
|
| 338 |
+
if not song:
|
| 339 |
+
raise HTTPException(status_code=404, detail="Song not found")
|
| 340 |
+
|
| 341 |
+
# Get the stored audio file path from the database
|
| 342 |
+
object_name = song.get("audio_file_path")
|
| 343 |
+
if not object_name:
|
| 344 |
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 345 |
|
| 346 |
+
file_data = download_mp3_file(song_id, object_name=object_name)
|
| 347 |
+
if not file_data:
|
| 348 |
+
raise HTTPException(status_code=404, detail="Audio file not found")
|
| 349 |
+
|
| 350 |
filename = f"{song.get('title', 'song')}_{song_id}.mp3" if song else f"song_{song_id}.mp3"
|
| 351 |
+
|
| 352 |
return StreamingResponse(
|
| 353 |
io.BytesIO(file_data),
|
| 354 |
media_type="audio/mpeg",
|
|
|
|
| 390 |
await invalidate_list_cache("all_songs")
|
| 391 |
|
| 392 |
# Determine the public base URL for the hosted environment
|
| 393 |
+
# Priority: 1) Env var, 2) HF Space detection, 3) Origin header, 4) Forwarded headers, 5) Fallback
|
| 394 |
public_base = os.getenv("PUBLIC_BASE_URL")
|
| 395 |
if not public_base:
|
| 396 |
+
hf_space = os.getenv("SPACE_ID")
|
| 397 |
+
if hf_space:
|
| 398 |
+
public_base = f"https://{hf_space.replace('/', '-')}.hf.space"
|
|
|
|
| 399 |
else:
|
| 400 |
+
origin = request.headers.get("origin")
|
| 401 |
+
if origin:
|
| 402 |
+
public_base = origin.rstrip("/")
|
| 403 |
+
else:
|
| 404 |
+
forwarded_proto = request.headers.get("x-forwarded-proto")
|
| 405 |
+
forwarded_host = request.headers.get("x-forwarded-host")
|
| 406 |
+
if forwarded_proto and forwarded_host:
|
| 407 |
+
public_base = f"{forwarded_proto}://{forwarded_host}"
|
| 408 |
+
else:
|
| 409 |
+
public_base = str(request.base_url).rstrip("/")
|
| 410 |
|
| 411 |
return {
|
| 412 |
"status": "success",
|
minio_client.py
CHANGED
|
@@ -98,14 +98,16 @@ def upload_mp3_file(
|
|
| 98 |
return None
|
| 99 |
|
| 100 |
|
| 101 |
-
def download_mp3_file(song_id: int, filename: str = None) -> Optional[bytes]:
|
| 102 |
"""Download an MP3 file from MinIO."""
|
| 103 |
client = get_minio_client()
|
| 104 |
if client is None:
|
| 105 |
return None
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
| 109 |
try:
|
| 110 |
response = client.get_object(MINIO_BUCKET, object_name)
|
| 111 |
return response.read()
|
|
|
|
| 98 |
return None
|
| 99 |
|
| 100 |
|
| 101 |
+
def download_mp3_file(song_id: int, filename: str = None, object_name: str = None) -> Optional[bytes]:
|
| 102 |
"""Download an MP3 file from MinIO."""
|
| 103 |
client = get_minio_client()
|
| 104 |
if client is None:
|
| 105 |
return None
|
| 106 |
+
|
| 107 |
+
# Use provided object_name, or construct it from song_id and filename
|
| 108 |
+
if object_name is None:
|
| 109 |
+
object_name = f"songs/{song_id}/{filename or f'song_{song_id}.mp3'}"
|
| 110 |
+
|
| 111 |
try:
|
| 112 |
response = client.get_object(MINIO_BUCKET, object_name)
|
| 113 |
return response.read()
|