attachments_manager.py 844 B

123456789101112131415161718192021222324252627282930
  1. import pathlib
  2. import base64
  3. from .attachment_file import attachment_file
  4. from .exceptions import resources_directory_not_exists
  5. class attachments_manager:
  6. def __init__(
  7. self,
  8. resources: pathlib.Path,
  9. init_directory: bool = False
  10. ) -> None:
  11. if init_directory:
  12. resources.mkdir()
  13. if not resources.is_dir() or not resources.exists():
  14. raise resources_directory_not_exists(resources)
  15. self.__resources = resouces
  16. @property
  17. def resources(self) -> pathlib.Path:
  18. return self.__resources
  19. def uploaded(self, content: str, extension: str) -> attachment_file:
  20. content_bytes = content.encode("ascii")
  21. decoded = base64.b64decode(content_bytes)
  22. return attachment_file(decoded, extension, self.__resources)