Skip to content

Commit

Permalink
Merge pull request #2551 from pjmartorell/fix/replace-mimemagic-with-…
Browse files Browse the repository at this point in the history
…marcel

Replace mimemagic with marcel gem
  • Loading branch information
mshibuya authored Mar 27, 2021
2 parents 0411ae1 + 3cefc38 commit 3f80967
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
2 changes: 1 addition & 1 deletion carrierwave.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |s|
s.add_dependency "activemodel", ">= 5.0.0"
s.add_dependency "mini_mime", ">= 0.1.3"
s.add_dependency "image_processing", "~> 1.1"
s.add_dependency "mimemagic", ">= 0.3.0"
s.add_dependency "marcel", "~> 1.0.0"
s.add_dependency "addressable", "~> 2.6"
s.add_dependency "ssrf_filter", "~> 1.0"
if RUBY_ENGINE == 'jruby'
Expand Down
11 changes: 5 additions & 6 deletions lib/carrierwave/sanitized_file.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
require 'pathname'
require 'active_support/core_ext/string/multibyte'
require 'mini_mime'
require 'mimemagic'
require 'mimemagic/overlay'
require 'marcel'

module CarrierWave

Expand Down Expand Up @@ -262,7 +261,7 @@ def to_file
def content_type
@content_type ||=
existing_content_type ||
mime_magic_content_type ||
marcel_magic_content_type ||
mini_mime_content_type
end

Expand Down Expand Up @@ -329,14 +328,14 @@ def existing_content_type
end
end

def mime_magic_content_type
def marcel_magic_content_type
if path
type = File.open(path) do |file|
MimeMagic.by_magic(file).try(:type)
Marcel::Magic.by_magic(file).try(:type)
end

if type.nil?
type = ::MiniMime.lookup_by_filename(path).try(:content_type)
type = Marcel::Magic.by_path(file).try(:type)
type = 'invalid/invalid' unless type.nil? || type.start_with?('text/')
end

Expand Down
2 changes: 1 addition & 1 deletion spec/sanitized_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@
expect(sanitized_file.content_type).to eq("application/octet-stream")
end

it "should detect content type correctly using MagicMime when content_type is not set" do
it "should detect content type correctly using Marcel when content_type is not set" do
sanitized_file.content_type = nil
sanitized_file.move_to(file_path("new_dir","gurr.png"))

Expand Down

0 comments on commit 3f80967

Please sign in to comment.