Class: Libis::Format::Identifier

Inherits:
Object
  • Object
show all
Includes:
Tools::Logger, Singleton
Defined in:
lib/libis/format/identifier.rb

Constant Summary collapse

RETRY_MIMETYPES =
%w(application/zip) + ::Libis::Format::Fido::BAD_MIMETYPES
FIDO_FAILURES =
%w(application/vnd.oasis.opendocument.text application/vnd.oasis.opendocument.spreadsheet)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#xml_validations ⇒ Object (readonly)

Returns the value of attribute xml_validations.



24
25
26
# File 'lib/libis/format/identifier.rb', line 24

def xml_validations
  @xml_validations
end

Class Method Details

.add_fido_format(f) ⇒ Object



50
51
52
# File 'lib/libis/format/identifier.rb', line 50

def self.add_fido_format(f)
  ::Libis::Format::Fido.add_format f
end

.add_xml_validation(mimetype, xsd_file) ⇒ Object



54
55
56
# File 'lib/libis/format/identifier.rb', line 54

def self.add_xml_validation(mimetype, xsd_file)
  instance.xml_validations[mimetype] = xsd_file
end

.get(file_path, options = nil) ⇒ Object



62
63
64
# File 'lib/libis/format/identifier.rb', line 62

def self.get(file_path, options = nil)
  instance.get file_path, options
end

.xml_validations ⇒ Object



58
59
60
# File 'lib/libis/format/identifier.rb', line 58

def self.xml_validations
  instance.xml_validations
end

Instance Method Details

#get(file, options = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/libis/format/identifier.rb', line 66

def get(file, options = nil)

  unless File.exists? file
    error 'File %s cannot be found.', file
    return nil
  end
  if File.directory? file
    error '%s is a directory.', file
    return nil
  end

  options ||= {}

  result = { messages: [] }

  # use FIDO
  # Note: FIDO does not always do a good job, mainly due to lacking container inspection.
  # FIDO misses should be registered in
  result = get_fido_identification(file, result, options[:formats]) unless options[:droid]

  # use DROID
  result = get_droid_identification file, result

  # use FILE
  result = get_file_identification(file, result)

  # Try file extension
  result = get_extension_identification(file, result)

  # determine XML type. Add custom types at runtime with
  # Libis::Tools::Format::Identifier.add_xml_validation('my_type', '/path/to/my_type.xsd')
  result = validate_against_xml_schema(file, result)

  result[:mimetype] ?
      log_msg(result, :info, "Identification of '#{file}': '#{result}'") :
      log_msg(result, :warn, "Could not identify MIME type of '#{file}'")
end

#get_droid_identification(file, result = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/libis/format/identifier.rb', line 117

def get_droid_identification(file, result = {})
  return result if result_ok? result, :DROID
  droid_output = ::Libis::Format::Droid.run file
  result[:messages] << [:debug, "DROID: #{droid_output}"]
  warn 'Droid found multiple matches; using first match only' if droid_output.size > 1
  result.clear
  droid_output = droid_output.first
  result[:mimetype] = droid_output[:mime_type].to_s.split(/[\s,]+/).find {|x| x =~ /.*\/.*/}
  result[:matchtype] = droid_output[:method]
  result[:puid] = droid_output[:puid]
  result[:format_name] = droid_output[:format_name]
  result[:format_version] = droid_output[:format_version]
  result[:method] = 'droid'

  log_msg(result, :debug, "Droid MIME-type: #{result[:mimetype]} (PRONOM UID: #{result[:puid]})")
end

#get_extension_identification(file, result = nil) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/libis/format/identifier.rb', line 151

def get_extension_identification(file, result = nil)
  return result if result_ok? result
  info = ::Libis::Format::TypeDatabase.ext_infos(File.extname(file)).first
  log_msg result, :debug, "File extension info: #{info}"
  if info
    result[:mimetype] = info[:MIME].first rescue nil
    result[:puid] = info[:PUID].first rescue nil
  end
  result[:method] = 'extension'
  result
end

#get_fido_identification(file, result = {}, xtra_formats = nil) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/libis/format/identifier.rb', line 104

def get_fido_identification(file, result = {}, xtra_formats = nil)
  return result if result_ok? result

  fido_result = ::Libis::Format::Fido.run(file, xtra_formats)

  return result unless fido_result.is_a? Hash

  result.merge! fido_result
  result[:method] = 'fido'

  log_msg(result, :debug, "Fido MIME-type: #{result[:mimetype]} (PRONOM UID: #{result[:puid]})")
end

#get_file_identification(file, result = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/libis/format/identifier.rb', line 134

def get_file_identification(file, result = nil)
  return result if result_ok? result
  begin
    output = ::Libis::Tools::Command.run('file', '-b', '--mime-type', "\"#{file.escape_for_string}\"")[:err]
    mimetype = output.strip.split
    if mimetype
      log_msg(result, :debug, "File result: '#{mimetype}'")
      result[:mimetype] = mimetype
      result[:puid] = get_puid(mimetype)
    end
    result[:method] = 'file'
  rescue Exception
    # ignored
  end
  result
end

#validate_against_xml_schema(file, result) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/libis/format/identifier.rb', line 163

def validate_against_xml_schema(file, result)
  return result unless result[:mimetype] =~ /^(text|application)\/xml$/
  doc = ::Libis::Tools::XmlDocument.open file
  xml_validations.each do |mime, xsd_file|
    next unless xsd_file
    if doc.validates_against?(xsd_file)
      log_msg result, :debug, "XML file validated against XML Schema: #{xsd_file}"
      result[:mimetype] = mime
      result[:puid] = nil
      result = ::Libis::Format::TypeDatabase.enrich(result, PUID: :puid, MIME: :mimetype)
    end
  end
  result
end