Class: Marcel::Magic
- Inherits:
-
Object
- Object
- Marcel::Magic
- Defined in:
- lib/marcel/magic.rb
Overview
Mime type detection
Instance Attribute Summary collapse
-
#mediatype ⇒ Object
readonly
Returns the value of attribute mediatype.
-
#subtype ⇒ Object
readonly
Returns the value of attribute subtype.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
-
.add(type, options) ⇒ Object
Add custom mime type.
-
.all_by_magic(io) ⇒ Object
Lookup all mime types by magic content analysis.
-
.by_extension(ext) ⇒ Object
Lookup mime type by file extension.
-
.by_magic(io) ⇒ Object
Lookup mime type by magic content analysis.
-
.by_path(path) ⇒ Object
Lookup mime type by filename.
- .child?(child, parent) ⇒ Boolean
-
.remove(type) ⇒ Object
Removes a mime type from the dictionary.
Instance Method Summary collapse
- #audio? ⇒ Boolean
-
#child_of?(parent) ⇒ Boolean
Returns true if type is child of parent type.
-
#comment ⇒ Object
Get mime comment.
-
#eql?(other) ⇒ Boolean
(also: #==)
Allow comparison with string.
-
#extensions ⇒ Object
Get string list of file extensions.
- #hash ⇒ Object
-
#image? ⇒ Boolean
Mediatype shortcuts.
-
#initialize(type) ⇒ Magic
constructor
Mime type by type string.
-
#text? ⇒ Boolean
Returns true if type is a text format.
-
#to_s ⇒ Object
Return type as string.
- #video? ⇒ Boolean
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
#mediatype ⇒ Object (readonly)
Returns the value of attribute mediatype.
13 14 15 |
# File 'lib/marcel/magic.rb', line 13 def mediatype @mediatype end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
13 14 15 |
# File 'lib/marcel/magic.rb', line 13 def subtype @subtype end |
#type ⇒ Object (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, ) extensions = [[:extensions]].flatten.compact TYPE_EXTS[type] = extensions parents = [[:parents]].flatten.compact TYPE_PARENTS[type] = parents unless parents.empty? extensions.each {|ext| EXTENSIONS[ext] = type } MAGIC.unshift [type, [:magic]] if [: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
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
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
58 59 60 |
# File 'lib/marcel/magic.rb', line 58 def child_of?(parent) self.class.child?(type, parent) end |
#comment ⇒ Object
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
103 104 105 |
# File 'lib/marcel/magic.rb', line 103 def eql?(other) type == other.to_s end |
#extensions ⇒ Object
Get string list of file extensions
63 64 65 |
# File 'lib/marcel/magic.rb', line 63 def extensions TYPE_EXTS[type] || [] end |
#hash ⇒ Object
107 108 109 |
# File 'lib/marcel/magic.rb', line 107 def hash type.hash end |
#image? ⇒ Boolean
Mediatype shortcuts
53 |
# File 'lib/marcel/magic.rb', line 53 def image?; mediatype == 'image'; end |
#text? ⇒ Boolean
Returns true if type is a text format
50 |
# File 'lib/marcel/magic.rb', line 50 def text?; mediatype == 'text' || child_of?('text/plain'); end |
#to_s ⇒ Object
Return type as string
98 99 100 |
# File 'lib/marcel/magic.rb', line 98 def to_s type end |
#video? ⇒ Boolean
55 |
# File 'lib/marcel/magic.rb', line 55 def video?; mediatype == 'video'; end |