Sha256: 429841e37152d878c0d47e861dad4ed7149d51764aaee145d0c2b4e1df108da5

Contents?: true

Size: 1.7 KB

Versions: 12

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require 'uri'

module OoxmlParser
  # Class for storing image data
  class FileReference < OOXMLDocumentObject
    # @return [String] id of resource
    attr_accessor :resource_id
    # @return [String] path to file
    attr_accessor :path
    # @return [String] content of file
    attr_accessor :content

    # Parse FileReference object
    # @param node [Nokogiri::XML:Element] node to parse
    # @return [FileReference] result of parsing
    def parse(node)
      node.attributes.each do |key, value|
        case key
        when 'embed', 'id', 'link'
          @resource_id = value.value
        end
      end
      return self unless @resource_id
      return self if @resource_id.empty?

      @path = root_object.get_link_from_rels(@resource_id)
      if !@path || @path.empty?
        warn "Cant find path to media file by id: #{@resource_id}"
        return self
      end
      return self if @path == 'NULL'
      return self if @path.match?(URI::DEFAULT_PARSER.make_regexp)

      full_path_to_file = root_object.unpacked_folder + root_object.root_subfolder + @path.gsub('..', '')
      if File.exist?(full_path_to_file)
        @content = if File.extname(@path) == '.xlsx'
                     parse_ole_xlsx(full_path_to_file)
                   else
                     File.binread(full_path_to_file)
                   end
      else
        warn "Couldn't find #{full_path_to_file} file on filesystem. Possible problem in original document"
      end
      self
    end

    private

    # Parse ole xlsx file
    # @param [String] full_path to file
    # @return [XLSXWorkbook]
    def parse_ole_xlsx(full_path)
      OoxmlParser::XlsxParser.parse_xlsx(full_path)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
ooxml_parser-0.37.1 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.37.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.36.1 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.36.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.35.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.34.2 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.34.1 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.34.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.33.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.32.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.31.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb
ooxml_parser-0.30.0 lib/ooxml_parser/common_parser/common_data/alternate_content/drawing/graphic/picture/docx_blip/file_reference.rb