Sha256: f072ec4ec4e4096ddeb2fdc394f0b13665908c6c03b6e08b8cb5ce40ea557210

Contents?: true

Size: 755 Bytes

Versions: 18

Compression:

Stored size: 755 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
      # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
      type = begin
               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

18 entries across 18 versions & 5 rubygems

Version Path
kt-paperclip-4.4.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-5.2.1 lib/paperclip/file_command_content_type_detector.rb
paperclip-5.2.0 lib/paperclip/file_command_content_type_detector.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/paperclip-5.1.0/lib/paperclip/file_command_content_type_detector.rb
paperclip-5.1.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-5.0.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.7 lib/paperclip/file_command_content_type_detector.rb
paperclip_jk-5.0.0.beta2 lib/paperclip/file_command_content_type_detector.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/lib/paperclip/file_command_content_type_detector.rb
paperclip-5.0.0.beta2 lib/paperclip/file_command_content_type_detector.rb
paperclip-5.0.0.beta1 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.6 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.5 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.4 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.3 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.2 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.1 lib/paperclip/file_command_content_type_detector.rb
paperclip-4.3.0 lib/paperclip/file_command_content_type_detector.rb