From e5266ff249f4f9540f4b9d9229eeaa52757fda2e Mon Sep 17 00:00:00 2001 From: Stephen Sykes Date: Fri, 7 Apr 2023 09:52:23 +0300 Subject: [PATCH] Handle nil being passed as uri Fixes #138 --- lib/fastimage.rb | 6 +++++- test/test.rb | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/fastimage.rb b/lib/fastimage.rb index 9b6c26d..a87d2bb 100644 --- a/lib/fastimage.rb +++ b/lib/fastimage.rb @@ -85,6 +85,8 @@ class SizeNotFound < FastImageException # :nodoc: end class CannotParseImage < FastImageException # :nodoc: end + class BadImageURI < FastImageException # :nodoc: + end DefaultTimeout = 2 unless const_defined?(:DefaultTimeout) @@ -228,6 +230,8 @@ def initialize(uri, options={}) :size end + raise BadImageURI if uri.nil? + @type, @state = nil if uri.respond_to?(:read) @@ -254,7 +258,7 @@ def initialize(uri, options={}) Errno::ENETUNREACH, ImageFetchFailure, Net::HTTPBadResponse, EOFError, Errno::ENOENT, OpenSSL::SSL::SSLError raise ImageFetchFailure if @options[:raise_on_failure] - rescue UnknownImageType + rescue UnknownImageType, BadImageURI raise if @options[:raise_on_failure] rescue CannotParseImage if @options[:raise_on_failure] diff --git a/test/test.rb b/test/test.rb index d785ef5..b9e5e76 100644 --- a/test/test.rb +++ b/test/test.rb @@ -481,4 +481,14 @@ def test_canon_raw_formats_are_not_recognised_as_tiff FastImage.size(TestUrl + "a.CRW", :raise_on_failure=>true) end end + + def test_returns_nil_when_uri_is_nil + assert_equal nil, FastImage.size(nil) + end + + def test_raises_when_uri_is_nil_and_raise_on_failure_is_set + assert_raises(FastImage::BadImageURI) do + FastImage.size(nil, :raise_on_failure => true) + end + end end