| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import base64
- import pathlib
- import asyncio
- from .attachment import attachment
- from .attachment import attachment_proxy
- from .attachment_file import attachment_file
- from .exceptions import resources_directory_not_exists
- class attachments_manager:
- def __init__(
- self,
- resources: pathlib.Path,
- init_directory: bool = False
- ) -> None:
- if init_directory:
- resources.mkdir()
-
- if not resources.is_dir() or not resources.exists():
- raise resources_directory_not_exists(resources)
- self.__resources = resources
- @property
- def resources(self) -> pathlib.Path:
- return self.__resources
- async def __decode(self, content: str) -> bytes:
- return await asyncio.to_thread(
- base64.b64decode,
- content.encode("ascii")
- )
- async def upload(self, content: str, extension: str) -> attachment_file:
- decoded = await self.__decode(content)
- file_handler = attachment_file.create(self.resources, extension)
- return await file_handler.store(decoded)
- def restore(self, file: attachment) -> attachment_file:
- return attachment_file(file.resources_path, self.resources)
|