Class: MimeMagic

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

Overview

Mime type detection

Constant Summary collapse

EXTENSIONS =
{}
TYPES =
{}
MAGIC =
[]
VERSION =

MimeMagic version string

'0.4.3'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type) ⇒ MimeMagic

Mime type by type string



15
16
17
18
# File 'lib/mimemagic.rb', line 15

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

Instance Attribute Details

#mediatypeObject (readonly)

Returns the value of attribute mediatype.



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

def mediatype
  @mediatype
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



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

def subtype
  @subtype
end

#typeObject (readonly)

Returns the value of attribute type.



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

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


29
30
31
32
33
34
35
36
# File 'lib/mimemagic.rb', line 29

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.



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

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



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

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.



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

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

.by_path(path) ⇒ Object

Lookup mime type by filename



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

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

.child?(child, parent) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.child?(child, parent)
  child == parent || TYPES.key?(child) && TYPES[child][1].any? {|p| child?(p, parent) }
end

.get_matches(parent) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mimemagic/tables.rb', line 18

def self.get_matches(parent)
  parent.elements.map {|match|
    if match['mask']
      nil
    else
      type = match['type']
      value = match['value']
      offset = match['offset'].split(':').map {|x| x.to_i }
      offset = offset.size == 2 ? offset[0]..offset[1] : offset[0]
      case type
      when 'string'
        # This *one* pattern match, in the entirety of fd.o's mime types blows up the parser
        # because of the escape character \c, so right here we have a hideous hack to
        # accommodate that.
        if value == '\chapter'
          '\chapter'
        else
          value.gsub!(/\\(x[\dA-Fa-f]{1,2}|0\d{1,3}|\d{1,3}|.)/) {
            eval("\"\\#{$1}\"")
          }
        end
      when 'big16'
        value = str2int(value)
        value = ((value >> 8).chr + (value & 0xFF).chr)
      when 'big32'
        value = str2int(value)
        value = (((value >> 24) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 8) & 0xFF).chr + (value & 0xFF).chr)
      when 'little16'
        value = str2int(value)
        value = ((value & 0xFF).chr + (value >> 8).chr)
      when 'little32'
        value = str2int(value)
        value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
      when 'host16' # use little endian
        value = str2int(value)
        value = ((value & 0xFF).chr + (value >> 8).chr)
      when 'host32' # use little endian
        value = str2int(value)
        value = ((value & 0xFF).chr + ((value >> 8) & 0xFF).chr + ((value >> 16) & 0xFF).chr + ((value >> 24) & 0xFF).chr)
      when 'byte'
        value = str2int(value)
        value = value.chr
      end
      children = get_matches(match)
      children.empty? ? [offset, value] : [offset, value, children]
    end
  }.compact
end

.open_mime_databaseObject



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

def self.open_mime_database
  path = MimeMagic::DATABASE_PATH
  File.open(path)
end

.parse_databaseObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/mimemagic/tables.rb', line 72

def self.parse_database
  file = open_mime_database

  doc = Nokogiri::XML(file)
  extensions = {}
  types = {}
  magics = []
  (doc/'mime-info/mime-type').each do |mime|
    comments = Hash[*(mime/'comment').map {|comment| [comment['xml:lang'], comment.inner_text] }.flatten]
    type = mime['type']
    subclass = (mime/'sub-class-of').map{|x| x['type']}
    exts = (mime/'glob').map{|x| x['pattern'] =~ /^\*\.([^\[\]]+)$/ ? $1.downcase : nil }.compact
    (mime/'magic').each do |magic|
      priority = magic['priority'].to_i
      matches = get_matches(magic)
      magics << [priority, type, matches]
    end
    if !exts.empty?
      exts.each{|x|
        extensions[x] = type if !extensions.include?(x)
      }
      types[type] = [exts,subclass,comments[nil]]
    end
  end

  magics = magics.sort {|a,b| [-a[0],a[1]] <=> [-b[0],b[1]] }

  common_types = [
    "image/jpeg",                                                              # .jpg
    "image/png",                                                               # .png
    "image/gif",                                                               # .gif
    "image/tiff",                                                              # .tiff
    "image/bmp",                                                               # .bmp
    "image/vnd.adobe.photoshop",                                               # .psd
    "image/webp",                                                              # .webp
    "image/svg+xml",                                                           # .svg

    "video/x-msvideo",                                                         # .avi
    "video/x-ms-wmv",                                                          # .wmv
    "video/mp4",                                                               # .mp4, .m4v
    "video/quicktime",                                                         # .mov
    "video/mpeg",                                                              # .mpeg
    "video/ogg",                                                               # .ogv
    "video/webm",                                                              # .webm
    "video/x-matroska",                                                        # .mkv
    "video/x-flv",                                                             # .flv

    "audio/mpeg",                                                              # .mp3
    "audio/x-wav",                                                             # .wav
    "audio/aac",                                                               # .aac
    "audio/flac",                                                              # .flac
    "audio/mp4",                                                               # .m4a
    "audio/ogg",                                                               # .ogg

    "application/pdf",                                                         # .pdf
    "application/msword",                                                      # .doc
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document", # .docx
    "application/vnd.ms-powerpoint",                                           # .pps
    "application/vnd.openxmlformats-officedocument.presentationml.slideshow",  # .ppsx
    "application/vnd.ms-excel",                                                # .pps
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",       # .ppsx
  ]

  common_magics = common_types.map do |common_type|
    magics.find { |_, type, _| type == common_type }
  end

  magics = (common_magics.compact + magics).uniq

  extensions.keys.sort.each do |key|
    EXTENSIONS[key] = extensions[key]
  end
  types.keys.sort.each do |key|
    exts = types[key][0]
    parents = types[key][1].sort
    comment = types[key][2]

    TYPES[key] = [exts, parents, comment]
  end
  magics.each do |priority, type, matches|
    MAGIC << [type, matches]
  end
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.


41
42
43
44
45
# File 'lib/mimemagic.rb', line 41

def self.remove(type)
  EXTENSIONS.delete_if {|ext, t| t == type }
  MAGIC.delete_if {|t, m| t == type }
  TYPES.delete(type)
end

.str2int(s) ⇒ Object



12
13
14
15
16
# File 'lib/mimemagic/tables.rb', line 12

def self.str2int(s)
  return s.to_i(16) if s[0..1].downcase == '0x'
  return s.to_i(8) if s[0..0].downcase == '0'
  s.to_i(10)
end

Instance Method Details

#audio?Boolean

Returns:

  • (Boolean)


52
# File 'lib/mimemagic.rb', line 52

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

#child_of?(parent) ⇒ Boolean

Returns true if type is child of parent type

Returns:

  • (Boolean)


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

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

#commentObject

Get mime comment



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

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

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

Allow comparison with string

Returns:

  • (Boolean)


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

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

#extensionsObject

Get string list of file extensions



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

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

#hashObject



105
106
107
# File 'lib/mimemagic.rb', line 105

def hash
  type.hash
end

#image?Boolean

Mediatype shortcuts

Returns:

  • (Boolean)


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

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

#text?Boolean

Returns true if type is a text format

Returns:

  • (Boolean)


48
# File 'lib/mimemagic.rb', line 48

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

#to_sObject

Return type as string



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

def to_s
  type
end

#video?Boolean

Returns:

  • (Boolean)


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

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