From 21771e6221d6a01851172679c841dd641755277f Mon Sep 17 00:00:00 2001 From: Bradley Priest Date: Sun, 11 Mar 2018 11:10:14 -0700 Subject: [PATCH 1/2] Replace mime-types gem with mini_mime --- CHANGELOG.md | 2 ++ carrierwave.gemspec | 2 +- lib/carrierwave/processing/mini_magick.rb | 2 +- lib/carrierwave/processing/rmagick.rb | 2 +- lib/carrierwave/sanitized_file.rb | 17 ++++++++--------- lib/carrierwave/uploader/download.rb | 4 ++-- spec/sanitized_file_spec.rb | 10 ++-------- 7 files changed, 17 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bd833a41..8493198a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed +* Replace mime-types dependency with mini_mime(@bradleypriest [#2292](https://github.com/carrierwaveuploader/carrierwave/pull/2292)) ## 1.2.2 - 2018-01-02 ### Fixed diff --git a/carrierwave.gemspec b/carrierwave.gemspec index 179ad9179..09c90c035 100644 --- a/carrierwave.gemspec +++ b/carrierwave.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |s| s.add_dependency "activesupport", ">= 4.0.0" s.add_dependency "activemodel", ">= 4.0.0" - s.add_dependency "mime-types", ">= 1.16" + s.add_dependency "mini_mime", ">= 0.1.3" if RUBY_ENGINE == 'jruby' s.add_development_dependency 'activerecord-jdbcpostgresql-adapter' else diff --git a/lib/carrierwave/processing/mini_magick.rb b/lib/carrierwave/processing/mini_magick.rb index 8c51cb4e5..1346748d8 100644 --- a/lib/carrierwave/processing/mini_magick.rb +++ b/lib/carrierwave/processing/mini_magick.rb @@ -310,7 +310,7 @@ def manipulate! if @format move_to = current_path.chomp(File.extname(current_path)) + ".#{@format}" - file.content_type = ::MIME::Types.type_for(move_to).first.to_s + file.content_type = ::MiniMime.lookup_by_filename(move_to).content_type file.move_to(move_to, permissions, directory_permissions) end diff --git a/lib/carrierwave/processing/rmagick.rb b/lib/carrierwave/processing/rmagick.rb index 424ef225c..129fb4de4 100644 --- a/lib/carrierwave/processing/rmagick.rb +++ b/lib/carrierwave/processing/rmagick.rb @@ -363,7 +363,7 @@ def manipulate!(options={}, &block) if options[:format] || @format frames.write("#{options[:format] || @format}:#{current_path}", &write_block) move_to = current_path.chomp(File.extname(current_path)) + ".#{options[:format] || @format}" - file.content_type = ::MIME::Types.type_for(move_to).first.to_s + file.content_type = ::MiniMime.lookup_by_filename(move_to).content_type file.move_to(move_to, permissions, directory_permissions) else frames.write(current_path, &write_block) diff --git a/lib/carrierwave/sanitized_file.rb b/lib/carrierwave/sanitized_file.rb index 0c889941e..109ffc544 100644 --- a/lib/carrierwave/sanitized_file.rb +++ b/lib/carrierwave/sanitized_file.rb @@ -1,12 +1,6 @@ require 'pathname' require 'active_support/core_ext/string/multibyte' - -begin - # Use mime/types/columnar if available, for reduced memory usage - require 'mime/types/columnar' -rescue LoadError - require 'mime/types' -end +require 'mini_mime' module CarrierWave @@ -267,9 +261,14 @@ def to_file def content_type return @content_type if @content_type if @file.respond_to?(:content_type) and @file.content_type - @content_type = @file.content_type.to_s.chomp + if @file.content_type.respond_to?(:content_type) + @content_type = @file.content_type.content_type.chomp + else + @content_type = @file.content_type.to_s.chomp + end elsif path - @content_type = ::MIME::Types.type_for(path).first.to_s + mime_type = ::MiniMime.lookup_by_filename(path) + @content_type = (mime_type && mime_type.content_type).to_s end end diff --git a/lib/carrierwave/uploader/download.rb b/lib/carrierwave/uploader/download.rb index 0ae2d50a7..c3adb8db2 100644 --- a/lib/carrierwave/uploader/download.rb +++ b/lib/carrierwave/uploader/download.rb @@ -18,9 +18,9 @@ def initialize(uri, remote_headers = {}) def original_filename filename = filename_from_header || filename_from_uri - mime_type = MIME::Types[file.content_type].first + mime_type = MiniMime.lookup_by_content_type(file.content_type) unless File.extname(filename).present? || mime_type.blank? - filename = "#{filename}.#{mime_type.extensions.first}" + filename = "#{filename}.#{mime_type.extension}" end filename end diff --git a/spec/sanitized_file_spec.rb b/spec/sanitized_file_spec.rb index 5d8273d9c..337454f5d 100644 --- a/spec/sanitized_file_spec.rb +++ b/spec/sanitized_file_spec.rb @@ -1,11 +1,5 @@ require 'spec_helper' - -begin - # Use mime/types/columnar if available, for reduced memory usage - require 'mime/types/columnar' -rescue LoadError - require 'mime/types' -end +require 'mini_mime' describe CarrierWave::SanitizedFile do before do @@ -189,7 +183,7 @@ it "handles Mime::Type object" do file = File.open(file_path('sponsored.doc')) - allow(file).to receive(:content_type).and_return(MIME::Type.new("application/msword")) + allow(file).to receive(:content_type).and_return(MiniMime.lookup_by_content_type("application/msword")) sanitized_file = CarrierWave::SanitizedFile.new(file) allow(sanitized_file).to receive(:file).and_return(file) From 7b90f2c64576e170a48b8fbbe840cf0d8920d8dd Mon Sep 17 00:00:00 2001 From: Bradley Priest Date: Sat, 12 May 2018 14:31:34 +0800 Subject: [PATCH 2/2] Simplify the implementation by removing a stub --- lib/carrierwave/sanitized_file.rb | 6 +----- spec/sanitized_file_spec.rb | 3 +-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/carrierwave/sanitized_file.rb b/lib/carrierwave/sanitized_file.rb index 109ffc544..18842c9a1 100644 --- a/lib/carrierwave/sanitized_file.rb +++ b/lib/carrierwave/sanitized_file.rb @@ -261,11 +261,7 @@ def to_file def content_type return @content_type if @content_type if @file.respond_to?(:content_type) and @file.content_type - if @file.content_type.respond_to?(:content_type) - @content_type = @file.content_type.content_type.chomp - else - @content_type = @file.content_type.to_s.chomp - end + @content_type = @file.content_type.to_s.chomp elsif path mime_type = ::MiniMime.lookup_by_filename(path) @content_type = (mime_type && mime_type.content_type).to_s diff --git a/spec/sanitized_file_spec.rb b/spec/sanitized_file_spec.rb index 337454f5d..2b53b5660 100644 --- a/spec/sanitized_file_spec.rb +++ b/spec/sanitized_file_spec.rb @@ -1,5 +1,4 @@ require 'spec_helper' -require 'mini_mime' describe CarrierWave::SanitizedFile do before do @@ -183,7 +182,7 @@ it "handles Mime::Type object" do file = File.open(file_path('sponsored.doc')) - allow(file).to receive(:content_type).and_return(MiniMime.lookup_by_content_type("application/msword")) + sanitized_file = CarrierWave::SanitizedFile.new(file) allow(sanitized_file).to receive(:file).and_return(file)