Module: CarrierWave::Magic

Extended by:
ActiveSupport::Concern
Defined in:
lib/carrierwave-magic.rb,
lib/carrierwave-magic/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

GENERIC_CONTENT_TYPES =
%w[application/octet-stream binary/octet-stream]
VERSION =
"0.0.4"

Instance Method Summary collapse

Instance Method Details

#generic_content_type?(content_type) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/carrierwave-magic.rb', line 24

def generic_content_type?(content_type)
  GENERIC_CONTENT_TYPES.include? content_type
end

#set_magic_content_type(override = false) ⇒ Object

Changes the file content_type using the ruby-filemagic gem



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/carrierwave-magic.rb', line 31

def set_magic_content_type(override=false)
  if override || file.content_type.blank? || generic_content_type?(file.content_type)
    new_content_type = ::FileMagic.new(::FileMagic::MAGIC_MIME).file( file.path ).split(';').first

    if file.respond_to?(:content_type=)
      file.content_type = new_content_type
    else
      file.instance_variable_set(:@content_type, new_content_type)
    end
  end
rescue ::Exception => e
  raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.magic_mime_types_processing_error", e: e, default: 'Failed to process file with FileMagic, Original Error: %{e}')
end