summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--axlsx.gemspec1
-rw-r--r--examples/IMAGE1UP.JPEGbin82255 -> 0 bytes
-rw-r--r--examples/image1_fake.jpg0
-rw-r--r--lib/axlsx.rb2
-rw-r--r--lib/axlsx/drawing/pic.rb6
-rw-r--r--lib/axlsx/util/mime_type_utils.rb11
-rw-r--r--test/drawing/tc_pic.rb23
-rw-r--r--test/util/tc_mime_type_utils.rb13
8 files changed, 39 insertions, 17 deletions
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/IMAGE1UP.JPEG b/examples/IMAGE1UP.JPEG
deleted file mode 100644
index ce1d0c56..00000000
--- a/examples/IMAGE1UP.JPEG
+++ /dev/null
Binary files differ
diff --git a/examples/image1_fake.jpg b/examples/image1_fake.jpg
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/examples/image1_fake.jpg
diff --git a/lib/axlsx.rb b/lib/axlsx.rb
index c6f01b19..c5d26c2b 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'
@@ -11,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 571e9047..a1e8483a 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, 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/drawing/tc_pic.rb b/test/drawing/tc_pic.rb
index 3cea49c0..2c58fcfd 100644
--- a/test/drawing/tc_pic.rb
+++ b/test/drawing/tc_pic.rb
@@ -5,8 +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_up = File.dirname(__FILE__) + "/../../examples/IMAGE1UP.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"
@image = ws.add_image :image_src => @test_img, :hyperlink => 'https://github.com/randym', :tooltip => "What's up doc?"
end
@@ -70,19 +72,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)
- 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)
+ 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_descr
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