From f8e6a378584c36ed668d5e57453a3d1662d0242f Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Mon, 3 Dec 2018 10:19:49 -0500 Subject: [PATCH] FIX: raise exception when getting dimensions of missing image - follow-up on 0eacd45ab15cbd20ed9f444fd447886a7fc6dccb --- app/models/upload.rb | 8 ++++++-- lib/image_sizer.rb | 2 +- spec/components/image_sizer_spec.rb | 4 ---- spec/models/upload_spec.rb | 10 ++++++++++ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/models/upload.rb b/app/models/upload.rb index bf3d4155154..350537952c5 100644 --- a/app/models/upload.rb +++ b/app/models/upload.rb @@ -127,8 +127,12 @@ class Upload < ActiveRecord::Base Discourse.store.download(self).path end - 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") + end nil end diff --git a/lib/image_sizer.rb b/lib/image_sizer.rb index 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_f diff --git a/spec/components/image_sizer_spec.rb b/spec/components/image_sizer_spec.rb index f3c0960d79e..b0132671016 100644 --- a/spec/components/image_sizer_spec.rb +++ b/spec/components/image_sizer_spec.rb @@ -24,10 +24,6 @@ describe ImageSizer do expect(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' do before do diff --git a/spec/models/upload_spec.rb b/spec/models/upload_spec.rb index f3b88c01984..6e64ee82b62 100644 --- a/spec/models/upload_spec.rb +++ b/spec/models/upload_spec.rb @@ -62,6 +62,16 @@ describe Upload do expect(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" do created_upload = UploadCreator.new(image, image_filename).create_for(user_id) expect(created_upload.extension).to eq("png")