local_storage.py 842 B

12345678910111213141516171819202122232425262728293031
  1. import os
  2. from dataclasses import dataclass
  3. from .base_storage import Storage
  4. from utils import mkdir_if_not_exists
  5. @dataclass
  6. class LocalConfig:
  7. folder: str = ""
  8. save_to: str = "./"
  9. class LocalStorage(Storage):
  10. def __init__(self, config:LocalConfig):
  11. self.folder = config.folder
  12. self.save_to = config.save_to
  13. mkdir_if_not_exists(self.save_to)
  14. def get_cdn_url(self, key):
  15. full_path = os.path.join(self.save_to, self.folder, key)
  16. mkdir_if_not_exists(os.path.join(*full_path.split(os.path.sep)[0:-1]))
  17. return os.path.abspath(full_path)
  18. def exists(self, key):
  19. return os.path.isfile(self.get_cdn_url(key))
  20. def uploadf(self, file, key, **kwargs):
  21. path = self.get_cdn_url(key)
  22. with open(path, "wb") as outf:
  23. outf.write(file.read())