Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
fix thumbnail generation long image names. (#12435)
Kyrylo Plaksin329000093125
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex a180dacb22..480ea24dcd 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -217,6 +217,7 @@ All notable, unreleased changes to this project will be documented in this file.- Set flat rates as the default tax calculation strategy - #12069 by @maarcingebala- Enables flat rates for channels in which no tax calculation method was set.- Users created by the OIDC plugin now have unusable password set instead of empty string - #12103 by @rafalp+- Fix thumbnail generation long image names - #12435 by @KirillPlaksin# 3.11.0diff --git a/saleor/graphql/core/tests/test_core.py b/saleor/graphql/core/tests/test_core.pyindex f3c3742ac6..66b6f087f8 100644--- a/saleor/graphql/core/tests/test_core.py+++ b/saleor/graphql/core/tests/test_core.py@@ -21,6 +21,7 @@ from ..filters import EnumFilterfrom ..mutations import BaseMutation, ModelWithExtRefMutationfrom ..types import FilterInputObjectTypefrom ..utils import (+ FILE_NAME_MAX_LENGTH,add_hash_to_file_name,ext_ref_to_global_id_or_error,get_duplicated_values,@@ -304,6 +305,35 @@ def test_add_hash_to_file_name(image, media_root):assert image._name.endswith(format)+def test_short_file_name_is_not_trimmed(image, media_root):+ image._name = "image"+ previous_file_name = image._name+ assert len(previous_file_name) < FILE_NAME_MAX_LENGTH++ add_hash_to_file_name(image)++ assert previous_file_name != image._name+ file_name, format = os.path.splitext(image._name)+ assert image._name.startswith(file_name)+ assert image._name.endswith(format)+ assert len(image._name.split("_")[0]) < FILE_NAME_MAX_LENGTH+++def test_long_file_name_is_trimmed(image, media_root):+ image._name = "2Fvar2Ffolders2Fbj2F61gtb14j7rz474yd15tnkzjh0000gn2FT2Fa"+ previous_file_name = image._name+ assert len(previous_file_name) > FILE_NAME_MAX_LENGTH++ add_hash_to_file_name(image)++ assert previous_file_name != image._name+ file_name, format = os.path.splitext(image._name)+ assert image._name.startswith(file_name)+ assert image._name.startswith(file_name[:FILE_NAME_MAX_LENGTH])+ assert image._name.endswith(format)+ assert len(image._name.split("_")[0]) == FILE_NAME_MAX_LENGTH++def test_external_reference_to_global_id(product):# givenproduct.external_reference = "test-ext-id"diff --git a/saleor/graphql/core/tests/test_file_validation.py b/saleor/graphql/core/tests/test_file_validation.pyindex 541398952d..7b06cc4466 100644--- a/saleor/graphql/core/tests/test_file_validation.py+++ b/saleor/graphql/core/tests/test_file_validation.py@@ -7,6 +7,7 @@ from django.core.files.uploadedfile import SimpleUploadedFilefrom PIL import Imagefrom ....product.error_codes import ProductErrorCode+from ...core.utils import FILE_NAME_MAX_LENGTHfrom ..validators.file import (clean_image_file,get_filename_from_url,@@ -31,6 +32,40 @@ def test_get_filename_from_url_unique():assert result != f"{file_name}.{file_format}"+def test_get_filename_from_url_with_long_name_is_trimmed():+ # given+ file_format = "jpg"+ file_name = "2Fvar2Ffolders2Fbj2F61gtb14j7rz474yd15tnkzjh0000gn2FT2Fa"+ assert len(file_name) > FILE_NAME_MAX_LENGTH+ url = f"http://example.com/{file_name}.{file_format}"++ # when+ result = get_filename_from_url(url)++ # then+ assert result.startswith(file_name[:FILE_NAME_MAX_LENGTH])+ assert result.endswith(file_format)+ assert result != f"{file_name}.{file_format}"+ assert len(result.split("_")[0]) == FILE_NAME_MAX_LENGTH+++def test_get_filename_from_url_with_short_name_is_not_trimmed():+ # given+ file_format = "jpg"+ file_name = "short"+ assert len(file_name) < FILE_NAME_MAX_LENGTH+ url = f"http://example.com/{file_name}.{file_format}"++ # when+ result = get_filename_from_url(url)++ # then+ assert result.startswith(file_name)+ assert result.endswith(file_format)+ assert result != f"{file_name}.{file_format}"+ assert len(result.split("_")[0]) < FILE_NAME_MAX_LENGTH++def test_is_image_mimetype_valid_mimetype():# givenvalid_mimetype = "image/jpeg"diff --git a/saleor/graphql/core/utils/__init__.py b/saleor/graphql/core/utils/__init__.pyindex 46eca5450e..a6ba01ec32 100644--- a/saleor/graphql/core/utils/__init__.py+++ b/saleor/graphql/core/utils/__init__.py@@ -11,6 +11,8 @@ from graphql.error import GraphQLErrorfrom ....plugins.const import APP_ID_PREFIXfrom ..validators import validate_if_int_or_uuid+FILE_NAME_MAX_LENGTH = 55+def snake_to_camel_case(name):"""Convert snake_case variable name to camelCase."""@@ -108,6 +110,7 @@ def to_global_id_or_none(instance):def add_hash_to_file_name(file):"""Add unique text fragment to the file name to prevent file overriding."""file_name, format = os.path.splitext(file._name)+ file_name = file_name[:FILE_NAME_MAX_LENGTH]hash = secrets.token_hex(nbytes=4)new_name = f"{file_name}_{hash}{format}"file._name = new_namediff --git a/saleor/graphql/core/validators/file.py b/saleor/graphql/core/validators/file.pyindex 741af02ddb..6e3acff972 100644--- a/saleor/graphql/core/validators/file.py+++ b/saleor/graphql/core/validators/file.py@@ -7,7 +7,7 @@ from django.core.exceptions import ValidationErrorfrom PIL import Image, UnidentifiedImageErrorfrom ....thumbnail import MIME_TYPE_TO_PIL_IDENTIFIER-from ..utils import add_hash_to_file_name+from ..utils import FILE_NAME_MAX_LENGTH, add_hash_to_file_nameImage.init()@@ -54,6 +54,7 @@ def get_filename_from_url(url: str) -> str:"""Prepare unique filename for file from URL to avoid overwritting."""file_name = os.path.basename(url)name, format = os.path.splitext(file_name)+ name = name[:FILE_NAME_MAX_LENGTH]hash = secrets.token_hex(nbytes=4)return f"{name}_{hash}{format}"