Class: MimeMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/mimemagic.rb,
lib/mimemagic/tables.rb,
lib/mimemagic/version.rb

Overview

Generated from freedesktop.org.xml

Constant Summary collapse

VERSION =

MimeMagic version string

'0.3.6'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ MimeMagic

Mime type by type string



11
12
13
14
# File 'lib/mimemagic.rb', line 11

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

Instance Attribute Details

#mediatypeObject (readonly)

Returns the value of attribute mediatype.



8
9
10
# File 'lib/mimemagic.rb', line 8

def mediatype
  @mediatype
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



8
9
10
# File 'lib/mimemagic.rb', line 8

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/mimemagic.rb', line 8

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



25
26
27
28
29
30
31
32
# File 'lib/mimemagic.rb', line 25

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

.all_by_magic(io) ⇒ Object

Lookup all mime types by magic content analysis. This is a slower operation.



97
98
99
# File 'lib/mimemagic.rb', line 97

def self.all_by_magic(io)
  magic_match(io, :select).map { |mime| new(mime[0]) }
end

.by_extension(ext) ⇒ Object

Lookup mime type by file extension



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

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.



90
91
92
93
# File 'lib/mimemagic.rb', line 90

def self.by_magic(io)
  mime = magic_match(io, :find)
  mime && new(mime[0])
end

.by_path(path) ⇒ Object

Lookup mime type by filename



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

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

.child?(child, parent) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/mimemagic.rb', line 117

def self.child?(child, parent)
  child == parent || TYPES.key?(child) && TYPES[child][1].any? { |p| child?(p, parent) }
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.



37
38
39
40
41
# File 'lib/mimemagic.rb', line 37

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

#audio?Boolean

Returns:

  • (Boolean)


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

def audio?
  mediatype == 'audio'
end

#child_of?(parent) ⇒ Boolean

Returns true if type is child of parent type

Returns:

  • (Boolean)


62
63
64
# File 'lib/mimemagic.rb', line 62

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

#commentObject

Get mime comment



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

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

#eql?(other) ⇒ Boolean Also known as: ==

Allow comparison with string

Returns:

  • (Boolean)


107
108
109
# File 'lib/mimemagic.rb', line 107

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

#extensionsObject

Get string list of file extensions



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

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

#hashObject



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

def hash
  type.hash
end

#image?Boolean

Mediatype shortcuts

Returns:

  • (Boolean)


49
50
51
# File 'lib/mimemagic.rb', line 49

def image?
  mediatype == 'image'
end

#text?Boolean

Returns true if type is a text format

Returns:

  • (Boolean)


44
45
46
# File 'lib/mimemagic.rb', line 44

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

#to_sObject

Return type as string



102
103
104
# File 'lib/mimemagic.rb', line 102

def to_s
  type
end

#video?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/mimemagic.rb', line 57

def video?
  mediatype == 'video'
end