Sha256: a1bd0449137e0baabd118da1ec54122f512b6b6939e64a6eab7357db7ce19f06

Contents?: true

Size: 738 Bytes

Versions: 14

Compression:

Stored size: 738 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 Terrapin::CommandLineError => e
               Paperclip.log("Error while determining content type: #{e}")
               SENSIBLE_DEFAULT
             end

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
kt-paperclip-7.2.2 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-7.2.1 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-7.2.0 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-6.4.2 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-7.1.1 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-7.1.0 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-7.0.1 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-7.0.0 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-6.4.1 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-6.4.0 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-6.3.0 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-6.2.2 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-6.2.1 lib/paperclip/file_command_content_type_detector.rb
kt-paperclip-6.2.0 lib/paperclip/file_command_content_type_detector.rb