class Object

Public Instance Methods

add_dots(array) click to toggle source

# File lib/mimetype.rb, line 131
def add_dots(array)
  array.map do |e|
    ".#{e.gsub(%r!^\.!, '')}"
  end
end
add_e(out) click to toggle source

# 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) click to toggle source

# 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) click to toggle source

– Find by content type @param content_type [String] the content type @return [Struct] –

# 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) click to toggle source

– Find by file extension @param ext [String] the extension in “.” format @return [Struct] –

# 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) click to toggle source

– @return [MimeType::MimeType] @param path [String, Pathutil, Pathname, File] the path Find by path –

# 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") click to toggle source

– @return [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 @param file [String, File] the path –

# 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() click to toggle source

– @return [Hash<Symbol,Hash<String,Struct>>] List of mime_types –

# 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() click to toggle source

– @return [Hash<Symbol,Hash<String,Struct>>] @note makes it a double hash for fast parsing Transform the mimetype.json file –

# 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