utils.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. import re
  3. from base64 import b64decode
  4. import httpx
  5. _timeout = int(os.getenv("REQUEST_TIMEOUT", "3"))
  6. _file_exts = (".png", ".jpg", ".jpeg", ".webp", ".gif", ".pdf")
  7. _data_uri_regex = re.compile(r"^data:[^;,]+;base64,")
  8. def load_resource(uri: str) -> bytes:
  9. if uri.startswith("http://") or uri.startswith("https://"):
  10. response = httpx.get(uri, timeout=_timeout)
  11. return response.content
  12. if uri.startswith("file://"):
  13. with open(uri[len("file://") :], "rb") as file:
  14. return file.read()
  15. if uri.lower().endswith(_file_exts):
  16. with open(uri, "rb") as file:
  17. return file.read()
  18. if re.match(_data_uri_regex, uri):
  19. return b64decode(uri.split(",")[1])
  20. return b64decode(uri)
  21. async def aio_load_resource(uri: str) -> bytes:
  22. if uri.startswith("http://") or uri.startswith("https://"):
  23. async with httpx.AsyncClient(timeout=_timeout) as client:
  24. response = await client.get(uri)
  25. return response.content
  26. if uri.startswith("file://"):
  27. with open(uri[len("file://") :], "rb") as file:
  28. return file.read()
  29. if uri.lower().endswith(_file_exts):
  30. with open(uri, "rb") as file:
  31. return file.read()
  32. if re.match(_data_uri_regex, uri):
  33. return b64decode(uri.split(",")[1])
  34. return b64decode(uri)