Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
thumbnailing: Move resizing functions into zerver.lib.thumbnail.
Alex Vandiver0153d6dbcdc1
diff --git a/tools/setup/generate_integration_bots_avatars.py b/tools/setup/generate_integration_bots_avatars.pyindex 39d6fb6af5b..9f1600e6c11 100755--- a/tools/setup/generate_integration_bots_avatars.py+++ b/tools/setup/generate_integration_bots_avatars.py@@ -21,7 +21,7 @@ from PIL import Imagefrom zerver.lib.integrations import INTEGRATIONSfrom zerver.lib.storage import static_path-from zerver.lib.upload.base import DEFAULT_AVATAR_SIZE, resize_avatar+from zerver.lib.thumbnail import DEFAULT_AVATAR_SIZE, resize_avatardef create_square_image(png: bytes) -> bytes:diff --git a/zerver/data_import/slack.py b/zerver/data_import/slack.pyindex b6545f8a567..ab9d3147459 100644--- a/zerver/data_import/slack.py+++ b/zerver/data_import/slack.py@@ -47,7 +47,8 @@ from zerver.data_import.slack_message_conversion import (from zerver.lib.emoji import codepoint_to_namefrom zerver.lib.export import MESSAGE_BATCH_CHUNK_SIZEfrom zerver.lib.storage import static_path-from zerver.lib.upload.base import resize_logo, sanitize_name+from zerver.lib.thumbnail import resize_logo+from zerver.lib.upload.base import sanitize_namefrom zerver.models import (CustomProfileField,CustomProfileFieldValue,diff --git a/zerver/lib/avatar.py b/zerver/lib/avatar.pyindex d4a50e85397..4e7882253b4 100644--- a/zerver/lib/avatar.py+++ b/zerver/lib/avatar.py@@ -9,8 +9,8 @@ from zerver.lib.avatar_hash import (user_avatar_content_hash,user_avatar_path_from_ids,)+from zerver.lib.thumbnail import MEDIUM_AVATAR_SIZEfrom zerver.lib.upload import get_avatar_url-from zerver.lib.upload.base import MEDIUM_AVATAR_SIZEfrom zerver.lib.url_encoding import append_url_query_stringfrom zerver.models import UserProfilediff --git a/zerver/lib/import_realm.py b/zerver/lib/import_realm.pyindex 155c8c9e63c..8481c87a077 100644--- a/zerver/lib/import_realm.py+++ b/zerver/lib/import_realm.py@@ -32,9 +32,10 @@ from zerver.lib.push_notifications import sends_notifications_directlyfrom zerver.lib.remote_server import maybe_enqueue_audit_log_uploadfrom zerver.lib.server_initialization import create_internal_realm, server_initializedfrom zerver.lib.streams import render_stream_description+from zerver.lib.thumbnail import BadImageErrorfrom zerver.lib.timestamp import datetime_to_timestampfrom zerver.lib.upload import upload_backend-from zerver.lib.upload.base import BadImageError, sanitize_name+from zerver.lib.upload.base import sanitize_namefrom zerver.lib.upload.s3 import get_bucketfrom zerver.lib.user_counts import realm_user_count_by_rolefrom zerver.lib.user_groups import create_system_user_groups_for_realmdiff --git a/zerver/lib/thumbnail.py b/zerver/lib/thumbnail.pyindex 2822d24c207..ba2afba8ff7 100644--- a/zerver/lib/thumbnail.py+++ b/zerver/lib/thumbnail.py@@ -1,8 +1,28 @@+import io+from typing import Optional, Tuplefrom urllib.parse import urljoinfrom django.utils.http import url_has_allowed_host_and_scheme+from django.utils.translation import gettext as _+from PIL import GifImagePlugin, Image, ImageOps, PngImagePlugin+from PIL.Image import DecompressionBombErrorfrom zerver.lib.camo import get_camo_url+from zerver.lib.exceptions import ErrorCode, JsonableError++DEFAULT_AVATAR_SIZE = 100+MEDIUM_AVATAR_SIZE = 500+DEFAULT_EMOJI_SIZE = 64++# These sizes were selected based on looking at the maximum common+# sizes in a library of animated custom emoji, balanced against the+# network cost of very large emoji images.+MAX_EMOJI_GIF_SIZE = 128+MAX_EMOJI_GIF_FILE_SIZE_BYTES = 128 * 1024 * 1024 # 128 kb+++class BadImageError(JsonableError):+ code = ErrorCode.BAD_IMAGEdef user_uploads_or_external(url: str) -> bool:@@ -17,3 +37,124 @@ def generate_thumbnail_url(path: str, size: str = "0x0") -> str:if url_has_allowed_host_and_scheme(path, allowed_hosts=None):return pathreturn get_camo_url(path)+++def resize_avatar(image_data: bytes, size: int = DEFAULT_AVATAR_SIZE) -> bytes:+ try:+ im = Image.open(io.BytesIO(image_data))+ im = ImageOps.exif_transpose(im)+ im = ImageOps.fit(im, (size, size), Image.Resampling.LANCZOS)+ except OSError:+ raise BadImageError(_("Could not decode image; did you upload an image file?"))+ except DecompressionBombError:+ raise BadImageError(_("Image size exceeds limit."))+ out = io.BytesIO()+ if im.mode == "CMYK":+ im = im.convert("RGB")+ im.save(out, format="png")+ return out.getvalue()+++def resize_logo(image_data: bytes) -> bytes:+ try:+ im = Image.open(io.BytesIO(image_data))+ im = ImageOps.exif_transpose(im)+ im.thumbnail((8 * DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE), Image.Resampling.LANCZOS)+ except OSError:+ raise BadImageError(_("Could not decode image; did you upload an image file?"))+ except DecompressionBombError:+ raise BadImageError(_("Image size exceeds limit."))+ out = io.BytesIO()+ if im.mode == "CMYK":+ im = im.convert("RGB")+ im.save(out, format="png")+ return out.getvalue()+++def resize_animated(im: Image.Image, size: int = DEFAULT_EMOJI_SIZE) -> bytes:+ assert im.n_frames > 1+ frames = []+ duration_info = []+ disposals = []+ # If 'loop' info is not set then loop for infinite number of times.+ loop = im.info.get("loop", 0)+ for frame_num in range(im.n_frames):+ im.seek(frame_num)+ new_frame = im.copy()+ new_frame.paste(im, (0, 0), im.convert("RGBA"))+ new_frame = ImageOps.pad(new_frame, (size, size), Image.Resampling.LANCZOS)+ frames.append(new_frame)+ if im.info.get("duration") is None: # nocoverage+ raise BadImageError(_("Corrupt animated image."))+ duration_info.append(im.info["duration"])+ if isinstance(im, GifImagePlugin.GifImageFile):+ disposals.append(+ im.disposal_method # type: ignore[attr-defined] # private member missing from stubs+ )+ elif isinstance(im, PngImagePlugin.PngImageFile):+ disposals.append(im.info.get("disposal", PngImagePlugin.Disposal.OP_NONE))+ else: # nocoverage+ raise BadImageError(_("Unknown animated image format."))+ out = io.BytesIO()+ frames[0].save(+ out,+ save_all=True,+ optimize=False,+ format=im.format,+ append_images=frames[1:],+ duration=duration_info,+ disposal=disposals,+ loop=loop,+ )++ return out.getvalue()+++def resize_emoji(+ image_data: bytes, size: int = DEFAULT_EMOJI_SIZE+) -> Tuple[bytes, bool, Optional[bytes]]:+ # This function returns three values:+ # 1) Emoji image data.+ # 2) If emoji is gif i.e. animated.+ # 3) If is animated then return still image data i.e. first frame of gif.++ try:+ im = Image.open(io.BytesIO(image_data))+ image_format = im.format+ if getattr(im, "n_frames", 1) > 1:+ # There are a number of bugs in Pillow which cause results+ # in resized images being broken. To work around this we+ # only resize under certain conditions to minimize the+ # chance of creating ugly images.+ should_resize = (+ im.size[0] != im.size[1] # not square+ or im.size[0] > MAX_EMOJI_GIF_SIZE # dimensions too large+ or len(image_data) > MAX_EMOJI_GIF_FILE_SIZE_BYTES # filesize too large+ )++ # Generate a still image from the first frame. Since+ # we're converting the format to PNG anyway, we resize unconditionally.+ still_image = im.copy()+ still_image.seek(0)+ still_image = ImageOps.exif_transpose(still_image)+ still_image = ImageOps.fit(still_image, (size, size), Image.Resampling.LANCZOS)+ out = io.BytesIO()+ still_image.save(out, format="PNG")+ still_image_data = out.getvalue()++ if should_resize:+ image_data = resize_animated(im, size)++ return image_data, True, still_image_data+ else:+ # Note that this is essentially duplicated in the+ # still_image code path, above.+ im = ImageOps.exif_transpose(im)+ im = ImageOps.fit(im, (size, size), Image.Resampling.LANCZOS)+ out = io.BytesIO()+ im.save(out, format=image_format)+ return out.getvalue(), False, None+ except OSError:+ raise BadImageError(_("Could not decode image; did you upload an image file?"))+ except DecompressionBombError:+ raise BadImageError(_("Image size exceeds limit."))diff --git a/zerver/lib/upload/base.py b/zerver/lib/upload/base.pyindex 84592d430c7..d5e76c1599d 100644--- a/zerver/lib/upload/base.py+++ b/zerver/lib/upload/base.py@@ -1,29 +1,12 @@-import ioimport osimport reimport unicodedatafrom datetime import datetimefrom typing import IO, Any, BinaryIO, Callable, Iterator, List, Optional, Tuple-from django.utils.translation import gettext as _-from PIL import GifImagePlugin, Image, ImageOps, PngImagePlugin-from PIL.Image import DecompressionBombError--from zerver.lib.exceptions import ErrorCode, JsonableErrorfrom zerver.models import Attachment, Realm, UserProfilefrom zerver.models.users import is_cross_realm_bot_email-DEFAULT_AVATAR_SIZE = 100-MEDIUM_AVATAR_SIZE = 500-DEFAULT_EMOJI_SIZE = 64--# These sizes were selected based on looking at the maximum common-# sizes in a library of animated custom emoji, balanced against the-# network cost of very large emoji images.-MAX_EMOJI_GIF_SIZE = 128-MAX_EMOJI_GIF_FILE_SIZE_BYTES = 128 * 1024 * 1024 # 128 kb--INLINE_MIME_TYPES = ["application/pdf","audio/aac",@@ -66,131 +49,6 @@ def sanitize_name(value: str) -> str:return value-class BadImageError(JsonableError):- code = ErrorCode.BAD_IMAGE---def resize_avatar(image_data: bytes, size: int = DEFAULT_AVATAR_SIZE) -> bytes:- try:- im = Image.open(io.BytesIO(image_data))- im = ImageOps.exif_transpose(im)- im = ImageOps.fit(im, (size, size), Image.Resampling.LANCZOS)- except OSError:- raise BadImageError(_("Could not decode image; did you upload an image file?"))- except DecompressionBombError:- raise BadImageError(_("Image size exceeds limit."))- out = io.BytesIO()- if im.mode == "CMYK":- im = im.convert("RGB")- im.save(out, format="png")- return out.getvalue()---def resize_logo(image_data: bytes) -> bytes:- try:- im = Image.open(io.BytesIO(image_data))- im = ImageOps.exif_transpose(im)- im.thumbnail((8 * DEFAULT_AVATAR_SIZE, DEFAULT_AVATAR_SIZE), Image.Resampling.LANCZOS)- except OSError:- raise BadImageError(_("Could not decode image; did you upload an image file?"))- except DecompressionBombError:- raise BadImageError(_("Image size exceeds limit."))- out = io.BytesIO()- if im.mode == "CMYK":- im = im.convert("RGB")- im.save(out, format="png")- return out.getvalue()---def resize_animated(im: Image.Image, size: int = DEFAULT_EMOJI_SIZE) -> bytes:- assert im.n_frames > 1- frames = []- duration_info = []- disposals = []- # If 'loop' info is not set then loop for infinite number of times.- loop = im.info.get("loop", 0)- for frame_num in range(im.n_frames):- im.seek(frame_num)- new_frame = im.copy()- new_frame.paste(im, (0, 0), im.convert("RGBA"))- new_frame = ImageOps.pad(new_frame, (size, size), Image.Resampling.LANCZOS)- frames.append(new_frame)- if im.info.get("duration") is None: # nocoverage- raise BadImageError(_("Corrupt animated image."))- duration_info.append(im.info["duration"])- if isinstance(im, GifImagePlugin.GifImageFile):- disposals.append(- im.disposal_method # type: ignore[attr-defined] # private member missing from stubs- )- elif isinstance(im, PngImagePlugin.PngImageFile):- disposals.append(im.info.get("disposal", PngImagePlugin.Disposal.OP_NONE))- else: # nocoverage- raise BadImageError(_("Unknown animated image format."))- out = io.BytesIO()- frames[0].save(- out,- save_all=True,- optimize=False,- format=im.format,- append_images=frames[1:],- duration=duration_info,- disposal=disposals,- loop=loop,- )-- return out.getvalue()---def resize_emoji(- image_data: bytes, size: int = DEFAULT_EMOJI_SIZE-) -> Tuple[bytes, bool, Optional[bytes]]:- # This function returns three values:- # 1) Emoji image data.- # 2) If emoji is gif i.e. animated.- # 3) If is animated then return still image data i.e. first frame of gif.-- try:- im = Image.open(io.BytesIO(image_data))- image_format = im.format- if getattr(im, "n_frames", 1) > 1:- # There are a number of bugs in Pillow which cause results- # in resized images being broken. To work around this we- # only resize under certain conditions to minimize the- # chance of creating ugly images.- should_resize = (- im.size[0] != im.size[1] # not square- or im.size[0] > MAX_EMOJI_GIF_SIZE # dimensions too large- or len(image_data) > MAX_EMOJI_GIF_FILE_SIZE_BYTES # filesize too large- )-- # Generate a still image from the first frame. Since- # we're converting the format to PNG anyway, we resize unconditionally.- still_image = im.copy()- still_image.seek(0)- still_image = ImageOps.exif_transpose(still_image)- still_image = ImageOps.fit(still_image, (size, size), Image.Resampling.LANCZOS)- out = io.BytesIO()- still_image.save(out, format="PNG")- still_image_data = out.getvalue()-- if should_resize:- image_data = resize_animated(im, size)-- return image_data, True, still_image_data- else:- # Note that this is essentially duplicated in the- # still_image code path, above.- im = ImageOps.exif_transpose(im)- im = ImageOps.fit(im, (size, size), Image.Resampling.LANCZOS)- out = io.BytesIO()- im.save(out, format=image_format)- return out.getvalue(), False, None- except OSError:- raise BadImageError(_("Could not decode image; did you upload an image file?"))- except DecompressionBombError:- raise BadImageError(_("Image size exceeds limit."))--class ZulipUploadBackend:# Message attachment uploadsdef get_public_upload_root_url(self) -> str:diff --git a/zerver/lib/upload/local.py b/zerver/lib/upload/local.pyindex 0a18ef912a8..9e8d0c59f50 100644--- a/zerver/lib/upload/local.py+++ b/zerver/lib/upload/local.py@@ -10,16 +10,9 @@ from django.conf import settingsfrom typing_extensions import overridefrom zerver.lib.avatar_hash import user_avatar_path+from zerver.lib.thumbnail import MEDIUM_AVATAR_SIZE, resize_avatar, resize_emoji, resize_logofrom zerver.lib.timestamp import timestamp_to_datetime-from zerver.lib.upload.base import (- MEDIUM_AVATAR_SIZE,- ZulipUploadBackend,- create_attachment,- resize_avatar,- resize_emoji,- resize_logo,- sanitize_name,-)+from zerver.lib.upload.base import ZulipUploadBackend, create_attachment, sanitize_namefrom zerver.lib.utils import assert_is_not_nonefrom zerver.models import Realm, RealmEmoji, UserProfilediff --git a/zerver/lib/upload/s3.py b/zerver/lib/upload/s3.pyindex 98ed7de301d..185dc4a270d 100644--- a/zerver/lib/upload/s3.py+++ b/zerver/lib/upload/s3.py@@ -14,14 +14,11 @@ from typing_extensions import overridefrom zerver.lib.avatar_hash import user_avatar_pathfrom zerver.lib.mime_types import guess_type+from zerver.lib.thumbnail import MEDIUM_AVATAR_SIZE, resize_avatar, resize_emoji, resize_logofrom zerver.lib.upload.base import (INLINE_MIME_TYPES,- MEDIUM_AVATAR_SIZE,ZulipUploadBackend,create_attachment,- resize_avatar,- resize_emoji,- resize_logo,sanitize_name,)from zerver.models import Realm, RealmEmoji, UserProfilediff --git a/zerver/tests/test_auth_backends.py b/zerver/tests/test_auth_backends.pyindex 050d72edcda..ffd753c4b02 100644--- a/zerver/tests/test_auth_backends.py+++ b/zerver/tests/test_auth_backends.py@@ -92,8 +92,8 @@ from zerver.lib.test_helpers import (read_test_image_file,use_s3_backend,)+from zerver.lib.thumbnail import DEFAULT_AVATAR_SIZE, MEDIUM_AVATAR_SIZE, resize_avatarfrom zerver.lib.types import Validator-from zerver.lib.upload.base import DEFAULT_AVATAR_SIZE, MEDIUM_AVATAR_SIZE, resize_avatarfrom zerver.lib.user_groups import is_user_in_groupfrom zerver.lib.users import get_all_api_keys, get_api_key, get_users_for_apifrom zerver.lib.utils import assert_is_not_nonediff --git a/zerver/tests/test_realm_emoji.py b/zerver/tests/test_realm_emoji.pyindex f266284fc16..01a7aab44b5 100644--- a/zerver/tests/test_realm_emoji.py+++ b/zerver/tests/test_realm_emoji.py@@ -8,7 +8,7 @@ from zerver.actions.users import do_change_user_rolefrom zerver.lib.exceptions import JsonableErrorfrom zerver.lib.test_classes import ZulipTestCasefrom zerver.lib.test_helpers import get_test_image_file-from zerver.lib.upload.base import BadImageError+from zerver.lib.thumbnail import BadImageErrorfrom zerver.models import Realm, RealmEmoji, UserProfilefrom zerver.models.realms import CommonPolicyEnum, get_realmdiff --git a/zerver/tests/test_transfer.py b/zerver/tests/test_transfer.pyindex e1bc8cf7092..bdf10e12be3 100644--- a/zerver/tests/test_transfer.py+++ b/zerver/tests/test_transfer.py@@ -13,6 +13,7 @@ from zerver.lib.test_helpers import (get_test_image_file,read_test_image_file,)+from zerver.lib.thumbnail import resize_emojifrom zerver.lib.transfer import (transfer_avatars_to_s3,transfer_emoji_to_s3,@@ -20,7 +21,6 @@ from zerver.lib.transfer import (transfer_uploads_to_s3,)from zerver.lib.upload import upload_message_attachment-from zerver.lib.upload.base import resize_emojifrom zerver.models import Attachment, RealmEmojidiff --git a/zerver/tests/test_upload.py b/zerver/tests/test_upload.pyindex df874d26f67..4b10ec341dd 100644--- a/zerver/tests/test_upload.py+++ b/zerver/tests/test_upload.py@@ -37,8 +37,9 @@ from zerver.lib.test_helpers import (ratelimit_rule,read_test_image_file,)+from zerver.lib.thumbnail import BadImageError, resize_emojifrom zerver.lib.upload import upload_message_attachment-from zerver.lib.upload.base import BadImageError, ZulipUploadBackend, resize_emoji, sanitize_name+from zerver.lib.upload.base import ZulipUploadBackend, sanitize_namefrom zerver.lib.upload.local import LocalUploadBackendfrom zerver.lib.upload.s3 import S3UploadBackendfrom zerver.lib.users import get_api_key@@ -1422,15 +1423,15 @@ class EmojiTest(UploadSerializeMixin, ZulipTestCase):animated_large_img_data = read_test_image_file(f"animated_large_img.{img_format}")# Test an image larger than max is resized- with patch("zerver.lib.upload.base.MAX_EMOJI_GIF_SIZE", 128):+ with patch("zerver.lib.thumbnail.MAX_EMOJI_GIF_SIZE", 128):test_resize()# Test an image file larger than max is resized- with patch("zerver.lib.upload.base.MAX_EMOJI_GIF_FILE_SIZE_BYTES", 3 * 1024 * 1024):+ with patch("zerver.lib.thumbnail.MAX_EMOJI_GIF_FILE_SIZE_BYTES", 3 * 1024 * 1024):test_resize()# Test an image smaller than max and smaller than file size max is not resized- with patch("zerver.lib.upload.base.MAX_EMOJI_GIF_SIZE", 512):+ with patch("zerver.lib.thumbnail.MAX_EMOJI_GIF_SIZE", 512):test_resize(size=256)# Test a non-animated GIF image which does need to be resizeddiff --git a/zerver/tests/test_upload_local.py b/zerver/tests/test_upload_local.pyindex 156524ddf54..39d027550c2 100644--- a/zerver/tests/test_upload_local.py+++ b/zerver/tests/test_upload_local.py@@ -10,6 +10,7 @@ import zerver.lib.uploadfrom zerver.lib.avatar_hash import user_avatar_pathfrom zerver.lib.test_classes import UploadSerializeMixin, ZulipTestCasefrom zerver.lib.test_helpers import get_test_image_file, read_test_image_file+from zerver.lib.thumbnail import DEFAULT_EMOJI_SIZE, MEDIUM_AVATAR_SIZE, resize_avatarfrom zerver.lib.upload import (all_message_attachments,delete_export_tarball,@@ -20,7 +21,6 @@ from zerver.lib.upload import (upload_export_tarball,upload_message_attachment,)-from zerver.lib.upload.base import DEFAULT_EMOJI_SIZE, MEDIUM_AVATAR_SIZE, resize_avatarfrom zerver.lib.upload.local import write_local_filefrom zerver.models import Attachment, RealmEmojifrom zerver.models.realms import get_realmdiff --git a/zerver/tests/test_upload_s3.py b/zerver/tests/test_upload_s3.pyindex 76e032a0a22..7474072e718 100644--- a/zerver/tests/test_upload_s3.py+++ b/zerver/tests/test_upload_s3.py@@ -20,6 +20,12 @@ from zerver.lib.test_helpers import (read_test_image_file,use_s3_backend,)+from zerver.lib.thumbnail import (+ DEFAULT_AVATAR_SIZE,+ DEFAULT_EMOJI_SIZE,+ MEDIUM_AVATAR_SIZE,+ resize_avatar,+)from zerver.lib.upload import (all_message_attachments,delete_export_tarball,@@ -29,12 +35,6 @@ from zerver.lib.upload import (upload_export_tarball,upload_message_attachment,)-from zerver.lib.upload.base import (- DEFAULT_AVATAR_SIZE,- DEFAULT_EMOJI_SIZE,- MEDIUM_AVATAR_SIZE,- resize_avatar,-)from zerver.lib.upload.s3 import S3UploadBackendfrom zerver.models import Attachment, RealmEmoji, UserProfilefrom zerver.models.realms import get_realm