Class: OoxmlParser::OOXMLDocumentObject

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb

Constant Summary collapse

DEFAULT_DIRECTORY_FOR_MEDIA =
'/tmp'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.namespace_prefixObject

Returns the value of attribute namespace_prefix.



20
21
22
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 20

def namespace_prefix
  @namespace_prefix
end

.path_to_folderObject

Returns the value of attribute path_to_folder.



24
25
26
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 24

def path_to_folder
  @path_to_folder
end

.root_subfolderObject

Returns the value of attribute root_subfolder.



21
22
23
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 21

def root_subfolder
  @root_subfolder
end

.themeObject

Returns the value of attribute theme.



22
23
24
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 22

def theme
  @theme
end

.xmls_stackObject

Returns the value of attribute xmls_stack.



23
24
25
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 23

def xmls_stack
  @xmls_stack
end

Class Method Details

.add_to_xmls_stack(path) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 72

def add_to_xmls_stack(path)
  OOXMLDocumentObject.xmls_stack << if path.include?('..')
                                      "#{File.dirname(OOXMLDocumentObject.xmls_stack.last)}/#{path}"
                                    else
                                      path
                                    end
end

.copy_file_and_rename_to_zip(path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 40

def copy_file_and_rename_to_zip(path)
  file_name = File.basename(path)
  tmp_folder = "/tmp/office_open_xml_parser_#{SecureRandom.uuid}"
  file_path = "#{tmp_folder}/#{file_name}"
  FileUtils.rm_rf(tmp_folder) if File.directory?(tmp_folder)
  FileUtils.mkdir_p(tmp_folder)
  path = "#{Dir.pwd}/#{path}" unless path[0] == '/'
  raise "Cannot find file by path #{path}" unless File.exist?(path)
  FileUtils.cp path, tmp_folder
  file_path
end

.copy_media_file(path_to_file) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 101

def copy_media_file(path_to_file)
  folder_to_save_media = '/tmp/media_from_' + File.basename(OOXMLDocumentObject.path_to_folder)
  path_to_copied_file = folder_to_save_media + '/' + File.basename(path_to_file)
  full_path_to_file = OOXMLDocumentObject.path_to_folder + path_to_file
  unless File.exist?(full_path_to_file)
    warn "Couldn't find #{full_path_to_file} file on filesystem. Possible problem in original document"
    return nil
  end
  FileUtils.mkdir(folder_to_save_media) unless File.exist?(folder_to_save_media)
  FileUtils.copy_file(full_path_to_file, path_to_copied_file)
  path_to_copied_file
end

.current_xmlObject



68
69
70
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 68

def current_xml
  OOXMLDocumentObject.path_to_folder + OOXMLDocumentObject.xmls_stack.last
end

.dirObject



64
65
66
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 64

def dir
  OOXMLDocumentObject.path_to_folder + File.dirname(OOXMLDocumentObject.xmls_stack.last) + '/'
end

.encrypted_file?(path_to_file) ⇒ True, False

Returns Check if file is protected by password on open.

Parameters:

  • path_to_file (String)

    file

Returns:

  • (True, False)

    Check if file is protected by password on open



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 28

def encrypted_file?(path_to_file)
  file_result = `file "#{path_to_file}"`
  # Support of Encrtypted status in `file` util was introduced in file v5.20
  # but LTS version of ubuntu before 16.04 uses older `file` and it return `Composite Document`
  # https://github.com/file/file/blob/master/ChangeLog#L217
  if file_result.include?('Encrypted') || file_result.include?('Composite Document File V2 Document, No summary info')
    warn("File #{path_to_file} is encrypted. Can't parse it")
    return true
  end
  false
end

Raises:

  • (LoadError)


80
81
82
83
84
85
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 80

def get_link_from_rels(id)
  rels_path = dir + "_rels/#{File.basename(OOXMLDocumentObject.xmls_stack.last)}.rels"
  raise LoadError, "Cannot find .rels file by path: #{rels_path}" unless File.exist?(rels_path)
  relationships = XmlSimple.xml_in(File.open(rels_path))
  relationships['Relationship'].each { |relationship| return relationship['Target'] if id == relationship['Id'] }
end

.media_folderObject



87
88
89
90
91
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 87

def media_folder
  path = "#{DEFAULT_DIRECTORY_FOR_MEDIA}/media_from_#{@file_name}"
  FileUtils.mkdir(path) unless File.exist?(path)
  path + '/'
end

.option_enabled?(node, attribute_name = 'val') ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 93

def option_enabled?(node, attribute_name = 'val')
  return true if node.to_s == '1'
  return false if node.to_s == '0'
  return false if node.attribute(attribute_name).nil?
  status = node.attribute(attribute_name).value
  status == 'true' || status == 'on' || status == '1'
end

.unzip_file(path_to_file, destination) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 52

def unzip_file(path_to_file, destination)
  Zip.warn_invalid_date = false
  Zip::File.open(path_to_file) do |zip_file|
    raise LoadError, "There is no files in zip #{path_to_file}" if zip_file.entries.empty?
    zip_file.each do |file|
      file_path = File.join(destination, file.name)
      FileUtils.mkdir_p(File.dirname(file_path))
      zip_file.extract(file, file_path) unless File.exist?(file_path)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb', line 10

def ==(other)
  instance_variables.each do |current_attribute|
    unless instance_variable_get(current_attribute) == other.instance_variable_get(current_attribute)
      return false
    end
  end
  true
end