Sha256: bea9515823795ffb37b9570a90b88fe8c496cc62bb8bcead1c4be668f178b818

Contents?: true

Size: 1.7 KB

Versions: 9

Compression:

Stored size: 1.7 KB

Contents

require 'mime/types'

module Paperclip
  # The Upfile module is a convenience module for adding uploaded-file-type methods
  # to the +File+ class. Useful for testing.
  #   user.avatar = File.new("test/test_avatar.jpg")
  module Upfile
    # Infer the MIME-type of the file from the extension.
    def content_type
      types = MIME::Types.type_for(self.original_filename)
      if types.length == 0
        type_from_file_command
      elsif types.length == 1
        types.first.content_type
      else
        iterate_over_array_to_find_best_option(types)
      end
    end

    def iterate_over_array_to_find_best_option(types)
      types.reject {|type| type.content_type.match(/\/x-/) }.first
    end

    def type_from_file_command
      #  On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
      type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
      mime_type = (Paperclip.run("file", "-b --mime :file", :file => self.path).split(/[:;]\s+/)[0] rescue "application/x-#{type}")
      mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
      mime_type
    end

    # Returns the file's normal name.
    def original_filename
      File.basename(self.path)
    end

    # Returns the size of the file.
    def size
      File.size(self)
    end
  end
end

if defined? StringIO
  class StringIO
    attr_accessor :original_filename, :content_type, :fingerprint

    def original_filename
      @original_filename ||= "stringio.txt"
    end

    def content_type
      @content_type ||= "text/plain"
    end

    def fingerprint
      @fingerprint ||= Digest::MD5.hexdigest(self.string)
    end
  end
end

class File #:nodoc:
  include Paperclip::Upfile
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
paperclip-v2_7-patched-ruby-1_8_6-2.7.5 lib/paperclip/upfile.rb
paperclip-2.7.5 lib/paperclip/upfile.rb
paperclip-2.7.4 lib/paperclip/upfile.rb
paperclip-2.7.2 lib/paperclip/upfile.rb
paperclip-2.8.0 lib/paperclip/upfile.rb
paperclip-2.7.1 lib/paperclip/upfile.rb
paperclip-2.7.0 lib/paperclip/upfile.rb
paperclip-2.6.0 lib/paperclip/upfile.rb
paperclip-2.5.2 lib/paperclip/upfile.rb