<p>How does one <strong>reliably</strong> determine a file's type? File extension analysis is not acceptable. There must be a rubyesque tool similar to the UNIX file(1) command?</p>

<p>This is regarding MIME or content type, not file system classifications, such as directory, file, or socket.</p>

[{'answer_id': 51616, 'author': 'Bobby Jack', 'author_id': 5058, 'author_profile': 'https://Stackoverflow.com/users/5058', 'pm_score': -1, 'selected': False, 'text': '<p>You could give a go with <a href="http://mime-types.rubyforge.org/" rel="nofollow noreferrer">MIME::Types for Ruby</a>.</p>\n\n<blockquote>\n  <p>This library allows for the identification of a file’s likely MIME content type. The identification of MIME content type is based on a file’s filename extensions.</p>\n</blockquote>\n'}, {'answer_id': 56933, 'author': 'Patrick Ritchie', 'author_id': 4748, 'author_profile': 'https://Stackoverflow.com/users/4748', 'pm_score': 5, 'selected': False, 'text': '<p>If you\'re on a Unix machine try this:</p>\n\n<pre><code>mimetype = `file -Ib #{path}`.gsub(/\\n/,"")\n</code></pre>\n\n<p>I\'m not aware of any pure Ruby solutions that work as reliably as \'file\'.</p>\n\n<p>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.</p>\n'}, {'answer_id': 94102, 'author': 'Chris Ingrassia', 'author_id': 6991, 'author_profile': 'https://Stackoverflow.com/users/6991', 'pm_score': 2, 'selected': False, 'text': '<p>You could give <a href="http://shared-mime.rubyforge.org/" rel="nofollow noreferrer">shared-mime</a> a try (gem install shared-mime-info).  Requires the use ofthe Freedesktop shared-mime-info library, but does both filename/extension checks as well as "magic" checks... tried giving it a whirl myself just now but I don\'t have the freedesktop shared-mime-info database installed and have to do "real work," unfortunately, but it might be what you\'re looking for.</p>\n'}, {'answer_id': 901660, 'author': 'Martin Carpenter', 'author_id': 39443, 'author_profile': 'https://Stackoverflow.com/users/39443', 'pm_score': 6, 'selected': False, 'text': '<p>There is a ruby binding to <code>libmagic</code> that does what you need. It is available as a gem named <a href="https://rubygems.org/gems/ruby-filemagic/versions/0.7.1" rel="noreferrer">ruby-filemagic</a>:</p>\n\n<pre><code>gem install ruby-filemagic\n</code></pre>\n\n<p>Require <code>libmagic-dev</code>.</p>\n\n<p>The documentation seems a little thin, but this should get you started:</p>\n\n<pre><code>$ irb \nirb(main):001:0&gt; require \'filemagic\' \n=&gt; true\nirb(main):002:0&gt; fm = FileMagic.new\n=&gt; #&lt;FileMagic:0x7fd4afb0&gt;\nirb(main):003:0&gt; fm.file(\'foo.zip\') \n=&gt; "Zip archive data, at least v2.0 to extract"\nirb(main):004:0&gt; \n</code></pre>\n'}, {'answer_id': 1373705, 'author': 'Qianjigui', 'author_id': 158165, 'author_profile': 'https://Stackoverflow.com/users/158165', 'pm_score': -1, 'selected': False, 'text': '<p>The ruby gem is well.\n<a href="http://rubyforge.org/projects/mime-types/" rel="nofollow noreferrer">mime-types for ruby</a></p>\n'}, {'answer_id': 3754019, 'author': 'heathanderson', 'author_id': 393068, 'author_profile': 'https://Stackoverflow.com/users/393068', 'pm_score': 1, 'selected': False, 'text': '<p>I recently found <a href="http://github.com/mattetti/mimetype-fu" rel="nofollow noreferrer">mimetype-fu</a>. </p>\n\n<p>It seems to be the easiest reliable solution to get a file\'s MIME type.</p>\n\n<p>The only caveat is that on a Windows machine it only uses the file extension, whereas on *Nix based  systems it works great.</p>\n'}, {'answer_id': 4117137, 'author': 'jamiew', 'author_id': 144807, 'author_profile': 'https://Stackoverflow.com/users/144807', 'pm_score': 4, 'selected': False, 'text': '<p>I found shelling out to be the most reliable. For compatibility on both Mac OS X and Ubuntu Linux I used:</p>\n\n<p><code>file --mime -b myvideo.mp4</code>\n<br/><strong>video/mp4; charset=binary</strong></p>\n\n<p>Ubuntu also prints video codec information if it can which is pretty cool:</p>\n\n<p><code>file -b myvideo.mp4</code>\n<br /><strong>ISO Media, MPEG v4 system, version 2</strong></p>\n'}, {'answer_id': 9005833, 'author': 'knoopx', 'author_id': 62368, 'author_profile': 'https://Stackoverflow.com/users/62368', 'pm_score': 0, 'selected': False, 'text': '<p>The best I found so far: </p>\n\n<p><a href="http://bogomips.org/mahoro.git/" rel="nofollow">http://bogomips.org/mahoro.git/</a></p>\n'}, {'answer_id': 10253211, 'author': 'joelparkerhenderson', 'author_id': 528726, 'author_profile': 'https://Stackoverflow.com/users/528726', 'pm_score': 1, 'selected': False, 'text': '<p>Pure Ruby solution using magic bytes and returning a symbol for the matching type:</p>\n\n<p><a href="https://github.com/SixArm/sixarm_ruby_magic_number_type" rel="nofollow">https://github.com/SixArm/sixarm_ruby_magic_number_type</a></p>\n\n<p>I wrote it, so if you have suggestions, let me know.</p>\n'}, {'answer_id': 16636012, 'author': 'Alain Beauvois', 'author_id': 183331, 'author_profile': 'https://Stackoverflow.com/users/183331', 'pm_score': 3, 'selected': False, 'text': '<p>You can use this reliable method base on the magic header of the file :</p>\n\n<pre><code>def get_image_extension(local_file_path)\n  png = Regexp.new("\\x89PNG".force_encoding("binary"))\n  jpg = Regexp.new("\\xff\\xd8\\xff\\xe0\\x00\\x10JFIF".force_encoding("binary"))\n  jpg2 = Regexp.new("\\xff\\xd8\\xff\\xe1(.*){2}Exif".force_encoding("binary"))\n  case IO.read(local_file_path, 10)\n  when /^GIF8/\n    \'gif\'\n  when /^#{png}/\n    \'png\'\n  when /^#{jpg}/\n    \'jpg\'\n  when /^#{jpg2}/\n    \'jpg\'\n  else\n    mime_type = `file #{local_file_path} --mime-type`.gsub("\\n", \'\') # Works on linux and mac\n    raise UnprocessableEntity, "unknown file type" if !mime_type\n    mime_type.split(\':\')[1].split(\'/\')[1].gsub(\'x-\', \'\').gsub(/jpeg/, \'jpg\').gsub(/text/, \'txt\').gsub(/x-/, \'\')\n  end  \nend\n</code></pre>\n'}, {'answer_id': 25892795, 'author': 'spyle', 'author_id': 326979, 'author_profile': 'https://Stackoverflow.com/users/326979', 'pm_score': 3, 'selected': False, 'text': "<p>If you're using the File class, you can augment it with the following functions based on @PatrickRichie's answer:</p>\n\n<pre><code>class File\n    def mime_type\n        `file --brief --mime-type #{self.path}`.strip\n    end\n\n    def charset\n        `file --brief --mime #{self.path}`.split(';').second.split('=').second.strip\n    end\nend\n</code></pre>\n\n<p>And, if you're using Ruby on Rails, you can drop this into config/initializers/file.rb and have available throughout your project.</p>\n"}, {'answer_id': 50278033, 'author': 'Paulo Fidalgo', 'author_id': 1006863, 'author_profile': 'https://Stackoverflow.com/users/1006863', 'pm_score': 2, 'selected': False, 'text': '<p>For those who came here by the search engine, a modern approach to find the MimeType in pure ruby is to use the <a href="https://github.com/minad/mimemagic" rel="nofollow noreferrer">mimemagic</a> gem.</p>\n\n<pre><code>require \'mimemagic\'\n\nMimeMagic.by_magic(File.open(\'tux.jpg\')).type # =&gt; "image/jpeg" \n</code></pre>\n\n<p>If you feel that is safe to use only the file extension, then you can use the <a href="https://github.com/mime-types/ruby-mime-types" rel="nofollow noreferrer">mime-types</a> gem:</p>\n\n<pre><code>MIME::Types.type_for(\'tux.jpg\') =&gt; [#&lt;MIME::Type: image/jpeg&gt;]\n</code></pre>\n'}, {'answer_id': 57431940, 'author': 'Jason Swett', 'author_id': 199712, 'author_profile': 'https://Stackoverflow.com/users/199712', 'pm_score': 3, 'selected': False, 'text': '<p>This was added as a comment on <a href="https://stackoverflow.com/a/56933/199712">this answer</a> but should really be its own answer:</p>\n\n<pre><code>path = # path to your file\n\nIO.popen(\n  ["file", "--brief", "--mime-type", path],\n  in: :close, err: :close\n) { |io| io.read.chomp }\n</code></pre>\n\n<p>I can confirm that it worked for me.</p>\n'}]
['https://Stackoverflow.com/questions/51572', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']