<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ruby, Rails, OSX and Linux fun &#187; mime_type</title>
	<atom:link href="http://www.frederico-araujo.com/tag/mime_type/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.frederico-araujo.com</link>
	<description>Howto&#039;s, Tutorials and personal experience</description>
	<lastBuildDate>Tue, 27 Dec 2011 10:46:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Really checking the content-type/mime_type of a file in OSX and Linux</title>
		<link>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/</link>
		<comments>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 18:55:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[osx]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[content_type]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mime_type]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.frederico-araujo.com/?p=95</guid>
		<description><![CDATA[I have came across many projects where checking file uploads and content-type (mime-type) is poorly implemented or heavy in resource.

Methods I have seen so far:

1. Checking content-type from file name: this inefficient, a user can just rename a file and you are fooled, or the file can have a different file format and you ...]]></description>
			<content:encoded><![CDATA[<p>I have came across many projects where checking file uploads and content-type (mime-type) is poorly implemented or heavy in resource.</p>
<p>Methods I have seen so far:</p>
<p>1. Checking content-type from file name: this inefficient, a user can just rename a file and you are fooled, or the file can have a different file format and you will not get the expected result.</p>
<p>2. Using Rmagick to check if the file is an image. This is so slow and uses so much Ram. You can try to initialize an rmagick object from an image file, then rescue when the file is not an image.</p>
<p>3. Using mini_magick to check if a file. This method is faster than rmagick. Implemen ted same way as rmagick.</p>
<p>A Better method for OSX and Linux,  is to use the command line tool &#8220;file&#8221; included in most UNIX operating systems.</p>
<p>It is very fast and very accurate.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">file = <span style="color:#996600;">&quot;/path/to/file.ext&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> RUBY_PLATFORM.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>darwin<span style="color:#006600; font-weight:bold;">|</span>linux<span style="color:#006600; font-weight:bold;">|</span>unix<span style="color:#006600; font-weight:bold;">|</span>solaris<span style="color:#006600; font-weight:bold;">|</span>bsd<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
 content_type = <span style="color:#996600;">`file --raw --brief &quot;#{file}&quot;`</span>.<span style="color:#CC0066; font-weight:bold;">chomp</span>
 <span style="color:#9966CC; font-weight:bold;">case</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>image<span style="color:#006600; font-weight:bold;">|</span>png<span style="color:#006600; font-weight:bold;">|</span>jpg<span style="color:#006600; font-weight:bold;">|</span>jpeg<span style="color:#006600; font-weight:bold;">|</span>gif<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;image&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>pdf<span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;pdf&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> content_type.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Microsoft Word|Microsoft Office Document&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
   real_type = <span style="color:#996600;">&quot;doc&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#008000; font-style:italic;"># This can go on and on</span>
   real_type = <span style="color:#996600;">&quot;Unknown&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Some examples of content types:</p>
<p>.doc =  Microsoft Word document data</p>
<p>.doc = Microsoft Office Document</p>
<p>.pdf = PDF document, version 1.4</p>
<p>.pdf = PDF document, version 1.3</p>
<p>.psd = Adobe Photoshop Image</p>
<p>.png  = PNG image data, 3508 x 4961, 8-bit/color RGBA, non-interlaced</p>
<p>.gif = GIF image data, version 89a, 195 x 109</p>
<p>.jpg = JPEG image data, EXIF standard</p>
<p>etc&#8230;</p>
<p>I hope this can be useful to someone.</p>
<p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://www.frederico-araujo.com/2009/06/14/really-finding-the-content-type-of-a-file-in-osx-and-linux/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

