Sha256: c87b8795fd938dabf339478ea69eacf9fe7d442b5965ef60cdcefb79591eccdd

Contents?: true

Size: 727 Bytes

Versions: 21

Compression:

Stored size: 727 Bytes

Contents

module Paperclip
  class FileCommandContentTypeDetector
    SENSIBLE_DEFAULT = "application/octet-stream"

    def initialize(filename)
      @filename = filename
    end

    def detect
      type_from_file_command
    end

    private

    def type_from_file_command
      type = begin
        # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
        Paperclip.run("file", "-b --mime :file", :file => @filename)
      rescue Cocaine::CommandLineError => e
        Paperclip.log("Error while determining content type: #{e}")
        SENSIBLE_DEFAULT
      end

      if type.nil? || type.match(/\(.*?\)/)
        type = SENSIBLE_DEFAULT
      end
      type.split(/[:;\s]+/)[0]
    end

  end
end

Version data entries

21 entries across 19 versions & 2 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/file_command_content_type_detector.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/file_command_content_type_detector.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.4/lib/paperclip/file_command_content_type_detector.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.2/lib/paperclip/file_command_content_type_detector.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.3/lib/paperclip/file_command_content_type_detector.rb
paperclip-4.2.4 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.2.3 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.2.2 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.2.1 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.2.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.1.1 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.1.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-3.5.4 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.0.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-3.5.3 lib/paperclip/file_command_content_type_detector.rb
paperclip-3.5.2 lib/paperclip/file_command_content_type_detector.rb
paperclip-3.5.1 lib/paperclip/file_command_content_type_detector.rb
paperclip-3.5.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-3.4.2 lib/paperclip/file_command_content_type_detector.rb
paperclip-3.4.1 lib/paperclip/file_command_content_type_detector.rb