A platform for community discussion. Free, open, simple.
FIX: raise exception when getting dimensions of missing image
Penar Musarajf8e6a378584c
diff --git a/app/models/upload.rb b/app/models/upload.rbindex bf3d4155154..350537952c5 100644--- a/app/models/upload.rb+++ b/app/models/upload.rb@@ -127,8 +127,12 @@ class Upload < ActiveRecord::BaseDiscourse.store.download(self).pathend- self.width, self.height = size = FastImage.new(path).size- self.thumbnail_width, self.thumbnail_height = ImageSizer.resize(*size)+ begin+ self.width, self.height = size = FastImage.new(path, raise_on_failure: true).size+ self.thumbnail_width, self.thumbnail_height = ImageSizer.resize(*size)+ rescue => e+ Discourse.warn_exception(e, message: "Error getting image dimensions")+ endnilenddiff --git a/lib/image_sizer.rb b/lib/image_sizer.rbindex 6e1646416cb..ee368ce3d1e 100644--- a/lib/image_sizer.rb+++ b/lib/image_sizer.rb@@ -1,7 +1,7 @@module ImageSizer# Resize an image to the aspect ratio we want- def self.resize(width = nil, height = nil, opts = {})+ def self.resize(width, height, opts = {})return if width.blank? || height.blank?max_width = (opts[:max_width] || SiteSetting.max_image_width).to_fdiff --git a/spec/components/image_sizer_spec.rb b/spec/components/image_sizer_spec.rbindex f3c0960d79e..b0132671016 100644--- a/spec/components/image_sizer_spec.rb+++ b/spec/components/image_sizer_spec.rb@@ -24,10 +24,6 @@ describe ImageSizer doexpect(ImageSizer.resize('100', '101')).to eq([100, 101])end- it 'returns nil if no width or height are provided' do- expect(ImageSizer.resize).to eq(nil)- end-describe 'when larger than the maximum width' dobefore dodiff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rbindex f3b88c01984..6e64ee82b62 100644--- a/spec/models/upload_spec.rb+++ b/spec/models/upload_spec.rb@@ -62,6 +62,16 @@ describe Upload doexpect(upload.thumbnail_height).to eq(500)end+ it "dimension calculation returns nil on missing image" do+ upload = UploadCreator.new(huge_image, "image.png").create_for(user_id)+ upload.update_columns(width: nil, height: nil, thumbnail_width: nil, thumbnail_height: nil)++ missing_url = "wrong_folder#{upload.url}"+ upload.update_columns(url: missing_url)+ expect(upload.thumbnail_height).to eq(nil)+ expect(upload.thumbnail_width).to eq(nil)+ end+it "extracts file extension" docreated_upload = UploadCreator.new(image, image_filename).create_for(user_id)expect(created_upload.extension).to eq("png")