summaryrefslogtreecommitdiffstats
path: root/core/files.py
diff options
context:
space:
mode:
authorMichael Manfre2022-12-15 02:50:54 -0500
committerGitHub2022-12-15 00:50:54 -0700
commitaf3142ac3adb0d1f31d160edcb6d076b293020b1 (patch)
tree73cfd5d447f6444602c2ff401399d567c673949b /core/files.py
parent69f1b3168ac3f29fc4bafba0418769248b10062a (diff)
downloadtakahe-af3142ac3adb0d1f31d160edcb6d076b293020b1.tar.gz
takahe-af3142ac3adb0d1f31d160edcb6d076b293020b1.tar.bz2
takahe-af3142ac3adb0d1f31d160edcb6d076b293020b1.zip
Basic Emoji suppport (#157)
Diffstat (limited to 'core/files.py')
-rw-r--r--core/files.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/core/files.py b/core/files.py
index a04cef9..3ef79aa 100644
--- a/core/files.py
+++ b/core/files.py
@@ -1,7 +1,10 @@
import io
import blurhash
+import httpx
+from django.conf import settings
from django.core.files import File
+from django.core.files.base import ContentFile
from PIL import Image, ImageOps
@@ -37,3 +40,28 @@ def blurhash_image(file) -> str:
Returns the blurhash for an image
"""
return blurhash.encode(file, 4, 4)
+
+
+async def get_remote_file(
+ url: str,
+ *,
+ timeout: float = settings.SETUP.REMOTE_TIMEOUT,
+ max_size: int | None = None,
+) -> tuple[File | None, str | None]:
+ """
+ Download a URL and return the File and content-type.
+ """
+ async with httpx.AsyncClient() as client:
+ async with client.stream("GET", url, timeout=timeout) as stream:
+ allow_download = max_size is None
+ if max_size:
+ try:
+ content_length = int(stream.headers["content-length"])
+ allow_download = content_length <= max_size
+ except TypeError:
+ pass
+ if allow_download:
+ file = ContentFile(await stream.aread(), name=url)
+ return file, stream.headers["content-type"]
+
+ return None, None