Detect MIME types by extension, path, or content
masterMimeMagic provides several methods to identify file types. You can detect types based on a file extension, a file path, or by inspecting the file's actual content (magic bytes).
MimeMagic.by_extension(ext): Detects type using an extension string (e.g.,'html'or'.html').MimeMagic.by_path(path): Detects type by looking at a file on disk.MimeMagic.by_magic(io): Detects type by reading the content of an IO object (like aFilehandle).
require 'mimemagic'
# By extension
MimeMagic.by_extension('html').text?
MimeMagic.by_extension('.html').child_of? 'text/plain'
# By path
MimeMagic.by_path('filename.txt')
# By content (magic bytes)
MimeMagic.by_magic(File.open('test.html'))