From 04d103075698a2f7ad0e1ba878bd8aa7eee24dc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janko=20Marohni=C4=87?= Date: Tue, 8 Mar 2016 21:09:42 +0700 Subject: [PATCH] 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. --- lib/fastimage.rb | 5 +++-- test/test.rb | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/fastimage.rb b/lib/fastimage.rb index 69c30dd..1096dfa 100644 --- a/lib/fastimage.rb +++ b/lib/fastimage.rb @@ -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 diff --git a/test/test.rb b/test/test.rb index 7769a80..7538194 100644 --- a/test/test.rb +++ b/test/test.rb @@ -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))