diff --git a/classquiz/routers/storage.py b/classquiz/routers/storage.py index 8e58f4a..70dc7be 100644 --- a/classquiz/routers/storage.py +++ b/classquiz/routers/storage.py @@ -57,7 +57,6 @@ async def download_file(file_name: str): raise HTTPException(status_code=404, detail="File not found") if download is None: raise HTTPException(status_code=404, detail="File not found") - media_type = "image/*" if item is not None: media_type = item.mime_type diff --git a/classquiz/storage/__init__.py b/classquiz/storage/__init__.py index 88469b1..b0e3620 100644 --- a/classquiz/storage/__init__.py +++ b/classquiz/storage/__init__.py @@ -42,13 +42,8 @@ class Storage: else: raise NotImplementedError(f"Backend {backend} not implemented") - async def download(self, file_name: str) -> Generator | None: - """ - No support for s3 since it doesn't make sense relaying the traffic through this backend - :param file_name: - :return: - """ - yield self.instance.download(file_name) + 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) diff --git a/classquiz/storage/local_storage.py b/classquiz/storage/local_storage.py index b8ed5eb..5138fd8 100644 --- a/classquiz/storage/local_storage.py +++ b/classquiz/storage/local_storage.py @@ -24,7 +24,11 @@ class LocalStorage: async def download(self, file_name: str) -> Generator | None: try: async with aiofiles.open(file=os.path.join(self.base_path, file_name), mode="rb") as f: - yield f.read() + while True: + chunk = await f.read(8192) + if not chunk: + break + yield chunk except FileNotFoundError: yield None @@ -43,6 +47,6 @@ class LocalStorage: def size(self, file_name: str) -> int | None: try: - return os.stat(os.path.join(self.base_path, file_name)) + return os.stat(os.path.join(self.base_path, file_name)).st_size except FileNotFoundError: return None diff --git a/classquiz/worker/storage.py b/classquiz/worker/storage.py index 01493f7..5cde920 100644 --- a/classquiz/worker/storage.py +++ b/classquiz/worker/storage.py @@ -48,8 +48,7 @@ async def calculate_hash(ctx, file_id_as_str: str): print("Retry raised!") raise Retry(defer=ctx["job_try"] * 10) async for chunk in file_bytes: - async for c in chunk: - file.write(c) + file.write(chunk) try: if 0 < file_data.size < 20_970_000: # greater than 0 but smaller than 20mbytes file_data.thumbhash = image_to_thumbhash(file) @@ -63,7 +62,6 @@ async def calculate_hash(ctx, file_id_as_str: str): while chunk := file.read(6400): hash_obj.update(chunk) file_data.hash = hash_obj.digest() - print("Got hash!") await file_data.update() file.close() user: User | None = await User.objects.get_or_none(id=file_data.user.id) diff --git a/frontend/src/routes/edit/files/+page.ts b/frontend/src/routes/edit/files/+page.ts index 0dcaa6a..00d85a5 100644 --- a/frontend/src/routes/edit/files/+page.ts +++ b/frontend/src/routes/edit/files/+page.ts @@ -9,7 +9,12 @@ import type { PrivateImageData } from '$lib/quiz_types'; export const load = (async ({ fetch }) => { const res = await fetch('/api/v1/storage/list'); const res2 = await fetch('/api/v1/storage/limit'); - const json: PrivateImageData[] = await res.json(); + let json: PrivateImageData[]; + if (res.ok) { + json = await res.json(); + } else { + json = []; + } const storage_usage: { limit: number; limit_reached: boolean; used: number } = await res2.json(); return {