Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nil being passed as uri #139

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/fastimage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -228,6 +230,8 @@ def initialize(uri, options={})
:size
end

raise BadImageURI if uri.nil?

@type, @state = nil

if uri.respond_to?(:read)
Expand All @@ -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]
Expand Down
10 changes: 10 additions & 0 deletions test/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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