2022-12-02 01:46:49 +00:00
|
|
|
import io
|
|
|
|
|
|
|
|
import blurhash
|
2022-12-15 07:50:54 +00:00
|
|
|
import httpx
|
|
|
|
from django.conf import settings
|
2022-12-02 01:46:49 +00:00
|
|
|
from django.core.files import File
|
2022-12-15 07:50:54 +00:00
|
|
|
from django.core.files.base import ContentFile
|
2022-12-02 01:46:49 +00:00
|
|
|
from PIL import Image, ImageOps
|
|
|
|
|
|
|
|
|
2022-12-29 17:35:14 +00:00
|
|
|
class ImageFile(File):
|
|
|
|
image: Image
|
|
|
|
|
|
|
|
|
2022-12-04 17:46:41 +00:00
|
|
|
def resize_image(
|
|
|
|
image: File,
|
|
|
|
*,
|
|
|
|
size: tuple[int, int],
|
|
|
|
cover=True,
|
|
|
|
keep_format=False,
|
2022-12-29 17:35:14 +00:00
|
|
|
) -> ImageFile:
|
2022-12-02 01:46:49 +00:00
|
|
|
"""
|
|
|
|
Resizes an image to fit insize the given size (cropping one dimension
|
|
|
|
to fit if needed)
|
|
|
|
"""
|
|
|
|
with Image.open(image) as img:
|
2023-01-01 18:40:56 +00:00
|
|
|
try:
|
|
|
|
# Take any orientation EXIF data, apply it, and strip the
|
|
|
|
# orientation data from the new image.
|
|
|
|
img = ImageOps.exif_transpose(img)
|
|
|
|
except Exception: # noqa
|
|
|
|
# exif_transpose can crash with different errors depending on
|
|
|
|
# the EXIF keys. Just ignore them all, better to have a rotated
|
|
|
|
# image than no image.
|
|
|
|
pass
|
|
|
|
|
2022-12-04 17:46:41 +00:00
|
|
|
if cover:
|
2022-12-20 06:39:37 +00:00
|
|
|
resized_image = ImageOps.fit(img, size, method=Image.Resampling.BILINEAR)
|
2022-12-04 17:46:41 +00:00
|
|
|
else:
|
2022-12-20 06:39:37 +00:00
|
|
|
resized_image = img.copy()
|
|
|
|
resized_image.thumbnail(size, resample=Image.Resampling.BILINEAR)
|
2022-12-02 01:46:49 +00:00
|
|
|
new_image_bytes = io.BytesIO()
|
2022-12-04 17:46:41 +00:00
|
|
|
if keep_format:
|
2022-12-20 06:39:37 +00:00
|
|
|
resized_image.save(new_image_bytes, format=img.format)
|
2022-12-29 17:35:14 +00:00
|
|
|
file = ImageFile(new_image_bytes)
|
2022-12-04 17:46:41 +00:00
|
|
|
else:
|
2023-01-08 21:28:09 +00:00
|
|
|
resized_image.save(new_image_bytes, format="webp", save_all=True)
|
2022-12-29 17:35:14 +00:00
|
|
|
file = ImageFile(new_image_bytes, name="image.webp")
|
2022-12-04 17:46:41 +00:00
|
|
|
file.image = resized_image
|
|
|
|
return file
|
2022-12-02 01:46:49 +00:00
|
|
|
|
|
|
|
|
2022-12-11 19:37:28 +00:00
|
|
|
def blurhash_image(file) -> str:
|
2022-12-02 01:46:49 +00:00
|
|
|
"""
|
|
|
|
Returns the blurhash for an image
|
|
|
|
"""
|
2022-12-11 19:37:28 +00:00
|
|
|
return blurhash.encode(file, 4, 4)
|
2022-12-15 07:50:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
2022-12-27 23:50:39 +00:00
|
|
|
headers = {
|
|
|
|
"User-Agent": settings.TAKAHE_USER_AGENT,
|
|
|
|
}
|
|
|
|
|
|
|
|
async with httpx.AsyncClient(headers=headers) as client:
|
2022-12-15 07:50:54 +00:00
|
|
|
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
|
2022-12-17 19:16:37 +00:00
|
|
|
except (KeyError, TypeError):
|
2022-12-15 07:50:54 +00:00
|
|
|
pass
|
|
|
|
if allow_download:
|
|
|
|
file = ContentFile(await stream.aread(), name=url)
|
2022-12-24 05:17:43 +00:00
|
|
|
return file, stream.headers.get(
|
|
|
|
"content-type", "application/octet-stream"
|
|
|
|
)
|
2022-12-15 07:50:54 +00:00
|
|
|
|
|
|
|
return None, None
|