Module: MimeType
- Defined in:
- lib/mimetype/version.rb,
lib/mimetype.rb
Overview
Copyright: 2018 - MIT License Encoding: utf-8
Defined Under Namespace
Classes: T
Constant Summary collapse
- JSON =
Pathutil.new(__dir__).join("mimetype.json")
- KEYS =
i(content_type content_types extension extensions).freeze
- STRING_KEYS =
%w(content_type content_types extension extensions).freeze
- PACK =
Pathutil.new(__dir__).join("mimetype.msgpack")
- VERSION =
"1.2.0"
Class Method Summary collapse
-
.add_dots(array) ⇒ Object
–.
-
.add_e(out) ⇒ Object
–.
-
.add_t(out) ⇒ Object
–.
-
.lookup_by_content_type(content_type) ⇒ Struct
– Find by content type –.
-
.lookup_by_extension(extension) ⇒ Struct
– Find by file extension –.
-
.lookup_by_filename(path) ⇒ MimeType::MimeType
– Find by path –.
-
.parse_etc_mimetypes(file = "mime.types") ⇒ Hash<String,Hash<Symbol,String>>
– Convert a mime file (read: ‘/etc/mime.types`) into JSON –.
-
.to_h ⇒ Hash<Symbol,Hash<String,Struct>>
– List of mime_types –.
-
.transform_mimetype_json ⇒ Hash<Symbol,Hash<String,Struct>>
– Transform the mimetype.json file –.
Class Method Details
.add_dots(array) ⇒ Object
–
129 130 131 132 133 |
# File 'lib/mimetype.rb', line 129 def add_dots(array) array.map do |e| ".#{e.gsub(%r!^\.!, '')}" end end |
.add_e(out) ⇒ Object
–
146 147 148 149 150 151 152 |
# File 'lib/mimetype.rb', line 146 def add_e(out) out[:e] = out[:c].each_with_object({}) do |(_, v), h| v.extensions.each do |e| h[e] = v end end end |
.add_t(out) ⇒ Object
–
136 137 138 139 140 141 142 143 |
# File 'lib/mimetype.rb', line 136 def add_t(out) out[:c].keys.each do |k| next unless out[:c][k].content_types.size > 1 out[:c][k].content_types.each do |nk| out[:c][nk] = out[:c][k] end end end |
.lookup_by_content_type(content_type) ⇒ Struct
– Find by content type –
124 125 126 |
# File 'lib/mimetype.rb', line 124 def lookup_by_content_type(content_type) to_h[:c][content_type] end |
.lookup_by_extension(extension) ⇒ Struct
– Find by file extension –
115 116 117 |
# File 'lib/mimetype.rb', line 115 def lookup_by_extension(extension) to_h[:e][extension] end |
.lookup_by_filename(path) ⇒ MimeType::MimeType
– Find by path –
105 106 107 108 |
# File 'lib/mimetype.rb', line 105 def lookup_by_filename(path) ext = File.extname(path) to_h[:e][ext] end |
.parse_etc_mimetypes(file = "mime.types") ⇒ Hash<String,Hash<Symbol,String>>
Note:
content_types is mostly used by us, when they are updated
– Convert a mime file (read: ‘/etc/mime.types`) into JSON –
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/mimetype.rb', line 53 def parse_etc_mimetypes(file = "mime.types") out = Pathutil.new(file).each_line.with_object({}) do |v, h| v = v.split(%r!\s+!) next if v.size < 2 || v[0] =~ %r!^#! k, v = v[0], add_dots(v[1..-1]) h[k] = { content_type: k, content_types: [], extension: v[0], extensions: v, } end Hash[out.sort_by do |k, _| k end] end |
.to_h ⇒ Hash<Symbol,Hash<String,Struct>>
– List of mime_types –
94 95 96 97 98 |
# File 'lib/mimetype.rb', line 94 def to_h return @to_h if @to_h msgp = Pathutil.new(__dir__).join("mimetype.msgpack").read @to_h = MessagePack.unpack(msgp) end |
.transform_mimetype_json ⇒ Hash<Symbol,Hash<String,Struct>>
Note:
makes it a double hash for fast parsing
– Transform the mimetype.json file –
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/mimetype.rb', line 77 def transform_mimetype_json out = {} out[:c] = JSON.read_json out[:c].each do |k, v| out[:c][k] = T.new(*v.values_at(*STRING_KEYS)) end add_t(out) add_e(out) out end |