Skip to content

Commit

Permalink
🐛 Fix S3 upload not working
Browse files Browse the repository at this point in the history
  • Loading branch information
mawoka-myblock committed Jun 15, 2024
1 parent fe12cf0 commit ac1072b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
11 changes: 9 additions & 2 deletions classquiz/routers/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ async def download_file_head(file_name: str) -> Response:


@router.post("/")
async def upload_file(file: UploadFile = File(), user: User = Depends(get_current_user)) -> PublicStorageItem:
async def upload_file(
request: Request, file: UploadFile = File(), user: User = Depends(get_current_user)
) -> PublicStorageItem:
if file.content_type not in ALLOWED_MIME_TYPES:
raise HTTPException(status_code=422, detail="Unsupported")
if user.storage_used > settings.free_storage_limit:
Expand All @@ -135,7 +137,12 @@ async def upload_file(file: UploadFile = File(), user: User = Depends(get_curren
deleted_at=None,
alt_text=None,
)
await storage.upload(file_name=file_id.hex, file_data=file.file, mime_type=file.content_type)
await storage.upload(
file_name=file_id.hex,
file_data=file.file,
mime_type=file.content_type,
size=request.headers.get("Content-Length"),
)
await file_obj.save()
await arq.enqueue_job("calculate_hash", file_id.hex)
return PublicStorageItem.from_db_model(file_obj)
Expand Down
6 changes: 4 additions & 2 deletions classquiz/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ def __init__(
def download(self, file_name: str) -> Generator | None:
return self.instance.download(file_name)

async def upload(self, file_name: str, file_data: BinaryIO, mime_type: str | None = None) -> None:
return await self.instance.upload(file=file_data, file_name=file_name, mime_type=mime_type)
async def upload(
self, file_name: str, file_data: BinaryIO, mime_type: str | None = None, size: int | None = None
) -> None:
return await self.instance.upload(file=file_data, file_name=file_name, mime_type=mime_type, size=size)

async def delete(self, file_names: [str]) -> None:
return await self.instance.delete(file_names=file_names)
Expand Down
8 changes: 7 additions & 1 deletion classquiz/storage/local_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ async def download(self, file_name: str) -> Generator | None:
yield None

# skipcq: PYL-W0613
async def upload(self, file_name: str, file: BinaryIO, mime_type: str | None = None) -> None:
async def upload(
self,
file_name: str,
file: BinaryIO,
size: int | None,
mime_type: str | None = None,
) -> None:
with open(file=os.path.join(self.base_path, file_name), mode="wb") as f:
copyfileobj(file, f)

Expand Down
5 changes: 2 additions & 3 deletions classquiz/storage/s3_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import hashlib
import hmac
import sys
from datetime import datetime, timedelta
from typing import Tuple, BinaryIO, Generator

Expand Down Expand Up @@ -109,9 +108,9 @@ def _generate_aws_signature_v4(self, method: str, path: str, expiry: int = None)
return headers, request_url

# skipcq: PYL-W0613
async def upload(self, file: BinaryIO, file_name: str, mime_type: str | None = "application/octet-stream") -> None:
async def upload(self, file: BinaryIO, file_name: str, size: int | None, mime_type: str | None = None) -> None:
headers, url = self._generate_aws_signature_v4(method="PUT", path=f"/{file_name}")
headers["Content-Length"] = sys.getsizeof(file)
headers["Content-Length"] = size
async with ClientSession() as session, session.put(url, headers=headers, data=file) as resp:
if resp.status == 200:
return None
Expand Down

0 comments on commit ac1072b

Please sign in to comment.