Skip to content

Commit

Permalink
temporarily update with mimemagic fix
Browse files Browse the repository at this point in the history
- tmp fix based on carrierwaveuploader#1934
  • Loading branch information
Neil-Ni committed May 6, 2016
1 parent ecf57fe commit 92de80e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
2 changes: 2 additions & 0 deletions carrierwave.gemspec
Expand Up @@ -28,6 +28,8 @@ Gem::Specification.new do |s|
s.add_dependency "activemodel", ">= 3.2.0"
s.add_dependency "json", ">= 1.7"
s.add_dependency "mime-types", ">= 1.16"
s.add_dependency "mimemagic", ">= 0.3.0"

if RUBY_ENGINE == 'jruby'
s.add_development_dependency 'activerecord-jdbcpostgresql-adapter'
else
Expand Down
27 changes: 21 additions & 6 deletions lib/carrierwave/sanitized_file.rb
Expand Up @@ -2,6 +2,7 @@

require 'pathname'
require 'active_support/core_ext/string/multibyte'
require 'mimemagic'

begin
# Use mime/types/columnar if available, for reduced memory usage
Expand Down Expand Up @@ -253,12 +254,10 @@ def to_file
# [String] the content type of the 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
elsif path
@content_type = ::MIME::Types.type_for(path).first.to_s
end
@content_type ||=
existing_content_type ||
mime_magic_content_type ||
mime_types_content_type
end

##
Expand Down Expand Up @@ -318,6 +317,22 @@ def sanitize(name)
return name.mb_chars.to_s
end

def existing_content_type
if @file.respond_to?(:content_type) && @file.content_type
@file.content_type.to_s.chomp
end
end

def mime_magic_content_type
MimeMagic.by_magic(File.open(path)).try(:type) if path
rescue Errno::ENOENT
nil
end

def mime_types_content_type
::MIME::Types.type_for(path).first.to_s if path
end

def split_extension(filename)
# regular expressions to try for identifying extensions
extension_matchers = [
Expand Down

0 comments on commit 92de80e

Please sign in to comment.