diff options
| author | Sebastiano <[email protected]> | 2023-01-17 14:55:35 +0100 |
|---|---|---|
| committer | Koza <[email protected]> | 2023-04-12 17:27:52 +0200 |
| commit | f91cf59d7a62643140378e601f0a11a4deb23465 (patch) | |
| tree | 612757024b7efb73d81d62402a56c347000108f8 /lib | |
| parent | 9bd3367b35bcc85f74c749cfbab05e9c430ab064 (diff) | |
| download | caxlsx-f91cf59d7a62643140378e601f0a11a4deb23465.tar.gz caxlsx-f91cf59d7a62643140378e601f0a11a4deb23465.zip | |
improve validation
add test cases
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 7 | ||||
| -rw-r--r-- | lib/axlsx/util/mime_type_utils.rb | 9 |
2 files changed, 14 insertions, 2 deletions
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index b6d085de..f2f72cea 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -77,7 +77,10 @@ module Axlsx def image_src=(v) Axlsx::validate_string(v) - unless remote? + if remote? + RegexValidator.validate('Pic.image_src', /\A#{URI::DEFAULT_PARSER.make_regexp}\z/, v) + RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type_from_uri(v) + else RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type(v) raise ArgumentError, "File does not exist" unless File.exist?(v) end @@ -194,7 +197,7 @@ module Axlsx str << '</xdr:cNvPicPr></xdr:nvPicPr>' str << '<xdr:blipFill>' if remote? - str << ('<a:blip xmlns:r ="' << XML_NS_R << '" r:link="' << relationship.Id << '" >') + str << ('<a:blip xmlns:r ="' << XML_NS_R << '" r:link="' << relationship.Id << '">') else str << ('<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="' << relationship.Id << '">') end diff --git a/lib/axlsx/util/mime_type_utils.rb b/lib/axlsx/util/mime_type_utils.rb index 9ad56630..5a6ad38e 100644 --- a/lib/axlsx/util/mime_type_utils.rb +++ b/lib/axlsx/util/mime_type_utils.rb @@ -1,3 +1,5 @@ +require 'open-uri' + module Axlsx # This module defines some utils related with mime type detection module MimeTypeUtils @@ -7,5 +9,12 @@ module Axlsx def self.get_mime_type(v) Marcel::MimeType.for(Pathname.new(v)) end + + # Detect a file mime type from URI + # @param [String] v URI + # @return [String] File mime type + def self.get_mime_type_from_uri(v) + Marcel::MimeType.for(URI.open(v)) + end end end |
