Commit 5be67595 by Alex Godin

working implementation of the mime-types gem

parent 1ff1cc73
...@@ -10,3 +10,4 @@ gem "appraisal" ...@@ -10,3 +10,4 @@ gem "appraisal"
gem "fog" gem "fog"
gem "bundler" gem "bundler"
gem "cocaine" gem "cocaine"
gem "mime-types"
...@@ -69,6 +69,7 @@ DEPENDENCIES ...@@ -69,6 +69,7 @@ DEPENDENCIES
bundler bundler
cocaine cocaine
fog fog
mime-types
mocha mocha
rake rake
shoulda shoulda
......
...@@ -6,22 +6,26 @@ module Paperclip ...@@ -6,22 +6,26 @@ module Paperclip
# Infer the MIME-type of the file from the extension. # Infer the MIME-type of the file from the extension.
def content_type def content_type
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase types = MIME::Types.type_for(self.original_filename)
case type if types.length == 0
when %r"jp(e|g|eg)" then "image/jpeg" type_from_file_command
when %r"tiff?" then "image/tiff" elsif types.length == 1
when %r"png", "gif", "bmp" then "image/#{type}" types.first.content_type
when %r"svg" then "image/svg+xml"
when "txt" then "text/plain"
when %r"html?" then "text/html"
when "js" then "application/js"
when "csv", "xml", "css" then "text/#{type}"
else else
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist. iterate_over_array_to_find_best_option(types)
content_type = (Paperclip.run("file", "-b --mime-type :file", :file => self.path).split(':').last.strip rescue "application/x-#{type}") end
content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/) end
content_type
def iterate_over_array_to_find_best_option(types)
types.reject {|type| type.content_type.match(/\/x-/) }.first
end end
def type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
mime_type = (Paperclip.run("file", "-b --mime-type :file", :file => self.path).split(':').last.strip rescue "application/x-#{type}")
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
mime_type
end end
# Returns the file's normal name. # Returns the file's normal name.
......
...@@ -8,6 +8,7 @@ require 'mocha' ...@@ -8,6 +8,7 @@ require 'mocha'
require 'active_record' require 'active_record'
require 'active_record/version' require 'active_record/version'
require 'active_support' require 'active_support'
require 'mime/types'
puts "Testing against version #{ActiveRecord::VERSION::STRING}" puts "Testing against version #{ActiveRecord::VERSION::STRING}"
......
...@@ -10,9 +10,9 @@ class UpfileTest < Test::Unit::TestCase ...@@ -10,9 +10,9 @@ class UpfileTest < Test::Unit::TestCase
%w(txt) => 'text/plain', %w(txt) => 'text/plain',
%w(htm html) => 'text/html', %w(htm html) => 'text/html',
%w(csv) => 'text/csv', %w(csv) => 'text/csv',
%w(xml) => 'text/xml', %w(xml) => 'application/xml',
%w(css) => 'text/css', %w(css) => 'text/css',
%w(js) => 'application/js', %w(js) => 'application/javascript',
%w(foo) => 'application/x-foo' %w(foo) => 'application/x-foo'
}.each do |extensions, content_type| }.each do |extensions, content_type|
extensions.each do |extension| extensions.each do |extension|
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment