FIX: FastImage#size returns nil if it can't fetch the image size.

This commit is contained in:
Guo Xiang Tan 2016-03-08 11:29:18 +08:00
parent fcc86d3a9d
commit 00078a438b
2 changed files with 13 additions and 4 deletions

View file

@ -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

View file

@ -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