Mime Detection on Ruby
require 'ruby-filemagic'
require 'mime/types'
file_path = 'some_document.doc'
# auto detect file's file-type
fm = FileMagic.new(FileMagic::MAGIC_MIME)
mime_type = fm.file(file_path)
puts mime_type
> 'application/msword; content=binary;'
# detect extension by mime_type
file_extension = MIME::Types[mime_type][0].extensions[0]
puts file_extension
> 'doc'
Written by Alif Rachmawadi
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#