From: Michael Tremer Date: Tue, 17 Jun 2025 13:23:57 +0000 (+0000) Subject: API: Implement creating a new upload X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fafaa7702b27b2b098a42f8a705313db26532ca1;p=pbs.git API: Implement creating a new upload Signed-off-by: Michael Tremer --- diff --git a/src/api/uploads.py b/src/api/uploads.py index 329010e2..e501c58d 100644 --- a/src/api/uploads.py +++ b/src/api/uploads.py @@ -19,6 +19,7 @@ ############################################################################### import fastapi +import pydantic import uuid from . import app @@ -26,6 +27,7 @@ from . import auth from . import backend from .. import uploads +from .. import users # Create a new router for all upload endpoints router = fastapi.APIRouter( @@ -55,6 +57,47 @@ async def get_upload(id: uuid.UUID, return upload +class UploadRequest(pydantic.BaseModel): + # Filename + filename: str + + # Filesize + size: int + + # Hexdigest (BLAKE2B512) + hexdigest_blake2b512: str + + # Digest (BLAKE2B512) + @property + def digest_blake2b512(self): + return bytes.fromhex(self.hexdigest_blake2b512) + + +@router.post("") +async def create(request: UploadRequest, + current_principal = fastapi.Depends(auth.get_current_principal)) -> uploads.Upload: + # Create a new upload + try: + upload = await backend.uploads.create( + filename = request.filename, + size = request.size, + owner = current_principal, + digest_blake2b512 = request.digest_blake2b512, + ) + + # Send 422 Unprocessable Entity if quota has been exceeded + except users.QuotaExceededError as e: + raise fastapi.HTTPException(422, "Quota exceeded") + + # Send 400 Bad Request if any of the data could not be parsed + except ValueError as e: + raise fastapi.HTTPException(400) + + # XXX This needs to go and we need to create a proper database session + await backend.db.commit() + + return upload + @router.get("/{id}") async def get(upload: uploads.Upload = fastapi.Depends(get_upload)) -> uploads.Upload: return upload