From c7c7937dcde9a94f2e5ee0549451b34e410803e0 Mon Sep 17 00:00:00 2001 From: marc Date: Fri, 3 Jul 2015 16:44:45 +0200 Subject: Validates whether an image is acceptable through mime type image/jpeg, image/png & image/gif are considered the allowed mime types for an image. mimemagic gem is added as dependency in order to do the checking. Added tests to check against three supported mime types. A fake jpg fixture (created with 'touch' command) has been added to test that extension is no longer used. --- axlsx.gemspec | 1 + examples/image1_fake.jpg | 0 lib/axlsx.rb | 1 + lib/axlsx/drawing/pic.rb | 6 +++--- test/drawing/tc_pic.rb | 14 +++++++++----- 5 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 examples/image1_fake.jpg diff --git a/axlsx.gemspec b/axlsx.gemspec index 9fc19285..8e3cd9ca 100644 --- a/axlsx.gemspec +++ b/axlsx.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |s| s.add_runtime_dependency 'nokogiri', '>= 1.4.1' s.add_runtime_dependency 'rubyzip', '~> 1.1.7' s.add_runtime_dependency "htmlentities", "~> 4.3.1" + s.add_runtime_dependency "mimemagic", "~> 0.3" s.add_development_dependency 'yard' s.add_development_dependency 'kramdown' diff --git a/examples/image1_fake.jpg b/examples/image1_fake.jpg new file mode 100644 index 00000000..e69de29b diff --git a/lib/axlsx.rb b/lib/axlsx.rb index c6f01b19..14456024 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -1,6 +1,7 @@ # encoding: UTF-8 require 'htmlentities' require 'axlsx/version.rb' +require 'mimemagic' require 'axlsx/util/simple_typed_list.rb' require 'axlsx/util/constants.rb' diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index 571e9047..b090c661 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -25,8 +25,8 @@ module Axlsx @picture_locking = PictureLocking.new(options) end - # allowed file extenstions - ALLOWED_EXTENSIONS = ['gif', 'jpeg', 'png', 'jpg'] + # allowed mime types + ALLOWED_MIME_TYPES = %w(image/jpeg image/png image/gif) # The name to use for this picture # @return [String] @@ -67,7 +67,7 @@ module Axlsx def image_src=(v) Axlsx::validate_string(v) - RestrictionValidator.validate 'Pic.image_src', ALLOWED_EXTENSIONS, File.extname(v.downcase).delete('.') + RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeMagic.by_magic(File.open(v)).to_s raise ArgumentError, "File does not exist" unless File.exist?(v) @image_src = v end diff --git a/test/drawing/tc_pic.rb b/test/drawing/tc_pic.rb index 3cea49c0..066d9962 100644 --- a/test/drawing/tc_pic.rb +++ b/test/drawing/tc_pic.rb @@ -5,7 +5,10 @@ class TestPic < Test::Unit::TestCase def setup @p = Axlsx::Package.new ws = @p.workbook.add_worksheet - @test_img = File.dirname(__FILE__) + "/../../examples/image1.jpeg" + @test_img = @test_img_jpg = File.dirname(__FILE__) + "/../../examples/image1.jpeg" + @test_img_png = File.dirname(__FILE__) + "/../../examples/image1.png" + @test_img_gif = File.dirname(__FILE__) + "/../../examples/image1.gif" + @test_img_fake = File.dirname(__FILE__) + "/../../examples/image1_fake.jpg" @test_img_up = File.dirname(__FILE__) + "/../../examples/IMAGE1UP.JPEG" @image = ws.add_image :image_src => @test_img, :hyperlink => 'https://github.com/randym', :tooltip => "What's up doc?" end @@ -70,11 +73,12 @@ class TestPic < Test::Unit::TestCase end def test_image_src - assert_raise(ArgumentError) { @image.image_src = 49 } - assert_raise(ArgumentError) { @image.image_src = 'Unknown' } assert_raise(ArgumentError) { @image.image_src = __FILE__ } - assert_nothing_raised { @image.image_src = @test_img } - assert_equal(@image.image_src, @test_img) + assert_raise(ArgumentError) { @image.image_src = @test_img_fake } + assert_nothing_raised { @image.image_src = @test_img_gif } + assert_nothing_raised { @image.image_src = @test_img_png } + assert_nothing_raised { @image.image_src = @test_img_jpg } + assert_equal(@image.image_src, @test_img_jpg) end def test_image_src_downcase -- cgit v1.2.3 From d7de10ade8d8f28efd0a41963aa263f9ef58ceb4 Mon Sep 17 00:00:00 2001 From: marc Date: Fri, 3 Jul 2015 16:58:21 +0200 Subject: Extract mime type detection to an utility class --- lib/axlsx.rb | 1 + lib/axlsx/drawing/pic.rb | 2 +- lib/axlsx/util/mime_type_utils.rb | 11 +++++++++++ test/util/tc_mime_type_utils.rb | 13 +++++++++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 lib/axlsx/util/mime_type_utils.rb create mode 100644 test/util/tc_mime_type_utils.rb diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 14456024..c5d26c2b 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -12,6 +12,7 @@ require 'axlsx/util/options_parser' # to be included with parsable intitites. #require 'axlsx/util/parser.rb' require 'axlsx/util/string' +require 'axlsx/util/mime_type_utils' require 'axlsx/stylesheet/styles.rb' diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index b090c661..a1e8483a 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -67,7 +67,7 @@ module Axlsx def image_src=(v) Axlsx::validate_string(v) - RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeMagic.by_magic(File.open(v)).to_s + RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type(v) raise ArgumentError, "File does not exist" unless File.exist?(v) @image_src = v end diff --git a/lib/axlsx/util/mime_type_utils.rb b/lib/axlsx/util/mime_type_utils.rb new file mode 100644 index 00000000..3fe2dbbd --- /dev/null +++ b/lib/axlsx/util/mime_type_utils.rb @@ -0,0 +1,11 @@ +module Axlsx + # This module defines some utils related with mime type detection + module MimeTypeUtils + # Detect a file mime type + # @param [String] v File path + # @return [String] File mime type + def self.get_mime_type(v) + MimeMagic.by_magic(File.open(v)).to_s + end + end +end diff --git a/test/util/tc_mime_type_utils.rb b/test/util/tc_mime_type_utils.rb new file mode 100644 index 00000000..ff54ae0a --- /dev/null +++ b/test/util/tc_mime_type_utils.rb @@ -0,0 +1,13 @@ +require 'tc_helper.rb' +class TestMimeTypeUtils < Test::Unit::TestCase + def setup + @test_img = File.dirname(__FILE__) + "/../../examples/image1.jpeg" + end + + def teardown + end + + def test_mime_type_utils + assert_equal(Axlsx::MimeTypeUtils::get_mime_type(@test_img), 'image/jpeg') + end +end -- cgit v1.2.3 From 9bc7bf33a7e935f9c0fdb7437a67df2f871a0e05 Mon Sep 17 00:00:00 2001 From: marc Date: Fri, 3 Jul 2015 17:12:08 +0200 Subject: Remove lowercase extension test It is no longer needed because now detection is though mime type --- examples/IMAGE1UP.JPEG | Bin 82255 -> 0 bytes test/drawing/tc_pic.rb | 9 --------- 2 files changed, 9 deletions(-) delete mode 100644 examples/IMAGE1UP.JPEG diff --git a/examples/IMAGE1UP.JPEG b/examples/IMAGE1UP.JPEG deleted file mode 100644 index ce1d0c56..00000000 Binary files a/examples/IMAGE1UP.JPEG and /dev/null differ diff --git a/test/drawing/tc_pic.rb b/test/drawing/tc_pic.rb index 066d9962..2c58fcfd 100644 --- a/test/drawing/tc_pic.rb +++ b/test/drawing/tc_pic.rb @@ -9,7 +9,6 @@ class TestPic < Test::Unit::TestCase @test_img_png = File.dirname(__FILE__) + "/../../examples/image1.png" @test_img_gif = File.dirname(__FILE__) + "/../../examples/image1.gif" @test_img_fake = File.dirname(__FILE__) + "/../../examples/image1_fake.jpg" - @test_img_up = File.dirname(__FILE__) + "/../../examples/IMAGE1UP.JPEG" @image = ws.add_image :image_src => @test_img, :hyperlink => 'https://github.com/randym', :tooltip => "What's up doc?" end @@ -81,14 +80,6 @@ class TestPic < Test::Unit::TestCase assert_equal(@image.image_src, @test_img_jpg) end - def test_image_src_downcase - assert_nothing_raised { @image.image_src = @test_img_up } - ct = @p.send(:content_types).detect do |t| - t.respond_to?(:extension) && t.extension.downcase == @image.extname.downcase - end - assert_equal("image/jpeg", ct.content_type) - end - def test_descr assert_raise(ArgumentError) { @image.descr = 49 } assert_nothing_raised { @image.descr = "test" } -- cgit v1.2.3