From cf4d92f686b55ec481ec93fc4f4de983b5a6c217 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Mon, 12 Feb 2024 09:56:43 +1000 Subject: [PATCH] FIX: Change max_image_megapixels logic (#25625) This commit changes `max_image_megapixels` to be used as is without multiplying by 2 to give extra leway. We found in reality this was just causing confusion for admins, especially with the already permissive 40MP default. --- config/locales/server.en.yml | 2 +- lib/upload_creator.rb | 4 ++-- spec/models/upload_spec.rb | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 13c944df02b..829b4e1efc4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -4345,7 +4345,7 @@ en: images: too_large: "Sorry, the image you are trying to upload is too big (maximum size is %{max_size_kb}KB), please resize it and try again." too_large_humanized: "Sorry, the image you are trying to upload is too big (maximum size is %{max_size}), please resize it and try again." - larger_than_x_megapixels: "Sorry, the image you are trying to upload is too large (maximum dimension is %{max_image_megapixels}-megapixels), please resize it and try again." + larger_than_x_megapixels: "Sorry, the image you are trying to upload is too large (maximum dimension is %{max_image_megapixels} megapixels), please resize it and try again." size_not_found: "Sorry, but we couldn't determine the size of the image. Maybe your image is corrupted?" placeholders: too_large: "(image larger than %{max_size_kb}KB)" diff --git a/lib/upload_creator.rb b/lib/upload_creator.rb index 54615a1101f..c992c285c12 100644 --- a/lib/upload_creator.rb +++ b/lib/upload_creator.rb @@ -301,12 +301,12 @@ class UploadCreator @upload.errors.add(:base, I18n.t("upload.empty")) elsif pixels == 0 && @image_info.type.to_s != "svg" @upload.errors.add(:base, I18n.t("upload.images.size_not_found")) - elsif max_image_pixels > 0 && pixels >= max_image_pixels * 2 + elsif max_image_pixels > 0 && pixels >= max_image_pixels @upload.errors.add( :base, I18n.t( "upload.images.larger_than_x_megapixels", - max_image_megapixels: SiteSetting.max_image_megapixels * 2, + max_image_megapixels: SiteSetting.max_image_megapixels, ), ) end diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index b955571fa0a..0fbd19ee48b 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -74,6 +74,7 @@ RSpec.describe Upload do end it "can reconstruct dimensions on demand" do + SiteSetting.max_image_megapixels = 85 upload = UploadCreator.new(huge_image, "image.png").create_for(user_id) upload.update_columns(width: nil, height: nil, thumbnail_width: nil, thumbnail_height: nil) @@ -93,6 +94,7 @@ RSpec.describe Upload do end it "dimension calculation returns nil on missing image" do + SiteSetting.max_image_megapixels = 85 upload = UploadCreator.new(huge_image, "image.png").create_for(user_id) upload.update_columns(width: nil, height: nil, thumbnail_width: nil, thumbnail_height: nil) @@ -107,7 +109,7 @@ RSpec.describe Upload do upload = UploadCreator.new(huge_image, "image.png").create_for(user_id) expect(upload.persisted?).to eq(false) expect(upload.errors.messages[:base].first).to eq( - I18n.t("upload.images.larger_than_x_megapixels", max_image_megapixels: 20), + I18n.t("upload.images.larger_than_x_megapixels", max_image_megapixels: 10), ) end