Class: MimeMagic
- 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
-
#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.
-
.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.
-
.remove(type) ⇒ Object
Removes a mime type from the dictionary.
Instance Method Summary collapse
-
#==(x) ⇒ Object
Allow comparison with string.
- #audio? ⇒ Boolean
-
#child_of?(parent) ⇒ Boolean
Returns true if type is child of parent type.
-
#comment ⇒ Object
Get mime comment.
-
#eql?(other) ⇒ Boolean
allow comparison with something else.
-
#ext ⇒ Object
Default extension.
-
#extensions ⇒ Object
Get string list of file extensions.
-
#hash ⇒ Object
allow comparison with hashes.
-
#image? ⇒ Boolean
Mediatype shortcuts.
-
#initialize(type) ⇒ MimeMagic
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
Instance Attribute Details
#mediatype ⇒ Object (readonly)
Returns the value of attribute mediatype.
24 25 26 |
# File 'lib/epitools/mimemagic.rb', line 24 def mediatype @mediatype end |
#subtype ⇒ Object (readonly)
Returns the value of attribute subtype.
24 25 26 |
# File 'lib/epitools/mimemagic.rb', line 24 def subtype @subtype 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, ) extensions = [[:extensions]].flatten.compact TYPES[type] = [extensions, [[:parents]].flatten.compact, [:comment]] extensions.each {|ext| EXTENSIONS[ext] = type } MAGIC.unshift [type, [:magic]] if [: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
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
68 69 70 |
# File 'lib/epitools/mimemagic.rb', line 68 def child_of?(parent) MimeMagic.child?(type, parent) end |
#comment ⇒ Object
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
125 126 127 |
# File 'lib/epitools/mimemagic.rb', line 125 def eql?(other) self.type == other.type end |
#ext ⇒ Object
Default extension
78 79 80 |
# File 'lib/epitools/mimemagic.rb', line 78 def ext extensions.first end |
#extensions ⇒ Object
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 |
#hash ⇒ Object
allow comparison with hashes
120 121 122 |
# File 'lib/epitools/mimemagic.rb', line 120 def hash type.hash end |
#image? ⇒ Boolean
Mediatype shortcuts
63 |
# File 'lib/epitools/mimemagic.rb', line 63 def image?; mediatype == 'image'; end |
#text? ⇒ Boolean
Returns true if type is a text format
60 |
# File 'lib/epitools/mimemagic.rb', line 60 def text?; mediatype == 'text' || child_of?('text/plain'); end |
#to_s ⇒ Object
Return type as string
110 111 112 |
# File 'lib/epitools/mimemagic.rb', line 110 def to_s type end |
#video? ⇒ Boolean
65 |
# File 'lib/epitools/mimemagic.rb', line 65 def video?; mediatype == 'video'; end |