FIX: raise exception when getting dimensions of missing image

- follow-up on 0eacd45ab1
This commit is contained in:
Penar Musaraj 2018-12-03 10:19:49 -05:00
parent 9125b5fbc4
commit f8e6a37858
4 changed files with 17 additions and 7 deletions

View File

@ -127,8 +127,12 @@ class Upload < ActiveRecord::Base
Discourse.store.download(self).path Discourse.store.download(self).path
end end
self.width, self.height = size = FastImage.new(path).size begin
self.thumbnail_width, self.thumbnail_height = ImageSizer.resize(*size) 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 nil
end end

View File

@ -1,7 +1,7 @@
module ImageSizer module ImageSizer
# Resize an image to the aspect ratio we want # 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? return if width.blank? || height.blank?
max_width = (opts[:max_width] || SiteSetting.max_image_width).to_f max_width = (opts[:max_width] || SiteSetting.max_image_width).to_f

View File

@ -24,10 +24,6 @@ describe ImageSizer do
expect(ImageSizer.resize('100', '101')).to eq([100, 101]) expect(ImageSizer.resize('100', '101')).to eq([100, 101])
end 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 describe 'when larger than the maximum width' do
before do before do

View File

@ -62,6 +62,16 @@ describe Upload do
expect(upload.thumbnail_height).to eq(500) expect(upload.thumbnail_height).to eq(500)
end 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 it "extracts file extension" do
created_upload = UploadCreator.new(image, image_filename).create_for(user_id) created_upload = UploadCreator.new(image, image_filename).create_for(user_id)
expect(created_upload.extension).to eq("png") expect(created_upload.extension).to eq("png")