module Headmin class ThumbnailView def initialize(local_assigns) @local_assigns = local_assigns end def class_names class_names = [@local_assigns[:class]] class_names << "img-thumbnail h-thumbnail" class_names.join(" ") end def width @local_assigns[:width] || 150 end def height @local_assigns[:height] || 150 end def src @local_assigns[:src] end def icon @local_assigns[:icon] end def mime_type if src stripped_path = URI.parse(src).path extension = File.extname(stripped_path) Rack::Mime.mime_type(extension) end end def image? mime_type&.match?(/^image/) end # https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types def icon_name return icon if icon type_map = { image: %w[image/bmp image/gif image/vnd.microsoft.icon image/jpeg image/png image/svg+xml image/tiff image/webp], play: %w[video/mp4 video/mpeg video/ogg video/mp2t video/webm video/3gpp video/3gpp2], music: %w[audio/aac audio/midi audio/x-midi audio/mpeg audio/ogg audio/opus audio/wav audio/webm audio/3gpp audio/3gpp2], word: %w[application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document], ppt: %w[application/vnd.ms-powerpoint application/vnd.openxmlformats-officedocument.presentationml.presentation], excel: %w[application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet], slides: %w[application/vnd.oasis.opendocument.presentation], spreadsheet: %w[application/vnd.oasis.opendocument.spreadsheet], richtext: %w[application/vnd.oasis.opendocument.text], zip: %w[application/zip application/x-7z-compressed application/x-bzip application/x-bzip2 application/gzip application/vnd.rar], pdf: %w[application/pdf] } type = type_map.find { |key, mime_types| mime_types.include?(mime_type) } if type ["file", "earmark", type.first].compact.join("-") else "question" end end end end