Class: Marcel::Magic

Inherits:
Object
  • Object
show all
Defined in:
lib/marcel/magic.rb

Overview

Mime type detection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ Magic

Mime type by type string



16
17
18
19
# File 'lib/marcel/magic.rb', line 16

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

Instance Attribute Details

#mediatypeObject (readonly)

Returns the value of attribute mediatype.



13
14
15
# File 'lib/marcel/magic.rb', line 13

def mediatype
  @mediatype
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



13
14
15
# File 'lib/marcel/magic.rb', line 13

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



13
14
15
# File 'lib/marcel/magic.rb', line 13

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



30
31
32
33
34
35
36
37
# File 'lib/marcel/magic.rb', line 30

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.



92
93
94
# File 'lib/marcel/magic.rb', line 92

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



72
73
74
75
76
# File 'lib/marcel/magic.rb', line 72

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.



85
86
87
88
# File 'lib/marcel/magic.rb', line 85

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

.by_path(path) ⇒ Object

Lookup mime type by filename



79
80
81
# File 'lib/marcel/magic.rb', line 79

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

.child?(child, parent) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/marcel/magic.rb', line 112

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.



42
43
44
45
46
# File 'lib/marcel/magic.rb', line 42

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
# File 'lib/marcel/magic.rb', line 53

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

#child_of?(parent) ⇒ Boolean

Returns true if type is child of parent type

Returns:

  • (Boolean)


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

def child_of?(parent)
  self.class.child?(type, parent)
end

#commentObject

Get mime comment



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

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

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

Allow comparison with string

Returns:

  • (Boolean)


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

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

#extensionsObject

Get string list of file extensions



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

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

#hashObject



106
107
108
# File 'lib/marcel/magic.rb', line 106

def hash
  type.hash
end

#image?Boolean

Mediatype shortcuts

Returns:

  • (Boolean)


52
# File 'lib/marcel/magic.rb', line 52

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

#text?Boolean

Returns true if type is a text format

Returns:

  • (Boolean)


49
# File 'lib/marcel/magic.rb', line 49

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

#to_sObject

Return type as string



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

def to_s
  type
end

#video?Boolean

Returns:

  • (Boolean)


54
# File 'lib/marcel/magic.rb', line 54

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