From af3142ac3adb0d1f31d160edcb6d076b293020b1 Mon Sep 17 00:00:00 2001 From: Michael Manfre Date: Thu, 15 Dec 2022 02:50:54 -0500 Subject: Basic Emoji suppport (#157) --- core/files.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'core/files.py') 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 -- cgit v1.2.3