File identification is a good thing to let someone else if possible. ruby-filemagic gem will do this.
gem 'ruby-filemagic'
When used, it returns a string:
require 'filemagic' magic = FileMagic.new p magic.file("/tmp/pic1.jpg")
The returned string can be matched with regular expressions:
case magic.file(path) when /JPEG/
ruby-filemagic uses the libmagic library, which recognizes a large number of file types.
The documentation is a bit rare (README doesn't even have a hello world example), and it doesn't update after a few years, but don't let it stop you from trying. It is fairly easy to use and quite robust - today I have production code, and it still works great.
If for some reason you cannot use the gem, but are in the * nix environment and have access to the file command, you can get the same functionality by downloading the file:
p `file /tmp/pic1.jpg`
In Debian, the file command is provided by the package file. Your OS may vary.
source share