Sha256: 5d971d4eb11d868468d16e00eeef6783a9a663bfb41b3ac04cedb058040250c9

Contents?: true

Size: 756 Bytes

Versions: 4

Compression:

Stored size: 756 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

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

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
kt-paperclip-5.4.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-6.1.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-6.0.0 lib/paperclip/file_command_content_type_detector.rb
paperclip-5.3.0 lib/paperclip/file_command_content_type_detector.rb