2024-06-12 20:19:15 +02:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from contextlib import contextmanager
|
|
|
|
from typing import Iterator, Optional, Tuple
|
2019-12-20 02:58:53 +01:00
|
|
|
from urllib.parse import urljoin
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2024-06-12 20:19:15 +02:00
|
|
|
import pyvips
|
2021-04-16 00:59:20 +02:00
|
|
|
from django.utils.http import url_has_allowed_host_and_scheme
|
2024-06-12 16:56:41 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2018-03-08 09:37:09 +01:00
|
|
|
|
|
|
|
from zerver.lib.camo import get_camo_url
|
2024-06-12 16:56:41 +02:00
|
|
|
from zerver.lib.exceptions import ErrorCode, JsonableError
|
|
|
|
|
|
|
|
DEFAULT_AVATAR_SIZE = 100
|
|
|
|
MEDIUM_AVATAR_SIZE = 500
|
|
|
|
DEFAULT_EMOJI_SIZE = 64
|
|
|
|
|
2024-06-12 20:19:15 +02:00
|
|
|
# We refuse to deal with any image whose total pixelcount exceeds this.
|
|
|
|
IMAGE_BOMB_TOTAL_PIXELS = 90000000
|
|
|
|
|
|
|
|
# Reject emoji which, after resizing, have stills larger than this
|
|
|
|
MAX_EMOJI_GIF_FILE_SIZE_BYTES = 128 * 1024 # 128 kb
|
2024-06-12 16:56:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BadImageError(JsonableError):
|
|
|
|
code = ErrorCode.BAD_IMAGE
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-09-01 13:39:16 +02:00
|
|
|
def user_uploads_or_external(url: str) -> bool:
|
2021-04-16 00:59:20 +02:00
|
|
|
return not url_has_allowed_host_and_scheme(url, allowed_hosts=None) or url.startswith(
|
|
|
|
"/user_uploads/"
|
|
|
|
)
|
2018-09-01 13:39:16 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-08-14 02:21:00 +02:00
|
|
|
def generate_thumbnail_url(path: str, size: str = "0x0") -> str:
|
2019-12-12 01:28:29 +01:00
|
|
|
path = urljoin("/", path)
|
2018-03-08 09:37:09 +01:00
|
|
|
|
2021-05-07 00:38:24 +02:00
|
|
|
if url_has_allowed_host_and_scheme(path, allowed_hosts=None):
|
2019-12-20 02:58:53 +01:00
|
|
|
return path
|
2021-05-07 00:38:24 +02:00
|
|
|
return get_camo_url(path)
|
2024-06-12 16:56:41 +02:00
|
|
|
|
|
|
|
|
2024-06-12 20:19:15 +02:00
|
|
|
@contextmanager
|
|
|
|
def libvips_check_image(image_data: bytes) -> Iterator[pyvips.Image]:
|
|
|
|
# The primary goal of this is to verify that the image is valid,
|
|
|
|
# and raise BadImageError otherwise. The yielded `source_image`
|
|
|
|
# may be ignored, since calling `thumbnail_buffer` is faster than
|
|
|
|
# calling `thumbnail_image` on a pyvips.Image, since the latter
|
|
|
|
# cannot make use of shrink-on-load optimizations:
|
|
|
|
# https://www.libvips.org/API/current/libvips-resample.html#vips-thumbnail-image
|
2024-06-12 16:56:41 +02:00
|
|
|
try:
|
2024-06-12 20:19:15 +02:00
|
|
|
source_image = pyvips.Image.new_from_buffer(image_data, "")
|
|
|
|
except pyvips.Error:
|
2024-06-12 16:56:41 +02:00
|
|
|
raise BadImageError(_("Could not decode image; did you upload an image file?"))
|
|
|
|
|
2024-06-12 20:19:15 +02:00
|
|
|
if source_image.width * source_image.height > IMAGE_BOMB_TOTAL_PIXELS:
|
|
|
|
raise BadImageError(_("Image size exceeds limit."))
|
2024-06-12 16:56:41 +02:00
|
|
|
|
|
|
|
try:
|
2024-06-12 20:19:15 +02:00
|
|
|
yield source_image
|
|
|
|
except pyvips.Error as e: # nocoverage
|
|
|
|
logging.exception(e)
|
|
|
|
raise BadImageError(_("Bad image!"))
|
|
|
|
|
|
|
|
|
|
|
|
def resize_avatar(image_data: bytes, size: int = DEFAULT_AVATAR_SIZE) -> bytes:
|
|
|
|
# This will scale up, if necessary, and will scale the smallest
|
|
|
|
# dimension to fit. That is, a 1x1000 image will end up with the
|
|
|
|
# one middle pixel enlarged to fill the full square.
|
|
|
|
with libvips_check_image(image_data):
|
|
|
|
return pyvips.Image.thumbnail_buffer(
|
|
|
|
image_data,
|
|
|
|
size,
|
|
|
|
height=size,
|
|
|
|
crop=pyvips.Interesting.CENTRE,
|
|
|
|
).write_to_buffer(".png")
|
|
|
|
|
2024-06-12 16:56:41 +02:00
|
|
|
|
2024-06-12 20:19:15 +02:00
|
|
|
def resize_logo(image_data: bytes) -> bytes:
|
|
|
|
# This will only scale the image down, and will resize it to
|
|
|
|
# preserve aspect ratio and be contained within 8*AVATAR by AVATAR
|
|
|
|
# pixels; it does not add any padding to make it exactly that
|
|
|
|
# size. A 1000x10 pixel image will end up as 800x8; a 10x10 will
|
|
|
|
# end up 10x10.
|
|
|
|
with libvips_check_image(image_data):
|
|
|
|
return pyvips.Image.thumbnail_buffer(
|
|
|
|
image_data,
|
|
|
|
8 * DEFAULT_AVATAR_SIZE,
|
|
|
|
height=DEFAULT_AVATAR_SIZE,
|
|
|
|
size=pyvips.Size.DOWN,
|
|
|
|
).write_to_buffer(".png")
|
2024-06-12 16:56:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
def resize_emoji(
|
2024-06-12 20:19:15 +02:00
|
|
|
image_data: bytes, emoji_file_name: str, size: int = DEFAULT_EMOJI_SIZE
|
2024-06-13 06:11:30 +02:00
|
|
|
) -> Tuple[bytes, Optional[bytes]]:
|
2024-06-12 20:19:15 +02:00
|
|
|
if len(image_data) > MAX_EMOJI_GIF_FILE_SIZE_BYTES:
|
|
|
|
raise BadImageError(_("Image size exceeds limit."))
|
|
|
|
|
|
|
|
# Square brackets are used for providing options to libvips' save
|
|
|
|
# operation; these should have been filtered out earlier, so we
|
|
|
|
# assert none are found here, for safety.
|
|
|
|
write_file_ext = os.path.splitext(emoji_file_name)[1]
|
|
|
|
assert "[" not in write_file_ext
|
|
|
|
|
2024-06-13 06:11:30 +02:00
|
|
|
# This function returns two values:
|
2024-06-12 16:56:41 +02:00
|
|
|
# 1) Emoji image data.
|
2024-06-13 06:11:30 +02:00
|
|
|
# 2) If it is animated, the still image data i.e. first frame of gif.
|
2024-06-12 20:19:15 +02:00
|
|
|
with libvips_check_image(image_data) as source_image:
|
|
|
|
if source_image.get_n_pages() == 1:
|
|
|
|
return (
|
|
|
|
pyvips.Image.thumbnail_buffer(
|
|
|
|
image_data,
|
|
|
|
size,
|
|
|
|
height=size,
|
|
|
|
crop=pyvips.Interesting.CENTRE,
|
|
|
|
).write_to_buffer(write_file_ext),
|
|
|
|
None,
|
2024-06-12 16:56:41 +02:00
|
|
|
)
|
2024-06-12 20:19:15 +02:00
|
|
|
first_still = pyvips.Image.thumbnail_buffer(
|
|
|
|
image_data,
|
|
|
|
size,
|
|
|
|
height=size,
|
|
|
|
crop=pyvips.Interesting.CENTRE,
|
|
|
|
).write_to_buffer(".png")
|
|
|
|
|
|
|
|
animated = pyvips.Image.thumbnail_buffer(
|
|
|
|
image_data,
|
|
|
|
size,
|
|
|
|
height=size,
|
|
|
|
# This is passed to the loader, and means "load all
|
|
|
|
# frames", instead of the default of just the first
|
|
|
|
option_string="n=-1",
|
|
|
|
)
|
|
|
|
if animated.width != animated.get("page-height"):
|
|
|
|
# If the image is non-square, we have to iterate the
|
|
|
|
# frames to add padding to make it so
|
|
|
|
if not animated.hasalpha():
|
|
|
|
animated = animated.addalpha()
|
|
|
|
frames = [
|
|
|
|
frame.gravity(
|
|
|
|
pyvips.CompassDirection.CENTRE,
|
|
|
|
size,
|
|
|
|
size,
|
|
|
|
extend=pyvips.Extend.BACKGROUND,
|
|
|
|
background=[0, 0, 0, 0],
|
|
|
|
)
|
|
|
|
for frame in animated.pagesplit()
|
|
|
|
]
|
|
|
|
animated = frames[0].pagejoin(frames[1:])
|
2024-06-13 06:11:30 +02:00
|
|
|
return (animated.write_to_buffer(write_file_ext), first_still)
|