From 00078a438b0dd480089518036d336d0d4cee995c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 8 Mar 2016 11:29:18 +0800 Subject: [PATCH] FIX: `FastImage#size` returns `nil` if it can't fetch the image size. --- lib/cooked_post_processor.rb | 11 +++++++---- spec/components/cooked_post_processor_spec.rb | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 147824d829f..ef1e3d69427 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -116,13 +116,16 @@ class CookedPostProcessor if w > 0 || h > 0 w = w.to_f h = h.to_f - original_width, original_height = get_size(img["src"]).map {|integer| integer.to_f} + + return unless original_image_size = get_size(img["src"]) + original_width, original_height = original_image_size.map(&:to_f) + if w > 0 ratio = w/original_width - return [w.floor, (original_height*ratio).floor] + [w.floor, (original_height*ratio).floor] else ratio = h/original_height - return [(original_width*ratio).floor, h.floor] + [(original_width*ratio).floor, h.floor] end end end @@ -149,7 +152,7 @@ class CookedPostProcessor return unless is_valid_image_url?(absolute_url) # we can *always* crawl our own images - return unless SiteSetting.crawl_images? || Discourse.store.has_been_uploaded?(url) + return unless SiteSetting.crawl_images || Discourse.store.has_been_uploaded?(url) @size_cache[url] ||= FastImage.size(absolute_url) rescue Zlib::BufError # FastImage.size raises BufError for some gifs diff --git a/spec/components/cooked_post_processor_spec.rb b/spec/components/cooked_post_processor_spec.rb index 89d0c51e0a1..80071340064 100644 --- a/spec/components/cooked_post_processor_spec.rb +++ b/spec/components/cooked_post_processor_spec.rb @@ -267,6 +267,12 @@ describe CookedPostProcessor do expect(cpp.get_size("http://foo.bar/image2.png")).to eq([100, 200]) end + it "returns nil if FastImage can't get the original size" do + Discourse.store.class.any_instance.expects(:has_been_uploaded?).returns(true) + FastImage.expects(:size).returns(nil) + expect(cpp.get_size("http://foo.bar/image3.png")).to eq(nil) + end + end end