CODEX - More changes to the binary files, listing and storing

This commit is contained in:
2026-02-23 13:58:40 +02:00
parent d390bdac0d
commit 04b2a0bcb8
5 changed files with 154 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
from fastapi import APIRouter, Depends, UploadFile, File, Query, HTTPException
from fastapi import APIRouter, Depends, UploadFile, File, Query, HTTPException, Response
from typing import Optional
from auth.models import TokenPayload
from auth.dependencies import require_permission
@@ -99,7 +99,14 @@ async def upload_file(
if file_type == "binary":
content_type = "application/octet-stream"
url = service.upload_file(melody_id, contents, file.filename, content_type)
url = service.upload_file_for_melody(
melody_id=melody_id,
melody_uid=melody.uid,
melody_pid=melody.pid,
file_bytes=contents,
filename=file.filename,
content_type=content_type,
)
# Update the melody document with the file URL
if file_type == "preview":
@@ -125,8 +132,8 @@ async def delete_file(
if file_type not in ("binary", "preview"):
raise HTTPException(status_code=400, detail="file_type must be 'binary' or 'preview'")
await service.get_melody(melody_id)
service.delete_file(melody_id, file_type)
melody = await service.get_melody(melody_id)
service.delete_file(melody_id, file_type, melody.uid)
@router.get("/{melody_id}/files")
@@ -135,5 +142,18 @@ async def get_files(
_user: TokenPayload = Depends(require_permission("melodies", "view")),
):
"""Get storage file URLs for a melody."""
await service.get_melody(melody_id)
return service.get_storage_files(melody_id)
melody = await service.get_melody(melody_id)
return service.get_storage_files(melody_id, melody.uid)
@router.get("/{melody_id}/download/binary")
async def download_binary_file(
melody_id: str,
_user: TokenPayload = Depends(require_permission("melodies", "view")),
):
"""Download current melody binary with a PID-based filename."""
melody = await service.get_melody(melody_id)
file_bytes, content_type = service.get_binary_file_bytes(melody_id, melody.uid)
filename = f"{(melody.pid or 'binary')}.bsm"
headers = {"Content-Disposition": f'attachment; filename="{filename}"'}
return Response(content=file_bytes, media_type=content_type, headers=headers)