From cbc17cbf6942080a93704fc445e42791222a9dd0 Mon Sep 17 00:00:00 2001 From: Juan Schwindt Date: Tue, 27 Dec 2016 21:48:23 -0300 Subject: [PATCH 1/3] Add support for Data Uri Scheme --- lib/fastimage.rb | 17 +++++++++++++---- test/test.rb | 13 ++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/fastimage.rb b/lib/fastimage.rb index 5d0b1e4..7e7b3c7 100644 --- a/lib/fastimage.rb +++ b/lib/fastimage.rb @@ -28,7 +28,7 @@ # or referrer which some servers require. Pass an :http_header argument to specify headers, # e.g., :http_header => {'User-Agent' => 'Fake Browser'}. # -# FastImage can give you information about the parsed display orientation of an image with Exif +# FastImage can give you information about the parsed display orientation of an image with Exif # data (jpeg or tiff). # # === Examples @@ -59,6 +59,8 @@ require 'addressable/uri' require 'delegate' require 'pathname' require 'zlib' +require 'base64' + class FastImage attr_reader :size, :type, :content_length, :orientation @@ -184,6 +186,8 @@ class FastImage if uri.respond_to?(:read) fetch_using_read(uri) + elsif uri.start_with?('data:') + fetch_using_base64(uri) else begin @parsed_uri = Addressable::URI.parse(uri) @@ -351,7 +355,7 @@ class FastImage else @orientation = 1 end - + instance_variable_set("@#{@property}", result) else raise CannotParseImage @@ -366,6 +370,11 @@ class FastImage send("parse_size_for_#{@type}") end + def fetch_using_base64(uri) + data = uri.split(',')[1] + fetch_using_read StringIO.new(Base64.decode64(data)) + end + module StreamUtil # :nodoc: def read_byte read(1)[0].ord @@ -468,7 +477,7 @@ class FastImage when " File.join(FixturePath, fn)) end @@ -358,11 +361,19 @@ class FastImageTest < Test::Unit::TestCase url = "#{TestUrl}test.gif" assert_equal 1, FastImage.new(url).orientation end - + def test_should_raise_when_handling_invalid_ico_files stringio = StringIO.new("\x00\x00003") assert_raises(FastImage::UnknownImageType) do FastImage.type(stringio, :raise_on_failure => true) end end + + def test_should_support_data_uri_scheme_images + assert_equal DataUriImageInfo[0], FastImage.type(DataUriImage) + assert_equal DataUriImageInfo[1], FastImage.size(DataUriImage) + assert_raises(FastImage::ImageFetchFailure) do + FastImage.type("data:", :raise_on_failure => true) + end + end end From dde7d6c533e4591b64fd618e35d844c6f90f9373 Mon Sep 17 00:00:00 2001 From: Juan Schwindt Date: Tue, 27 Dec 2016 21:59:02 -0300 Subject: [PATCH 2/3] Add Data Uri handling support in README --- README.textile | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.textile b/README.textile index a6e04f8..9e98409 100644 --- a/README.textile +++ b/README.textile @@ -37,6 +37,8 @@ FastImage accepts additional HTTP headers. This can be used to set a user agent FastImage can give you information about the parsed display orientation of an image with Exif data (jpeg or tiff). +FastImage also handles "Data URIs":https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs correctly. + h2. Security As of v1.6.7 FastImage no longer uses @openuri@ to open files, but directly calls @File.open@. Take care to sanitise the strings passed to FastImage; it will try to read from whatever is passed. @@ -62,6 +64,8 @@ FastImage.size("http://stephensykes.com/images/ss.com_x.gif", :http_header => {' => [266, 56] FastImage.new("http://stephensykes.com/images/ExifOrientation3.jpg").orientation => 3 +FastImage.size("data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==") +=> [1, 1] h2. Installation @@ -97,7 +101,7 @@ irb> uri = "http://upload.wikimedia.org/wikipedia/commons/b/b4/Mardin_1350660_13 irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}} [9545, 6623] 0.680000 0.250000 0.930000 ( 7.571887) - + irb> puts Benchmark.measure {p FastImage.size(uri)} [9545, 6623] 0.010000 0.000000 0.010000 ( 0.090640) @@ -114,7 +118,7 @@ irb> uri = "http://upload.wikimedia.org/wikipedia/commons/1/11/Shinbutsureijoush irb> puts Benchmark.measure {open(uri, 'rb') {|fh| p ImageSize.new(fh).size}} [1120, 1559] 1.080000 0.370000 1.450000 ( 13.766962) - + irb> puts Benchmark.measure {p FastImage.size(uri)} [1120, 1559] 3.490000 3.810000 7.300000 ( 11.754315) @@ -124,8 +128,8 @@ h2. Tests You'll need to @gem install fakeweb@ and possibly also @gem install test-unit@ to be able to run the tests. -bc.. $ ruby test.rb -Run options: +bc.. $ ruby test.rb +Run options: # Running tests: From bc43e8b57081c7d4b20ca8895156c5ec191e3d05 Mon Sep 17 00:00:00 2001 From: Stephen Sykes Date: Wed, 22 Feb 2017 18:17:22 +0200 Subject: [PATCH 3/3] Fix missing end --- test/test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/test.rb b/test/test.rb index 34edbe5..862ac46 100644 --- a/test/test.rb +++ b/test/test.rb @@ -375,6 +375,7 @@ class FastImageTest < Test::Unit::TestCase assert_raises(FastImage::ImageFetchFailure) do FastImage.type("data:", :raise_on_failure => true) end + end def test_should_work_with_domains_with_underscores assert_equal :gif, FastImage.type("http://foo_bar.inbro.net/images/p.gif")