Sha256: 927bee5e4a96e87fb7427bc7d93bbf5fe517891e13d5041ca363f9639e3f5bfd

Contents?: true

Size: 1.86 KB

Versions: 3

Compression:

Stored size: 1.86 KB

Contents

require 'mime/types'

module CarrierWave

  ##
  # This module simplifies the use of the mime-types gem to intelligently
  # 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/mime_types'
  #
  # And then include it in your uploader:
  #
  #     class MyUploader < CarrierWave::Uploader::Base
  #       include CarrierWave::MimeTypes
  #     end
  #
  # You can now use the provided helper:
  #
  #     class MyUploader < CarrierWave::Uploader::Base
  #       include CarrierWave::MimeTypes
  #
  #       process :set_content_type
  #     end
  #
  module MimeTypes
    extend ActiveSupport::Concern

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

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

    def generic_content_type?
      GENERIC_CONTENT_TYPES.include? file.content_type
    end

    ##
    # Changes the file content_type using the mime-types gem
    #
    # === Parameters
    #
    # [override (Boolean)] whether or not to override the file's content_type
    #                      if it is already set and not a generic content-type,
    #                      false by default
    #
    def set_content_type(override=false)
      if override || file.content_type.blank? || generic_content_type?
        new_content_type = ::MIME::Types.type_for(file.original_filename).first.to_s
        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 ::MIME::InvalidContentType => e
      raise CarrierWave::ProcessingError, I18n.translate(:"errors.messages.mime_types_processing_error", :e => e)
    end

  end # MimeTypes
end # CarrierWave

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
sunrise-cms-0.5.0.rc1 vendor/bundle/ruby/1.9.1/gems/carrierwave-0.7.1/lib/carrierwave/processing/mime_types.rb
carrierwave-0.7.1 lib/carrierwave/processing/mime_types.rb
carrierwave-0.7.0 lib/carrierwave/processing/mime_types.rb