From: Michael Tremer Date: Sun, 29 Jun 2025 17:53:53 +0000 (+0000) Subject: API: Wrap all upload operations into a database session X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=489bfd8096ab13108e5c7a2fb1a8a8d3afbab2eb;p=pbs.git API: Wrap all upload operations into a database session Signed-off-by: Michael Tremer --- diff --git a/src/api/uploads.py b/src/api/uploads.py index f7d0607f..e3c3299f 100644 --- a/src/api/uploads.py +++ b/src/api/uploads.py @@ -79,12 +79,13 @@ 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, - ) + async with backend.db: + 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: @@ -94,9 +95,6 @@ async def create(request: UploadRequest, 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("") @@ -115,9 +113,10 @@ async def put(request: fastapi.Request, upload: uploads.Upload = fastapi.Depends # Stream the payload try: - async with upload.stream() as f: - async for chunk in request.stream(): - await f.write(chunk) + async with backend.db: + async with upload.stream() as f: + async for chunk in request.stream(): + await f.write(chunk) # Raise an error if we could not import the file except ValueError as e: @@ -130,7 +129,8 @@ async def put(request: fastapi.Request, upload: uploads.Upload = fastapi.Depends @router.delete("/{id}") async def delete(upload: uploads.Upload = fastapi.Depends(get_upload)): # Delete the upload - await upload.delete() + async with backend.db(): + await upload.delete() # Send 204 return fastapi.Response(status_code=204)