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
  TYPE_EXTS[type] = extensions
  parents = [options[:parents]].flatten.compact
  TYPE_PARENTS[type] = parents unless parents.empty?
  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.



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

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



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

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.



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

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

.by_path(path) ⇒ Object

Lookup mime type by filename



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

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

.child?(child, parent) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.child?(child, parent)
  child == parent || TYPE_PARENTS[child]&.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
47
# 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 }
  TYPE_EXTS.delete(type)
  TYPE_PARENTS.delete(type)
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


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

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

#child_of?(parent) ⇒ Boolean

Returns true if type is child of parent type

Returns:

  • (Boolean)


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

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

#commentObject

Get mime comment



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

def comment
  nil # deprecated
end

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

Allow comparison with string

Returns:

  • (Boolean)


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

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

#extensionsObject

Get string list of file extensions



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

def extensions
  TYPE_EXTS[type] || []
end

#hashObject



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

def hash
  type.hash
end

#image?Boolean

Mediatype shortcuts

Returns:

  • (Boolean)


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

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

#text?Boolean

Returns true if type is a text format

Returns:

  • (Boolean)


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

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

#to_sObject

Return type as string



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

def to_s
  type
end

#video?Boolean

Returns:

  • (Boolean)


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

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