Determine File Type in Ruby

How to check in rails uploaded file type?

uploaded_io.content_type contains the MIME type of file.

So:

uploaded_io.content_type == "text/csv"

How do I reliably determine file type when FileMagic is not an option?

filemagic isn't the gem you're looking for. This appears to be someone's project not intended for public consumption (the rubygems page links to a company's website, and no source code is available).

Instead, install the ruby-filemagic gem. Here's the source and documentation. We can use the library like so:

begin
fm = FileMagic.new(FileMagic::MAGIC_MIME)
puts fm.file("foo.txt", true)
ensure
fm.close
end

...or by using the open() helper method, which automatically closes the resource for us:

FileMagic.open(FileMagic::MAGIC_MIME) { |fm| puts fm.file("foo.txt", true) }

This also works with yet another helper, mime(), which additionally sets the flag for us:

FileMagic.mime { |fm| puts fm.file("foo.txt", true) }

This gem relies on libmagic, a library used to identify file types using the file's magic number. The file command, commonly found on Unix/Linux systems, uses the same library:

$ file --mime-type foo.txt
foo.txt: text/plain

We can also invoke this command from the Ruby application to get a file's MIME type:

IO.popen(["file", "--mime-type", "--brief", "foo.txt"]) { |io| puts io.read.chomp }

MimeMagic is an alternative to FileMagic.

How to detect a file type in a Rails app?

I guess (is not a good thing to let other people guess what you should already provided in your question) you have a model Document and your uploader is media, something like this:

class Document < ActiveRecord::Base
mount_uploader :media, MediaUploader
end

If this is the case, for each document you get the extension (document.media.file.extension.downcase) and compare it with 'jpg', 'jpeg', 'png'

<% document.each do |doc| %>
<% if ['jpg', 'jpeg', 'png'].include?(document.media.file.extension.downcase) %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>

Carrierwave can give you the content type if you want it by using:

document.media.content_type # this returns image/png for a png file ...

Edit:

I think a better way is to check it like this (it's cleaner):

<% document.each do |doc| %>
<% if document.media.content_type =~ /image/ %>
<%= link_to image_tag(doc.media_url(:thumb)) %>
<% else %>
<%= link_to doc.media.path %> # This link downloading the file
<% end %>
<% end %>

Find the extension of a filename in Ruby

irb(main):002:0> accepted_formats = [".txt", ".pdf"]
=> [".txt", ".pdf"]
irb(main):003:0> File.extname("example.pdf") # get the extension
=> ".pdf"
irb(main):004:0> accepted_formats.include? File.extname("example.pdf")
=> true
irb(main):005:0> accepted_formats.include? File.extname("example.txt")
=> true
irb(main):006:0> accepted_formats.include? File.extname("example.png")
=> false

Detect MIME type of uploaded file in Ruby

The ruby-filemagic gem will do it:

require 'filemagic'

puts FileMagic.new(FileMagic::MAGIC_MIME).file(__FILE__)
# => text/x-ruby; charset=us-ascii

This gem does not look at the file extension at all. It reads a bit of the file contents and uses that to guess the file's type.

How to determine mime type by the file extension? ( Ruby )

You could use the mime-types gem:

puts MIME::Types.type_for('css')
=> [text/css]


Related Topics



Leave a reply



Submit