Module MIME
In: lib/shared-mime-info.rb

shared-mime-info is a pure Ruby library for accessing the MIME info database provided by Freedesktop on Standards/shared-mime-info-spec.

This provides a way to guess the mime type of a file by doing both filename lookups and magic file checks. This implementation tries to follow the version 0.13 of the specification.

Methods

[]   check   check_globs   check_magics  

Classes and Modules

Class MIME::Type

Constants

VERSION = '0.1'

Public Class methods

Returns the MIME::Type object corresponding to type.

[Source]

# File lib/shared-mime-info.rb, line 241
    def [](type)
      @types.fetch type, nil
    end

Look for the type of a file by doing successive checks with the filename patterns or magic numbers. If none of the matches are successful, returns a type of application/octet-stream if the file contains control characters at its beginning, or text/plain otherwise.

Returns a MIME::Type object.

[Source]

# File lib/shared-mime-info.rb, line 281
    def check(filename)
      check_special(filename) ||
      open(filename) { |f|
        check_magics_with_priority(f, 80) ||
        check_globs(filename) ||
        check_magics_with_priority(f, 0) ||
        check_default(f)
      }
    end

Look for the type of a file by doing successive checks on the filename patterns.

Returns a MIME::Type object or nil if nothing matches.

[Source]

# File lib/shared-mime-info.rb, line 249
    def check_globs(filename)
      enum = Enumerable::Enumerator.new(@globs, :each_key)
      found = enum.select { |pattern| File.fnmatch pattern, filename }

      if found.empty?
        downcase_filename = filename.downcase
        found = enum.select { |pattern|
          File.fnmatch pattern, downcase_filename
        }
      end

      @globs[found.max]
    end

Look for the type of a file by doing successive checks on magic numbers.

Returns a MIME::Type object or nil if nothing matches.

[Source]

# File lib/shared-mime-info.rb, line 267
    def check_magics(file)
      if file.respond_to? :read
        check_magics_with_priority(file, 0)
      else
        open(file) {|f| check_magics_with_priority(f, 0) }
      end
    end

[Validate]