Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine file type in Ruby

There is a ruby binding to libmagic that does what you need. It is available as a gem named ruby-filemagic:

gem install ruby-filemagic

Require libmagic-dev.

The documentation seems a little thin, but this should get you started:

$ irb 
irb(main):001:0> require 'filemagic' 
=> true
irb(main):002:0> fm = FileMagic.new
=> #<FileMagic:0x7fd4afb0>
irb(main):003:0> fm.file('foo.zip') 
=> "Zip archive data, at least v2.0 to extract"
irb(main):004:0> 

If you're on a Unix machine try this:

mimetype = `file -Ib #{path}`.gsub(/\n/,"")

I'm not aware of any pure Ruby solutions that work as reliably as 'file'.

Edited to add: depending what OS you are running you may need to use 'i' instead of 'I' to get file to return a mime-type.


I found shelling out to be the most reliable. For compatibility on both Mac OS X and Ubuntu Linux I used:

file --mime -b myvideo.mp4
video/mp4; charset=binary

Ubuntu also prints video codec information if it can which is pretty cool:

file -b myvideo.mp4
ISO Media, MPEG v4 system, version 2


You can use this reliable method base on the magic header of the file :

def get_image_extension(local_file_path)
  png = Regexp.new("\x89PNG".force_encoding("binary"))
  jpg = Regexp.new("\xff\xd8\xff\xe0\x00\x10JFIF".force_encoding("binary"))
  jpg2 = Regexp.new("\xff\xd8\xff\xe1(.*){2}Exif".force_encoding("binary"))
  case IO.read(local_file_path, 10)
  when /^GIF8/
    'gif'
  when /^#{png}/
    'png'
  when /^#{jpg}/
    'jpg'
  when /^#{jpg2}/
    'jpg'
  else
    mime_type = `file #{local_file_path} --mime-type`.gsub("\n", '') # Works on linux and mac
    raise UnprocessableEntity, "unknown file type" if !mime_type
    mime_type.split(':')[1].split('/')[1].gsub('x-', '').gsub(/jpeg/, 'jpg').gsub(/text/, 'txt').gsub(/x-/, '')
  end  
end

This was added as a comment on this answer but should really be its own answer:

path = # path to your file

IO.popen(
  ["file", "--brief", "--mime-type", path],
  in: :close, err: :close
) { |io| io.read.chomp }

I can confirm that it worked for me.