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.3.0"

Class Method Summary collapse

Class Method Details

.add_dots(array) ⇒ Object



131
132
133
134
135
# File 'lib/mimetype.rb', line 131

def add_dots(array)
  array.map do |e|
    ".#{e.gsub(%r!^\.!, '')}"
  end
end

.add_e(out) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/mimetype.rb', line 148

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



138
139
140
141
142
143
144
145
# File 'lib/mimetype.rb', line 138

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(v) ⇒ Struct

– Find by content type –

Parameters:

  • content_type (String)

    the content type

Returns:

  • (Struct)


125
126
127
128
# File 'lib/mimetype.rb', line 125

def lookup_by_content_type(v)
  v = v.content_type if v.is_a?(T)
  to_h[:c][v]
end

.lookup_by_extension(v) ⇒ Struct

– Find by file extension –

Parameters:

  • ext (String)

    the extension in “.” format

Returns:

  • (Struct)


115
116
117
118
# File 'lib/mimetype.rb', line 115

def lookup_by_extension(v)
  v = v.extension if v.is_a?(T)
  to_h[:e][v]
end

.lookup_by_filename(v) ⇒ MimeType::MimeType

– Find by path –

Parameters:

  • path (String, Pathutil, Pathname, File)

    the path

Returns:

  • (MimeType::MimeType)


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

def lookup_by_filename(v)
  v = File.extname(v)
  to_h[:e][v]
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 –

Parameters:

  • file (String, File) (defaults to: "mime.types")

    the path

Returns:

  • (Hash<String,Hash<Symbol,String>>)


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_hHash<Symbol,Hash<String,Struct>>

– List of mime_types –

Returns:

  • (Hash<Symbol,Hash<String,Struct>>)


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_jsonHash<Symbol,Hash<String,Struct>>

Note:

makes it a double hash for fast parsing

– Transform the mimetype.json file –

Returns:

  • (Hash<Symbol,Hash<String,Struct>>)


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