Files get deleted, if quiz gets deleted

This commit is contained in:
Mawoka
2022-04-02 20:51:17 +02:00
parent 0bad308c9c
commit e649b5ac46
7 changed files with 44 additions and 21 deletions
+14 -3
View File
@@ -1,17 +1,28 @@
import io
import os
import aiofiles
import aiofiles.os
class LocalStorage:
def __init__(self, base_path: str):
self.base_path = base_path
async def get_file(self, file_name: str) -> io.BytesIO:
async with aiofiles.open(file=os.path.join(self.base_path, file_name), mode='rb') as f:
return io.BytesIO(await f.read())
async def get_file(self, file_name: str) -> io.BytesIO | None:
try:
async with aiofiles.open(file=os.path.join(self.base_path, file_name), mode='rb') as f:
return io.BytesIO(await f.read())
except FileNotFoundError:
return None
async def write_file(self, file_name: str, data: bytes) -> None:
async with aiofiles.open(file=os.path.join(self.base_path, file_name), mode='wb') as f:
await f.write(data)
async def delete_file(self, file_names: [str]) -> None:
for i in file_names:
try:
await aiofiles.os.remove(os.path.join(self.base_path, i))
except FileNotFoundError:
pass
return None