import pathlib import aiofiles from .attachment import attachment from .attachment import attachment_proxy from .aleatory_file_name import aleatory_file_name class attachment_file: @classmethod def create(cls, directory: pathlib.Path, extension: str) -> object: while True: new_name = aleatory_file_name.path(extension) new_path = directory / new_name if not new_path.exists() and not new_path.is_file(): return cls(new_name, directory) def __init__( self, file_name: pathlib.Path, directory: pathlib.Path ) -> None: self.__file_name = file_name self.__directory = directory self.__file_path = directory / file_name @property def path(self) -> pathlib.Path: return self.__file_path @property def directory(self) -> pathlib.Path: return self.__directory @property def name(self) -> str: return str(self.__file_name) @property def relative_path(self) -> pathlib.Path: return self.__file_name async def store(self, content: bytes) -> attachment_proxy: async with aiofiles.open(self.path, "wb") as handler: await handler.write(content) return attachment_proxy.create(self.name) async def load(self) -> bytes: async with aiofiles.open(self.path, "rb") as handler: return await handler.read()