directory_image.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import pathlib
  2. from .image import image
  3. from .exception import directory_image_exception
  4. class directory_image:
  5. def __init__(self, target: pathlib.Path, thumbnail: int = 400) -> None:
  6. self.__target = target
  7. self.__thumbnail_size = thumbnail
  8. if not self.__target.is_dir():
  9. content = "Directory for image hosting: \""
  10. content = content + str(target) + "\"."
  11. raise directory_image_exception(content)
  12. @property
  13. def thumbnail_size(self) -> int:
  14. return self.__thumbnail_size
  15. @property
  16. def target(self) -> pathlib.Path:
  17. return self.__target
  18. def __name_full(self, image_id: int) -> str:
  19. return "full_" + str(image_id) + "_.png"
  20. def __name_thumbnail(self, image_id: int) -> str:
  21. return "thumbnail_" + str(image_id) + "_.webp"
  22. def __full_path(self, image_id: int) -> pathlib.Path:
  23. return self.target / pathlib.Path(self.__full_name(image_id))
  24. def __thumbnail_path(self, image_id: int) -> pathlib.Path:
  25. return self.target / pathlib.Path(self.__thumbnail_name(image_id))
  26. def store(self, coded: str, image_id: int) -> None:
  27. full = self.__full_path(image_id)
  28. thumbnail = self.__full_path(image_id)
  29. image(coded, self.thumbnail_size) \
  30. .save_full(full) \
  31. .save_thumbnail(thumbnail)