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
paperclip-3.4.0 lib/paperclip/file_command_content_type_detector.rb