Always rewind the given IO

When an error occured when extracting dimensions, rewinding would be
skipped and the code would jump straight to "rescue" statements. We want
that the IO is always rewinded, even in case of errors.

Note that this doesn't affect the return value of the method, because
the return value of a method-level "ensure" is always ignored by Ruby.
This commit is contained in:
Janko Marohnić 2016-03-08 21:09:42 +07:00
parent 9c7ada1a41
commit 04d1030756
2 changed files with 15 additions and 2 deletions

View File

@ -200,8 +200,6 @@ class FastImage
end
end
uri.rewind if uri.respond_to?(:rewind)
raise SizeNotFound if @options[:raise_on_failure] && @property == :size && !@size
rescue Timeout::Error, SocketError, Errno::ECONNREFUSED, Errno::EHOSTUNREACH, Errno::ECONNRESET,
@ -220,6 +218,9 @@ class FastImage
end
end
ensure
uri.rewind if uri.respond_to?(:rewind)
end
private

View File

@ -292,6 +292,18 @@ class FastImageTest < Test::Unit::TestCase
end
end
def test_should_rewind_ios
string = File.read(File.join(FixturePath, "test.bmp"))
stringio = StringIO.new(string)
FastImage.type(stringio)
assert_equal 0, stringio.pos
string = File.read(File.join(FixturePath, "test.xml"))
stringio = StringIO.new(string)
FastImage.type(stringio)
assert_equal 0, stringio.pos
end
def test_gzipped_file
url = "http://example.nowhere/#{GzipTestImg}"
assert_equal([970, 450], FastImage.size(url))