Class: MimeMagic

Inherits:
Object show all
Defined in:
lib/epitools/mimemagic.rb,
lib/epitools/mimemagic_tables.rb

Overview

TODO:

Order by popularity, to speed up the MAGIC.find command.

Constant Summary collapse

VERSION =
'0.1.8b'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ MimeMagic

Mime type by type string


27
28
29
30
# File 'lib/epitools/mimemagic.rb', line 27

def initialize(type)
  @type = type
  @mediatype, @subtype = type.split('/', 2)
end

Instance Attribute Details

#mediatypeObject (readonly)

Returns the value of attribute mediatype


24
25
26
# File 'lib/epitools/mimemagic.rb', line 24

def mediatype
  @mediatype
end

#subtypeObject (readonly)

Returns the value of attribute subtype


24
25
26
# File 'lib/epitools/mimemagic.rb', line 24

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type


24
25
26
# File 'lib/epitools/mimemagic.rb', line 24

def type
  @type
end

Class Method Details

.add(type, options) ⇒ Object

Add custom mime type. Arguments:

  • type: Mime type

  • options: Options hash

Option keys:

  • :extensions: String list or single string of file extensions

  • :parents: String list or single string of parent mime types

  • :magic: Mime magic specification

  • :comment: Comment string


41
42
43
44
45
46
47
48
# File 'lib/epitools/mimemagic.rb', line 41

def self.add(type, options)
  extensions = [options[:extensions]].flatten.compact
  TYPES[type] = [extensions,
                [options[:parents]].flatten.compact,
                options[:comment]]
  extensions.each {|ext| EXTENSIONS[ext] = type }
  MAGIC.unshift [type, options[:magic]] if options[:magic]
end

.by_extension(ext) ⇒ Object

Lookup mime type by file extension


88
89
90
91
92
# File 'lib/epitools/mimemagic.rb', line 88

def self.by_extension(ext)
  ext = ext.to_s.downcase
  mime = ext[0..0] == '.' ? EXTENSIONS[ext[1..-1]] : EXTENSIONS[ext]
  mime && new(mime)
end

.by_magic(io) ⇒ Object

Lookup mime type by magic content analysis. This is a slow operation.


101
102
103
104
105
106
107
# File 'lib/epitools/mimemagic.rb', line 101

def self.by_magic(io)
  if !(io.respond_to?(:seek) && io.respond_to?(:read))
    io = StringIO.new(io.to_s, 'rb:binary')
  end
  mime = MAGIC.find {|type, matches| magic_match(io, matches) }
  mime && new(mime[0])
end

.by_path(path) ⇒ Object

Lookup mime type by filename


95
96
97
# File 'lib/epitools/mimemagic.rb', line 95

def self.by_path(path)
  by_extension(File.extname(path))
end

.remove(type) ⇒ Object

Removes a mime type from the dictionary. You might want to do this if you're seeing impossible conflicts (for instance, application/x-gmc-link).

  • type: The mime type to remove. All associated extensions and magic are removed too.


53
54
55
56
57
# File 'lib/epitools/mimemagic.rb', line 53

def self.remove(type)
  EXTENSIONS.delete_if {|ext, t| t == type }
  MAGIC.delete_if {|t, m| t == type }
  TYPES.delete(type)
end

Instance Method Details

#==(x) ⇒ Object

Allow comparison with string


115
116
117
# File 'lib/epitools/mimemagic.rb', line 115

def ==(x)
  type == x.to_s
end

#audio?Boolean

Returns:

  • (Boolean)

64
# File 'lib/epitools/mimemagic.rb', line 64

def audio?; mediatype == 'audio'; end

#child_of?(parent) ⇒ Boolean

Returns true if type is child of parent type

Returns:

  • (Boolean)

68
69
70
# File 'lib/epitools/mimemagic.rb', line 68

def child_of?(parent)
  MimeMagic.child?(type, parent)
end

#commentObject

Get mime comment


83
84
85
# File 'lib/epitools/mimemagic.rb', line 83

def comment
  (TYPES.key?(type) ? TYPES[type][2] : nil).to_s
end

#eql?(other) ⇒ Boolean

allow comparison with something else

Returns:

  • (Boolean)

125
126
127
# File 'lib/epitools/mimemagic.rb', line 125

def eql?(other)
  self.type == other.type
end

#extObject

Default extension


78
79
80
# File 'lib/epitools/mimemagic.rb', line 78

def ext
  extensions.first
end

#extensionsObject

Get string list of file extensions


73
74
75
# File 'lib/epitools/mimemagic.rb', line 73

def extensions
  TYPES.key?(type) ? TYPES[type][0] : []
end

#hashObject

allow comparison with hashes


120
121
122
# File 'lib/epitools/mimemagic.rb', line 120

def hash
  type.hash
end

#image?Boolean

Mediatype shortcuts

Returns:

  • (Boolean)

63
# File 'lib/epitools/mimemagic.rb', line 63

def image?; mediatype == 'image'; end

#text?Boolean

Returns true if type is a text format

Returns:

  • (Boolean)

60
# File 'lib/epitools/mimemagic.rb', line 60

def text?; mediatype == 'text' || child_of?('text/plain'); end

#to_sObject

Return type as string


110
111
112
# File 'lib/epitools/mimemagic.rb', line 110

def to_s
  type
end

#video?Boolean

Returns:

  • (Boolean)

65
# File 'lib/epitools/mimemagic.rb', line 65

def video?; mediatype == 'video'; end