Class: Libis::Format::Fido

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

Constant Summary collapse

BAD_MIMETYPES =
[nil, '', 'None', 'application/octet-stream']

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_format(f) ⇒ Object



78
79
80
# File 'lib/libis/format/fido.rb', line 78

def self.add_format(f)
  instance.formats << f
end

.formats ⇒ Object



82
83
84
# File 'lib/libis/format/fido.rb', line 82

def self.formats
  instance.formats
end

.run(file, formats = nil) ⇒ Object



18
19
20
# File 'lib/libis/format/fido.rb', line 18

def self.run(file, formats = nil)
  self.new.run file, formats
end

Instance Method Details

#run(file, xtra_formats = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/libis/format/fido.rb', line 22

def run(file, xtra_formats = nil)

  fido_results = []

  fmt_list = formats.dup
  case xtra_formats
    when Array
      fmt_list += xtra_formats
    when String
      fmt_list << xtra_formats
    else
      # do nothing
  end

  args = []
  args << '-loadformats' << "#{fmt_list.join(',')}" unless fmt_list.empty?
  args << "#{file.escape_for_string}"
  fido = ::Libis::Tools::Command.run(Libis::Format::Config[:fido_path], *args)
  warn "Fido errors: #{fido[:err].join("\n")}" unless fido[:err].empty?

  keys = [:status, :time, :puid, :format_name, :signature_name, :filesize, :filename, :mimetype, :matchtype]
  fido_output = CSV.parse(fido[:out].join("\n")).map { |a| Hash[keys.zip(a)] }

  fido_output.each do |x|
    if x[:status] == 'OK'
      x[:mimetype] = get_mimetype(x[:puid]) if x[:mimetype] == 'None'
      next if BAD_MIMETYPES.include? x[:mimetype]
      x[:score] = 5
      case x[:matchtype]
        when 'signature'
          x[:score] += 5
        when 'container'
          typeinfo = ::Libis::Format::TypeDatabase.puid_typeinfo(x[:puid])
          ext = File.extname(file)
          x[:score] += 2 if typeinfo and typeinfo[:EXTENSIONS].include?(ext)
        else
          # do nothing
      end
      fido_results << x
    end
  end

  fido_results = fido_results.inject({}) do |result, value|
    result[value[:score]] ||= []
    result[value[:score]] << value
    result
  end

  max_score = fido_results.keys.max

  # Only if we find a single hit of type 'signature' or 'container', we are confident enough to return a result
  return {} unless max_score and max_score >= 5 && fido_results[max_score].size == 1

  fido_results[max_score].first
end