Spaces:
Runtime error
Runtime error
nifty-coder commited on
Commit ·
1b9b8c1
1
Parent(s): 888e323
Fixed a sidebar playback issue
Browse files- __pycache__/main.cpython-314.pyc +0 -0
- main.py +66 -1
__pycache__/main.cpython-314.pyc
CHANGED
|
Binary files a/__pycache__/main.cpython-314.pyc and b/__pycache__/main.cpython-314.pyc differ
|
|
|
main.py
CHANGED
|
@@ -851,11 +851,13 @@ async def get_presigned_url(
|
|
| 851 |
|
| 852 |
try:
|
| 853 |
# Generate presigned URL (1 hour expiry)
|
|
|
|
| 854 |
presigned_url = s3_client.generate_presigned_url(
|
| 855 |
'get_object',
|
| 856 |
Params={
|
| 857 |
'Bucket': R2_BUCKET_NAME,
|
| 858 |
-
'Key': found_key
|
|
|
|
| 859 |
},
|
| 860 |
ExpiresIn=3600 # 1 hour
|
| 861 |
)
|
|
@@ -926,6 +928,69 @@ async def list_stems(
|
|
| 926 |
logger.error(f"Failed to list stems: {e}")
|
| 927 |
raise HTTPException(status_code=500, detail="Failed to list stems")
|
| 928 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 929 |
|
| 930 |
@app.get("/my-songs")
|
| 931 |
async def list_my_songs(
|
|
|
|
| 851 |
|
| 852 |
try:
|
| 853 |
# Generate presigned URL (1 hour expiry)
|
| 854 |
+
# Use ResponseContentDisposition to force a direct download when the URL is accessed
|
| 855 |
presigned_url = s3_client.generate_presigned_url(
|
| 856 |
'get_object',
|
| 857 |
Params={
|
| 858 |
'Bucket': R2_BUCKET_NAME,
|
| 859 |
+
'Key': found_key,
|
| 860 |
+
'ResponseContentDisposition': f'attachment; filename="{request.filename}"'
|
| 861 |
},
|
| 862 |
ExpiresIn=3600 # 1 hour
|
| 863 |
)
|
|
|
|
| 928 |
logger.error(f"Failed to list stems: {e}")
|
| 929 |
raise HTTPException(status_code=500, detail="Failed to list stems")
|
| 930 |
|
| 931 |
+
@app.get("/songs/{song_id}/zip")
|
| 932 |
+
async def get_song_zip(
|
| 933 |
+
song_id: str,
|
| 934 |
+
background_tasks: BackgroundTasks,
|
| 935 |
+
authorization: str = Header(...)
|
| 936 |
+
):
|
| 937 |
+
"""
|
| 938 |
+
Download all stems for a song as a single ZIP archive.
|
| 939 |
+
Stems are fetched from R2 and zipped on the fly.
|
| 940 |
+
"""
|
| 941 |
+
if not R2_ENABLED:
|
| 942 |
+
raise HTTPException(status_code=503, detail="R2 storage not configured")
|
| 943 |
+
|
| 944 |
+
# Verify auth
|
| 945 |
+
user = get_authenticated_user(authorization)
|
| 946 |
+
|
| 947 |
+
# 1. Find the stems
|
| 948 |
+
prefixes = get_user_storage_prefixes(user)
|
| 949 |
+
found_keys = []
|
| 950 |
+
for user_prefix in prefixes:
|
| 951 |
+
prefix = f"{user_prefix}{song_id}/"
|
| 952 |
+
try:
|
| 953 |
+
response = s3_client.list_objects_v2(Bucket=R2_BUCKET_NAME, Prefix=prefix)
|
| 954 |
+
if 'Contents' in response:
|
| 955 |
+
for obj in response['Contents']:
|
| 956 |
+
if obj['Key'].endswith('.mp3'):
|
| 957 |
+
found_keys.append(obj['Key'])
|
| 958 |
+
if found_keys:
|
| 959 |
+
break
|
| 960 |
+
except Exception as e:
|
| 961 |
+
logger.error(f"Error listing objects for ZIP: {e}")
|
| 962 |
+
continue
|
| 963 |
+
|
| 964 |
+
if not found_keys:
|
| 965 |
+
raise HTTPException(status_code=404, detail="No stems found for this song")
|
| 966 |
+
|
| 967 |
+
# 2. Create ZIP
|
| 968 |
+
temp_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
|
| 969 |
+
try:
|
| 970 |
+
with zipfile.ZipFile(temp_zip.name, 'w', zipfile.ZIP_DEFLATED) as zf:
|
| 971 |
+
for key in found_keys:
|
| 972 |
+
filename = os.path.basename(key)
|
| 973 |
+
try:
|
| 974 |
+
obj = s3_client.get_object(Bucket=R2_BUCKET_NAME, Key=key)
|
| 975 |
+
zf.writestr(filename, obj['Body'].read())
|
| 976 |
+
except Exception as e:
|
| 977 |
+
logger.error(f"Failed to add {key} to ZIP: {e}")
|
| 978 |
+
continue
|
| 979 |
+
|
| 980 |
+
# Add cleanup task
|
| 981 |
+
background_tasks.add_task(os.remove, temp_zip.name)
|
| 982 |
+
|
| 983 |
+
return FileResponse(
|
| 984 |
+
temp_zip.name,
|
| 985 |
+
filename=f"stems_{song_id[:8]}.zip",
|
| 986 |
+
media_type="application/zip"
|
| 987 |
+
)
|
| 988 |
+
except Exception as e:
|
| 989 |
+
if os.path.exists(temp_zip.name):
|
| 990 |
+
os.remove(temp_zip.name)
|
| 991 |
+
logger.error(f"Failed to create ZIP archive: {e}")
|
| 992 |
+
raise HTTPException(status_code=500, detail="Failed to create ZIP archive")
|
| 993 |
+
|
| 994 |
|
| 995 |
@app.get("/my-songs")
|
| 996 |
async def list_my_songs(
|