Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CarrierWave::MagicMimeTypes #1584

Merged
merged 8 commits into from
Mar 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ notifications:

addons:
postgresql: "9.3"
apt_packages:
- libmagic-dev
1 change: 1 addition & 0 deletions carrierwave.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "mini_magick", ">= 3.6.0"
if RUBY_ENGINE != 'jruby'
s.add_development_dependency "rmagick"
s.add_development_dependency "ruby-filemagic", ">= 0.6.3"
end
s.add_development_dependency "nokogiri", "~> 1.6.3"
s.add_development_dependency "timecop", "0.7.1"
Expand Down
1 change: 1 addition & 0 deletions lib/carrierwave/processing.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require "carrierwave/processing/rmagick"
require "carrierwave/processing/mini_magick"
require "carrierwave/processing/mime_types"
require "carrierwave/processing/magic_mime_types"
71 changes: 71 additions & 0 deletions lib/carrierwave/processing/magic_mime_types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# encoding: utf-8

module CarrierWave
##
# This module simplifies the use of ruby-filemagic gem to intelligently
# and correctly guess and set the content-type of a file. If you want
# to use this, you'll need to require this file:
#
# require 'carrierwave/processing/magic_mime_types'
#
# And then include it in your uploader:
#
# class MyUploader < CarrierWave::Uploader::Base
# include CarrierWave::MagicMimeTypes
# end
#
# You can use the provided helper:
#
# class MyUploader < CarrierWave::Uploader::Base
# include CarrierWave::MagicMimeTypes
#
# process :set_content_type
# end
module MagicMimeTypes
extend ActiveSupport::Concern

included do
begin
require "filemagic"
rescue LoadError => e
e.message << " (You may need to install the ruby-filemagic gem)"
raise e
end
end

module ClassMethods
def set_content_type(override=false)
process :set_content_type => override
end
end

##
# Changes the file content_type using the ruby-filemagic gem
#
# === Parameters
#
# [override (Boolean)] wheter or not to override the file's content_type
# if it is already set, false by default
def set_content_type(override=false)
if override || file.content_type.blank?
File.open(file.path) do |fd|
data = fd.read(1024) || ""
new_content_type = filemagic.buffer(data)
if file.respond_to?(:content_type=)
file.content_type = new_content_type
else
file.instance_variable_set(:@content_type, new_content_type)
end
end
end
end

##
# FileMagic object with the MAGIC_MIME_TYPE flag set
#
# @return [FileMagic] a filemagic object
def filemagic
@filemagic ||= FileMagic.new(FileMagic::MAGIC_MIME_TYPE)
end
end
end
Binary file added spec/fixtures/ruby.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions spec/processing/magic_mime_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# encoding: utf-8

require 'spec_helper'

describe CarrierWave::MagicMimeTypes, :filemagic => true do

before do
@klass = Class.new(CarrierWave::Uploader::Base) do
attr_accessor :content_type
include CarrierWave::MagicMimeTypes
end
@instance = @klass.new
FileUtils.cp(file_path('ruby.gif'), file_path('ruby_copy.gif'))
@instance.stub(:original_filename).and_return file_path('ruby_copy.gif')
@instance.stub(:file).and_return CarrierWave::SanitizedFile.new(file_path('ruby_copy.gif'))
@file = @instance.file
end

after do
FileUtils.rm(file_path('ruby_copy.gif'))
end

describe "#set_content_type" do
it "does not set the content_type if already set" do
@instance.file.content_type = 'image/jpeg'
@instance.file.should_not_receive(:content_type=)
@instance.set_content_type
end

it "sets content_type if content_type is nil" do
pending
@instance.file.content_type = nil
@instance.file.should_receive(:content_type=).with('image/png')
@instance.set_content_type
end

it "sets content_type if content_type is blank" do
@instance.file.content_type = ''
@instance.file.should_receive(:content_type=).with('image/png')
@instance.set_content_type
end

it "sets content_type if override is true" do
@instance.file.content_type = 'image/png'
@instance.file.should_receive(:content_type=).with('image/png')
@instance.set_content_type(true)
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,6 @@ def color_of_pixel(path, x, y)
config.include CarrierWave::Test::ManipulationHelpers
if RUBY_ENGINE == 'jruby'
config.filter_run_excluding :rmagick => true
config.filter_run_excluding :filemagic => true
end
end