🐛 Fixed storage

This commit is contained in:
Mawoka
2023-06-18 10:09:15 +02:00
parent 5f35adeec7
commit cebc43fc3b
5 changed files with 15 additions and 14 deletions
-1
View File
@@ -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
+2 -7
View File
@@ -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)
+6 -2
View File
@@ -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
+1 -3
View File
@@ -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)
+6 -1
View File
@@ -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 {