| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import pathlib
- import aiofiles
- from .aleatory_file_name import aleatory_file_name
- class attachment_file:
- def __init__(
- self,
- extension: str,
- root: pathlib.Path
- ) -> None:
- self.__root = root
- self.__name = self.__get_name(extension)
- async def load(self) -> bytes:
- async with aiofiles.open(self.path, "rb") as handler:
- return await handler.read()
- async def store(self, content: bytes) -> object:
- async with aiofiles.open(self.path, "wb") as handler:
- await handler.write(content)
-
- return self
- def __get_name(self, extension: str) -> str:
- while True:
- new_name = aleatory_file_name(extension)
- new_path = self.root / pathlib.Path(new_name)
- if not new_path.is_file() and not new_path.exists():
- return new_name
-
- def exists(self) -> bool:
- return self.path.exists() and self.path.is_file()
-
- @property
- def name(self) -> str:
- return self.__name
- @property
- def root(self) -> pathlib.Path:
- return self.__root
- @property
- def path(self) -> pathlib.Path:
- return self.root / pathlib.Path(self.name)
-
-
|