Spaces:
Runtime error
Runtime error
nifty-coder commited on
Commit ·
dd40bee
1
Parent(s): 652ff30
R2 further integration
Browse files
main.py
CHANGED
|
@@ -415,10 +415,8 @@ async def process_upload(
|
|
| 415 |
file_path = os.path.join(root, filename)
|
| 416 |
s3_key = f"uploads/{uid}/{song_id}/{filename}"
|
| 417 |
|
| 418 |
-
# Store original filename as metadata on
|
| 419 |
-
metadata = {}
|
| 420 |
-
if not uploaded_files: # First file
|
| 421 |
-
metadata['original-filename'] = file.filename
|
| 422 |
|
| 423 |
with open(file_path, 'rb') as f:
|
| 424 |
s3_client.upload_fileobj(
|
|
@@ -682,15 +680,17 @@ async def list_my_songs(
|
|
| 682 |
"last_modified": obj['LastModified'].isoformat()
|
| 683 |
})
|
| 684 |
|
| 685 |
-
# Get metadata from
|
| 686 |
-
if
|
| 687 |
try:
|
| 688 |
head_response = s3_client.head_object(
|
| 689 |
Bucket=R2_BUCKET_NAME,
|
| 690 |
Key=obj['Key']
|
| 691 |
)
|
|
|
|
| 692 |
metadata = head_response.get('Metadata', {})
|
| 693 |
-
|
|
|
|
| 694 |
except Exception as e:
|
| 695 |
logger.warning(f"Failed to get metadata for {obj['Key']}: {e}")
|
| 696 |
|
|
@@ -713,6 +713,91 @@ async def list_my_songs(
|
|
| 713 |
raise HTTPException(status_code=500, detail="Failed to list songs")
|
| 714 |
|
| 715 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
if __name__ == "__main__":
|
| 717 |
import uvicorn
|
| 718 |
# Default to 7860 for Hugging Face Spaces
|
|
|
|
| 415 |
file_path = os.path.join(root, filename)
|
| 416 |
s3_key = f"uploads/{uid}/{song_id}/{filename}"
|
| 417 |
|
| 418 |
+
# Store original filename as metadata on all files for robustness
|
| 419 |
+
metadata = {'original-filename': file.filename}
|
|
|
|
|
|
|
| 420 |
|
| 421 |
with open(file_path, 'rb') as f:
|
| 422 |
s3_client.upload_fileobj(
|
|
|
|
| 680 |
"last_modified": obj['LastModified'].isoformat()
|
| 681 |
})
|
| 682 |
|
| 683 |
+
# Get metadata from file to retrieve original filename
|
| 684 |
+
if not original_title:
|
| 685 |
try:
|
| 686 |
head_response = s3_client.head_object(
|
| 687 |
Bucket=R2_BUCKET_NAME,
|
| 688 |
Key=obj['Key']
|
| 689 |
)
|
| 690 |
+
# Use case-insensitive check for metadata keys
|
| 691 |
metadata = head_response.get('Metadata', {})
|
| 692 |
+
# Boto3/S3 often normalizes keys to lowercase
|
| 693 |
+
original_title = metadata.get('original-filename') or metadata.get('original-filename'.lower())
|
| 694 |
except Exception as e:
|
| 695 |
logger.warning(f"Failed to get metadata for {obj['Key']}: {e}")
|
| 696 |
|
|
|
|
| 713 |
raise HTTPException(status_code=500, detail="Failed to list songs")
|
| 714 |
|
| 715 |
|
| 716 |
+
@app.delete("/songs/{song_id}")
|
| 717 |
+
async def delete_song(
|
| 718 |
+
song_id: str,
|
| 719 |
+
authorization: str = Header(...)
|
| 720 |
+
):
|
| 721 |
+
"""
|
| 722 |
+
Delete all files for a given song ID from R2.
|
| 723 |
+
Requires Firebase authentication.
|
| 724 |
+
"""
|
| 725 |
+
if not R2_ENABLED:
|
| 726 |
+
raise HTTPException(status_code=503, detail="R2 storage not configured")
|
| 727 |
+
|
| 728 |
+
# Verify token and get UID
|
| 729 |
+
uid = verify_firebase_token(authorization)
|
| 730 |
+
|
| 731 |
+
# Construct prefix for all objects in this song directory
|
| 732 |
+
prefix = f"uploads/{uid}/{song_id}/"
|
| 733 |
+
|
| 734 |
+
try:
|
| 735 |
+
# Find all objects to delete
|
| 736 |
+
response = s3_client.list_objects_v2(
|
| 737 |
+
Bucket=R2_BUCKET_NAME,
|
| 738 |
+
Prefix=prefix
|
| 739 |
+
)
|
| 740 |
+
|
| 741 |
+
if 'Contents' in response:
|
| 742 |
+
delete_objects = {'Objects': [{'Key': obj['Key']} for obj in response['Contents']]}
|
| 743 |
+
s3_client.delete_objects(
|
| 744 |
+
Bucket=R2_BUCKET_NAME,
|
| 745 |
+
Delete=delete_objects
|
| 746 |
+
)
|
| 747 |
+
logger.info(f"Deleted {len(delete_objects['Objects'])} objects for song {song_id}")
|
| 748 |
+
|
| 749 |
+
return {"message": f"Song {song_id} deleted successfully"}
|
| 750 |
+
except Exception as e:
|
| 751 |
+
logger.error(f"Failed to delete song {song_id}: {e}")
|
| 752 |
+
raise HTTPException(status_code=500, detail="Failed to delete song")
|
| 753 |
+
|
| 754 |
+
|
| 755 |
+
@app.delete("/songs")
|
| 756 |
+
async def delete_all_songs(
|
| 757 |
+
authorization: str = Header(...)
|
| 758 |
+
):
|
| 759 |
+
"""
|
| 760 |
+
Delete all files for the authenticated user from R2.
|
| 761 |
+
"""
|
| 762 |
+
if not R2_ENABLED:
|
| 763 |
+
raise HTTPException(status_code=503, detail="R2 storage not configured")
|
| 764 |
+
|
| 765 |
+
# Verify token and get UID
|
| 766 |
+
uid = verify_firebase_token(authorization)
|
| 767 |
+
|
| 768 |
+
# Construct prefix for all objects for this user
|
| 769 |
+
prefix = f"uploads/{uid}/"
|
| 770 |
+
|
| 771 |
+
try:
|
| 772 |
+
# Paginating delete if there are many objects
|
| 773 |
+
deleted_count = 0
|
| 774 |
+
while True:
|
| 775 |
+
response = s3_client.list_objects_v2(
|
| 776 |
+
Bucket=R2_BUCKET_NAME,
|
| 777 |
+
Prefix=prefix
|
| 778 |
+
)
|
| 779 |
+
|
| 780 |
+
if 'Contents' not in response:
|
| 781 |
+
break
|
| 782 |
+
|
| 783 |
+
delete_objects = {'Objects': [{'Key': obj['Key']} for obj in response['Contents']]}
|
| 784 |
+
s3_client.delete_objects(
|
| 785 |
+
Bucket=R2_BUCKET_NAME,
|
| 786 |
+
Delete=delete_objects
|
| 787 |
+
)
|
| 788 |
+
deleted_count += len(delete_objects['Objects'])
|
| 789 |
+
|
| 790 |
+
# If there are more objects, they will be picked up in next iteration
|
| 791 |
+
if not response.get('IsTruncated'):
|
| 792 |
+
break
|
| 793 |
+
|
| 794 |
+
logger.info(f"Deleted all {deleted_count} objects for user {uid}")
|
| 795 |
+
return {"message": f"All {deleted_count} objects deleted successfully"}
|
| 796 |
+
except Exception as e:
|
| 797 |
+
logger.error(f"Failed to delete all songs for user {uid}: {e}")
|
| 798 |
+
raise HTTPException(status_code=500, detail="Failed to delete all songs")
|
| 799 |
+
|
| 800 |
+
|
| 801 |
if __name__ == "__main__":
|
| 802 |
import uvicorn
|
| 803 |
# Default to 7860 for Hugging Face Spaces
|