module Poleica::Typeable

Retrieve the mimetype, the extension and the type of a file, it needs a “path” method

Constants

MIMETYPE_EXTRACT_REGEX

Public Instance Methods

file_extension() click to toggle source
# File lib/poleica/types/typeable.rb, line 5
def file_extension
  @file_extension ||= extract_extension
end
file_mimetype() click to toggle source
# File lib/poleica/types/typeable.rb, line 9
def file_mimetype
  @file_mimetype ||= extract_mimetype
end
file_type() click to toggle source
# File lib/poleica/types/typeable.rb, line 13
def file_type
  @file_type ||= (detect_type_with_extension ||
                  detect_type_with_mimetype  ||
                  Types::General).new(path)
end

Private Instance Methods

detect_type_with_extension() click to toggle source
# File lib/poleica/types/typeable.rb, line 31
def detect_type_with_extension
  Types::All.find do |type|
    type::COMPATIBLE_EXTENSIONS.include?(file_extension)
  end
end
detect_type_with_mimetype() click to toggle source
# File lib/poleica/types/typeable.rb, line 37
def detect_type_with_mimetype
  Types::All.find do |type|
    type::COMPATIBLE_MIMETYPES.include?(file_mimetype)
  end
end
extract_extension() click to toggle source
# File lib/poleica/types/typeable.rb, line 27
def extract_extension
  (::File.extname(path)[1..-1] || '').strip
end
extract_mimetype() click to toggle source
# File lib/poleica/types/typeable.rb, line 23
def extract_mimetype
  `file -b --mime '#{path}'`.strip[MIMETYPE_EXTRACT_REGEX] || ''
end