diff options
| author | Koza <[email protected]> | 2023-04-12 17:33:59 +0200 |
|---|---|---|
| committer | Koza <[email protected]> | 2023-04-12 17:33:59 +0200 |
| commit | 2d714298a462a1482bd8e12fbb2efb74d6acee5f (patch) | |
| tree | c377925f9f0ea9106d3c7d808130863c11a6ce63 /lib | |
| parent | e548f377932207130cec4ac257a3907385d547d5 (diff) | |
| parent | 518e74f8769daf67fca461139d3c7229abdb6e60 (diff) | |
| download | caxlsx-2d714298a462a1482bd8e12fbb2efb74d6acee5f.tar.gz caxlsx-2d714298a462a1482bd8e12fbb2efb74d6acee5f.zip | |
Merge branch 'feature/remote_images_support'
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 41 | ||||
| -rw-r--r-- | lib/axlsx/package.rb | 2 | ||||
| -rw-r--r-- | lib/axlsx/util/mime_type_utils.rb | 9 |
3 files changed, 46 insertions, 6 deletions
diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index fb4d01fe..e31876bf 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -7,6 +7,7 @@ module Axlsx # Creates a new Pic(ture) object # @param [Anchor] anchor the anchor that holds this image + # @option options [Boolean] :remote indicates if image_src is a remote URI # @option options [String] :name # @option options [String] :descr # @option options [String] :image_src @@ -18,6 +19,7 @@ module Axlsx @anchor = anchor @hyperlink = nil @anchor.drawing.worksheet.workbook.images << self + @remote = options[:remote] parse_options options start_at(*options[:start_at]) if options[:start_at] yield self if block_given? @@ -54,6 +56,10 @@ module Axlsx # @return [Integer] attr_reader :opacity + # Flag for remote picture (from URI) + # @return [Boolean] + attr_reader :remote + # sets or updates a hyperlink for this image. # @param [String] v The href value for the hyper link # @option options @see Hyperlink#initialize All options available to the Hyperlink class apply - however href will be overridden with the v parameter value. @@ -71,8 +77,13 @@ module Axlsx def image_src=(v) Axlsx::validate_string(v) - RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type(v) - raise ArgumentError, "File does not exist" unless File.exist?(v) + 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 @image_src = v end @@ -83,10 +94,17 @@ module Axlsx # @see descr def descr=(v) Axlsx::validate_string(v); @descr = v; end + # @see remote + def remote=(v) Axlsx::validate_boolean(v); @remote = v; end + + def remote? + remote == 1 || remote.to_s == 'true' + end + # The file name of image_src without any path information # @return [String] def file_name - File.basename(image_src) unless image_src.nil? + File.basename(image_src) unless remote? || image_src.nil? end # returns the extension of image_src without the preceeding '.' @@ -110,7 +128,11 @@ module Axlsx # The relationship object for this pic. # @return [Relationship] def relationship - Relationship.new(self, IMAGE_R, "../#{pn}") + if remote? + Relationship.new(self, IMAGE_R, "#{image_src}", target_mode: :External) + else + Relationship.new(self, IMAGE_R, "../#{pn}") + end end # providing access to the anchor's width attribute @@ -174,7 +196,7 @@ module Axlsx picture_locking.to_xml_string(str) str << '</xdr:cNvPicPr></xdr:nvPicPr>' str << '<xdr:blipFill>' - str << ('<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="' << relationship.Id << '">') + str << relationship_xml_portion if opacity str << "<a:alphaModFix amt=\"#{opacity}\"/>" end @@ -186,6 +208,15 @@ module Axlsx private + # Return correct xml relationship string portion + def relationship_xml_portion + if remote? + ('<a:blip xmlns:r ="' << XML_NS_R << '" r:link="' << relationship.Id << '">') + else + ('<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="' << relationship.Id << '">') + end + end + # Changes the anchor to a one cell anchor. def use_one_cell_anchor return if @anchor.is_a?(OneCellAnchor) diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index 2118d0ea..b61c3de1 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -247,7 +247,7 @@ module Axlsx end workbook.images.each do |image| - parts << { :entry => "xl/#{image.pn}", :path => image.image_src } + parts << { :entry => "xl/#{image.pn}", :path => image.image_src } unless image.remote? end if use_shared_strings 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 |
