Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Default to serving 4K images instead of originals (#11996)
Patryk Zawadzki1af1c3a368ec
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 31359b4996..e0210e0c15 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -37,7 +37,7 @@ All notable, unreleased changes to this project will be documented in this file.The discount amount might be 0.01 greater, and the total price might be 0.01 lower.E.g. if you had an order for $13 and applied a 12.5% discount, you would get $11.38 with a $1.62 discount, but now it will be calculated as $11.37 with $1.63 discount.-+- Media and image fields now default to returning 4K thumbnails instead of original uploads - #11996 by @patrys### GraphQL API- Add possibility to remove `stocks` and `channel listings` in `ProductVariantBulkUpdate` mutation.diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.pyindex 880ab3e48c..919f3529fe 100644--- a/saleor/graphql/account/tests/test_account.py+++ b/saleor/graphql/account/tests/test_account.py@@ -900,7 +900,7 @@ def test_query_user_avatar_with_size_thumbnail_url_returned()-def test_query_user_avatar_only_format_provided_original_image_returned(+def test_query_user_avatar_original_size_custom_format_provided_original_image_returned(staff_api_client, media_root, permission_manage_staff, site_settings):# given@@ -913,7 +913,7 @@ def test_query_user_avatar_only_format_provided_original_image_returned(format = ThumbnailFormatEnum.WEBP.nameid = graphene.Node.to_global_id("User", user.pk)- variables = {"id": id, "format": format}+ variables = {"id": id, "format": format, "size": 0}# whenresponse = staff_api_client.post_graphql(@@ -942,6 +942,8 @@ def test_query_user_avatar_no_size_value(id = graphene.Node.to_global_id("User", user.pk)variables = {"id": id}+ user_uuid = graphene.Node.to_global_id("User", user.uuid)+# whenresponse = staff_api_client.post_graphql(USER_AVATAR_QUERY, variables, permissions=[permission_manage_staff]@@ -952,7 +954,7 @@ def test_query_user_avatar_no_size_value(data = content["data"]["user"]assert (data["avatar"]["url"]- == f"http://{site_settings.site.domain}/media/user-avatars/{avatar_mock.name}"+ == f"http://{site_settings.site.domain}/thumbnail/{user_uuid}/4096/")@@ -5897,7 +5899,7 @@ USER_AVATAR_UPDATE_MUTATION = """mutation userAvatarUpdate($image: Upload!) {userAvatarUpdate(image: $image) {user {- avatar {+ avatar(size: 0) {url}}diff --git a/saleor/graphql/account/types.py b/saleor/graphql/account/types.pyindex 63d0c4f91a..2bbad55f4a 100644--- a/saleor/graphql/account/types.py+++ b/saleor/graphql/account/types.py@@ -488,7 +488,7 @@ class User(ModelObjectType[models.User]):if not root.avatar:return- if not size:+ if size == 0:return Image(url=root.avatar.url, alt=None)format = get_thumbnail_format(format)diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.pyindex 8015b028ff..144581f5da 100644--- a/saleor/graphql/core/types/common.py+++ b/saleor/graphql/core/types/common.py@@ -570,16 +570,16 @@ class TimePeriod(graphene.ObjectType):class ThumbnailField(graphene.Field):size = graphene.Int(description=(- "Size of the image. If not provided, the original image "- "will be returned."- )+ "Desired longest side the image in pixels. Defaults to 4096. "+ "Images are never cropped. "+ "Pass 0 to retrieve the original size (not recommended)."+ ),)format = ThumbnailFormatEnum(default_value="ORIGINAL",description=("The format of the image. When not provided, format of the original "- "image will be used. Must be provided together with the size value, "- "otherwise original image will be returned." + ADDED_IN_36 + PREVIEW_FEATURE+ "image will be used." + ADDED_IN_36 + PREVIEW_FEATURE),)diff --git a/saleor/graphql/product/tests/benchmark/test_product.py b/saleor/graphql/product/tests/benchmark/test_product.pyindex d926e1069b..28fe84ee2b 100644--- a/saleor/graphql/product/tests/benchmark/test_product.py+++ b/saleor/graphql/product/tests/benchmark/test_product.py@@ -771,7 +771,7 @@ def test_products_media_for_federation_query_count(__typename... on ProductMedia {id- url+ url(size: 0)}}}diff --git a/saleor/graphql/product/tests/mutations/test_product_media_delete.py b/saleor/graphql/product/tests/mutations/test_product_media_delete.pyindex b3a8137524..ebb2f8810f 100644--- a/saleor/graphql/product/tests/mutations/test_product_media_delete.py+++ b/saleor/graphql/product/tests/mutations/test_product_media_delete.py@@ -24,7 +24,7 @@ def test_product_media_delete(productMediaDelete(id: $id) {media {id- url+ url(size: 0)}}}diff --git a/saleor/graphql/product/tests/queries/test_product_query.py b/saleor/graphql/product/tests/queries/test_product_query.pyindex 03134f3ab2..4499bbefbf 100644--- a/saleor/graphql/product/tests/queries/test_product_query.py+++ b/saleor/graphql/product/tests/queries/test_product_query.py@@ -1854,7 +1854,7 @@ def test_query_product_media_by_id_with_size_thumbnail_url_returned()-def test_query_product_media_by_id_only_format_provided_original_image_returned(+def test_query_product_media_by_id_zero_size_custom_format_provided(user_api_client, product_with_image, channel_USD, site_settings):query = QUERY_PRODUCT_MEDIA_BY_ID@@ -1868,6 +1868,7 @@ def test_query_product_media_by_id_only_format_provided_original_image_returned("mediaId": media_id,"channel": channel_USD.slug,"format": format,+ "size": 0,}response = user_api_client.post_graphql(query, variables)@@ -1934,7 +1935,7 @@ def test_query_product_media_by_id_avif_format()-def test_query_product_media_by_id_no_size_value_original_image_returned(+def test_query_product_media_by_id_zero_size_value_original_image_returned(user_api_client, product_with_image, channel_USD, site_settings):query = QUERY_PRODUCT_MEDIA_BY_ID@@ -1946,6 +1947,7 @@ def test_query_product_media_by_id_no_size_value_original_image_returned("productId": graphene.Node.to_global_id("Product", product_with_image.pk),"mediaId": media_id,"channel": channel_USD.slug,+ "size": 0,}response = user_api_client.post_graphql(query, variables)@@ -2011,7 +2013,7 @@ def test_query_product_media_for_federation(__typename... on ProductMedia {id- url+ url(size: 0)}}}diff --git a/saleor/graphql/product/tests/test_category.py b/saleor/graphql/product/tests/test_category.pyindex fc01841a51..e2b2159ec2 100644--- a/saleor/graphql/product/tests/test_category.py+++ b/saleor/graphql/product/tests/test_category.py@@ -613,7 +613,7 @@ MUTATION_CATEGORY_UPDATE_MUTATION = """parent {id}- backgroundImage{+ backgroundImage(size: 0) {alturl}@@ -1479,7 +1479,7 @@ def test_category_image_query_with_size_thumbnail_url_returned()-def test_category_image_query_only_format_provided_original_image_returned(+def test_category_image_query_zero_size_custom_format_provided_original_image_returned(user_api_client, non_default_category, media_root, site_settings):# given@@ -1497,6 +1497,7 @@ def test_category_image_query_only_format_provided_original_image_returned(variables = {"id": category_id,"format": format,+ "size": 0,}# when@@ -1512,7 +1513,7 @@ def test_category_image_query_only_format_provided_original_image_returned(assert data["backgroundImage"]["url"] == expected_url-def test_category_image_query_no_size_value_original_image_returned(+def test_category_image_query_zero_size_value_original_image_returned(user_api_client, non_default_category, media_root, site_settings):# given@@ -1527,6 +1528,7 @@ def test_category_image_query_no_size_value_original_image_returned(category_id = graphene.Node.to_global_id("Category", category.pk)variables = {"id": category_id,+ "size": 0,}# whendiff --git a/saleor/graphql/product/tests/test_collection.py b/saleor/graphql/product/tests/test_collection.pyindex 9710950266..b66e1da75f 100644--- a/saleor/graphql/product/tests/test_collection.py+++ b/saleor/graphql/product/tests/test_collection.py@@ -718,7 +718,7 @@ MUTATION_UPDATE_COLLECTION_WITH_BACKGROUND_IMAGE = """) {collection {slug- backgroundImage{+ backgroundImage(size: 0) {alturl}@@ -1459,7 +1459,7 @@ def test_collection_image_query_with_size_thumbnail_url_returned()-def test_collection_image_query_only_format_provided_original_image_returned(+def test_collection_image_query_zero_size_custom_format_provided(user_api_client, published_collection, media_root, channel_USD, site_settings):# given@@ -1478,6 +1478,7 @@ def test_collection_image_query_only_format_provided_original_image_returned("id": collection_id,"channel": channel_USD.slug,"format": format,+ "size": 0,}# when@@ -1495,7 +1496,7 @@ def test_collection_image_query_only_format_provided_original_image_returned(assert data["backgroundImage"]["url"] == expected_url-def test_collection_image_query_no_size_value_original_image_returned(+def test_collection_image_query_zero_size_value_original_image_returned(user_api_client, published_collection, media_root, channel_USD, site_settings):# given@@ -1511,6 +1512,7 @@ def test_collection_image_query_no_size_value_original_image_returned(variables = {"id": collection_id,"channel": channel_USD.slug,+ "size": 0,}# whendiff --git a/saleor/graphql/product/types/categories.py b/saleor/graphql/product/types/categories.pyindex bc43e67d55..e6d02558d8 100644--- a/saleor/graphql/product/types/categories.py+++ b/saleor/graphql/product/types/categories.py@@ -109,7 +109,7 @@ class Category(ModelObjectType[models.Category]):returnalt = root.background_image_alt- if not size:+ if size == 0:return Image(url=root.background_image.url, alt=alt)format = get_thumbnail_format(format)diff --git a/saleor/graphql/product/types/collections.py b/saleor/graphql/product/types/collections.pyindex abf9ebeee0..55c43f57c7 100644--- a/saleor/graphql/product/types/collections.py+++ b/saleor/graphql/product/types/collections.py@@ -102,7 +102,7 @@ class Collection(ChannelContextTypeWithMetadata[models.Collection]):returnalt = node.background_image_alt- if not size:+ if size == 0:return Image(url=node.background_image.url, alt=alt)format = get_thumbnail_format(format)diff --git a/saleor/graphql/product/types/products.py b/saleor/graphql/product/types/products.pyindex c065b6f657..58534f79da 100644--- a/saleor/graphql/product/types/products.py+++ b/saleor/graphql/product/types/products.py@@ -1791,7 +1791,7 @@ class ProductMedia(ModelObjectType[models.ProductMedia]):if not root.image:return- if not size:+ if size == 0:return build_absolute_uri(root.image.url)format = get_thumbnail_format(format)@@ -1851,7 +1851,7 @@ class ProductImage(graphene.ObjectType):if not root.image:return- if not size:+ if size == 0:return build_absolute_uri(root.image.url)format = get_thumbnail_format(format)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 5394e133cd..da72eaf4c9 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -4959,12 +4959,12 @@ type Product implements Node & ObjectWithMetadata {descriptionJson: JSONString @deprecated(reason: "This field will be removed in Saleor 4.0. Use the `description` field instead.")thumbnail("""- Size of the image. If not provided, the original image will be returned.+ Desired longest side the image in pixels. Defaults to 4096. Images are never cropped. Pass 0 to retrieve the original size (not recommended)."""size: Int"""- The format of the image. When not provided, format of the original image will be used. Must be provided together with the size value, otherwise original image will be returned.+ The format of the image. When not provided, format of the original image will be used.Added in Saleor 3.6.@@ -5984,12 +5984,12 @@ type Category implements Node & ObjectWithMetadata {): CategoryCountableConnectionbackgroundImage("""- Size of the image. If not provided, the original image will be returned.+ Desired longest side the image in pixels. Defaults to 4096. Images are never cropped. Pass 0 to retrieve the original size (not recommended)."""size: Int"""- The format of the image. When not provided, format of the original image will be used. Must be provided together with the size value, otherwise original image will be returned.+ The format of the image. When not provided, format of the original image will be used.Added in Saleor 3.6.@@ -6595,12 +6595,12 @@ type ProductImage {sortOrder: Inturl("""- Size of the image. If not provided, the original image will be returned.+ Desired longest side the image in pixels. Defaults to 4096. Images are never cropped. Pass 0 to retrieve the original size (not recommended)."""size: Int"""- The format of the image. When not provided, format of the original image will be used. Must be provided together with the size value, otherwise original image will be returned.+ The format of the image. When not provided, format of the original image will be used.Added in Saleor 3.6.@@ -6677,12 +6677,12 @@ type ProductMedia implements Node & ObjectWithMetadata {oembedData: JSONString!url("""- Size of the image. If not provided, the original image will be returned.+ Desired longest side the image in pixels. Defaults to 4096. Images are never cropped. Pass 0 to retrieve the original size (not recommended)."""size: Int"""- The format of the image. When not provided, format of the original image will be used. Must be provided together with the size value, otherwise original image will be returned.+ The format of the image. When not provided, format of the original image will be used.Added in Saleor 3.6.@@ -7045,12 +7045,12 @@ type Collection implements Node & ObjectWithMetadata {): ProductCountableConnectionbackgroundImage("""- Size of the image. If not provided, the original image will be returned.+ Desired longest side the image in pixels. Defaults to 4096. Images are never cropped. Pass 0 to retrieve the original size (not recommended)."""size: Int"""- The format of the image. When not provided, format of the original image will be used. Must be provided together with the size value, otherwise original image will be returned.+ The format of the image. When not provided, format of the original image will be used.Added in Saleor 3.6.@@ -8858,12 +8858,12 @@ type User implements Node & ObjectWithMetadata {editableGroups: [Group!]avatar("""- Size of the image. If not provided, the original image will be returned.+ Desired longest side the image in pixels. Defaults to 4096. Images are never cropped. Pass 0 to retrieve the original size (not recommended)."""size: Int"""- The format of the image. When not provided, format of the original image will be used. Must be provided together with the size value, otherwise original image will be returned.+ The format of the image. When not provided, format of the original image will be used.Added in Saleor 3.6.@@ -10007,12 +10007,12 @@ type OrderLine implements Node & ObjectWithMetadata {digitalContentUrl: DigitalContentUrlthumbnail("""- Size of the image. If not provided, the original image will be returned.+ Desired longest side the image in pixels. Defaults to 4096. Images are never cropped. Pass 0 to retrieve the original size (not recommended)."""size: Int"""- The format of the image. When not provided, format of the original image will be used. Must be provided together with the size value, otherwise original image will be returned.+ The format of the image. When not provided, format of the original image will be used.Added in Saleor 3.6.diff --git a/saleor/plugins/email_common.py b/saleor/plugins/email_common.pyindex 2e88742f15..92377bbcfb 100644--- a/saleor/plugins/email_common.py+++ b/saleor/plugins/email_common.py@@ -142,7 +142,7 @@ def format_datetime(this, date, date_format=None):return date.strftime(date_format)-def get_product_image_thumbnail(this, size, image_data):+def get_product_image_thumbnail(this, size: int, image_data):"""Use provided size to get a correct image."""expected_size = get_thumbnail_size(size)return image_data["original"][str(expected_size)]diff --git a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.pyindex 095e12ae78..a6586398e7 100644--- a/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py+++ b/saleor/plugins/webhook/tests/subscription_webhooks/subscription_queries.py@@ -694,7 +694,7 @@ PRODUCT_MEDIA_CREATED = """...on ProductMediaCreated{productMedia{id- url+ url(size: 0)productId}}@@ -708,7 +708,7 @@ PRODUCT_MEDIA_UPDATED = """...on ProductMediaUpdated{productMedia{id- url+ url(size: 0)productId}}@@ -722,7 +722,7 @@ PRODUCT_MEDIA_DELETED = """...on ProductMediaDeleted{productMedia{id- url+ url(size: 0)productId}}diff --git a/saleor/thumbnail/__init__.py b/saleor/thumbnail/__init__.pyindex 30390ee3c4..e19d867df5 100644--- a/saleor/thumbnail/__init__.py+++ b/saleor/thumbnail/__init__.py@@ -3,6 +3,8 @@ default_app_config = "saleor.thumbnail.app.ThumbnailAppConfig"# defines the available thumbnail resolutionsTHUMBNAIL_SIZES = [32, 64, 128, 256, 512, 1024, 2048, 4096]+DEFAULT_THUMBNAIL_SIZE = 4096+class ThumbnailFormat:ORIGINAL = "original"diff --git a/saleor/thumbnail/utils.py b/saleor/thumbnail/utils.pyindex 7f35a28e23..e0a42a90f5 100644--- a/saleor/thumbnail/utils.py+++ b/saleor/thumbnail/utils.py@@ -1,5 +1,5 @@from io import BytesIO-from typing import TYPE_CHECKING, Optional, Union+from typing import TYPE_CHECKING, Optionalimport grapheneimport magic@@ -7,7 +7,12 @@ from django.core.files.storage import default_storagefrom django.urls import reversefrom PIL import Image-from . import MIME_TYPE_TO_PIL_IDENTIFIER, THUMBNAIL_SIZES, ThumbnailFormat+from . import (+ DEFAULT_THUMBNAIL_SIZE,+ MIME_TYPE_TO_PIL_IDENTIFIER,+ THUMBNAIL_SIZES,+ ThumbnailFormat,+)if TYPE_CHECKING:from .models import Thumbnail@@ -38,13 +43,16 @@ def prepare_image_proxy_url(return reverse("thumbnail", kwargs=kwargs)-def get_thumbnail_size(size: Union[str, int]) -> int:+def get_thumbnail_size(size: Optional[int]) -> int:"""Return the closest size to the given one of the available sizes."""- size = int(size)- if size in THUMBNAIL_SIZES:- return size-- return min(THUMBNAIL_SIZES, key=lambda x: abs(x - size))+ if size is None:+ requested_size = DEFAULT_THUMBNAIL_SIZE+ else:+ requested_size = size+ if requested_size in THUMBNAIL_SIZES:+ return requested_size++ return min(THUMBNAIL_SIZES, key=lambda x: abs(x - requested_size))def get_thumbnail_format(format: Optional[str]) -> Optional[str]:diff --git a/saleor/thumbnail/views.py b/saleor/thumbnail/views.pyindex a555ad4c63..963687ab8c 100644--- a/saleor/thumbnail/views.py+++ b/saleor/thumbnail/views.py@@ -46,7 +46,10 @@ def handle_thumbnail(if object_type not in TYPE_TO_MODEL_DATA_MAPPING.keys():return HttpResponseNotFound("Invalid instance type.")- size_px: int = get_thumbnail_size(size)+ try:+ size_px = get_thumbnail_size(int(size))+ except ValueError:+ return HttpResponseNotFound("Invalid size.")# return the thumbnail if it's already existmodel_data = TYPE_TO_MODEL_DATA_MAPPING[object_type]