Sha256: bd572c9ea87c12348b56ecac4a2935f92f1f302eb4984e027290d3db599edab0

Contents?: true

Size: 1.69 KB

Versions: 12

Compression:

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

12 entries across 12 versions & 4 rubygems

Version Path
cloudfuji_paperclip-2.4.6 lib/paperclip/upfile.rb
paperclip-2.5.1 lib/paperclip/upfile.rb
paperclip-2.5.0 lib/paperclip/upfile.rb
mender_paperclip-2.4.3.1 lib/paperclip/upfile.rb
jmcnevin-paperclip-2.4.5.1 lib/paperclip/upfile.rb
jmcnevin-paperclip-2.4.5 lib/paperclip/upfile.rb
paperclip-2.4.5 lib/paperclip/upfile.rb
mender_paperclip-2.4.3 lib/paperclip/upfile.rb
paperclip-2.4.4 lib/paperclip/upfile.rb
paperclip-2.4.3 lib/paperclip/upfile.rb
paperclip-2.4.2 lib/paperclip/upfile.rb
paperclip-2.4.1 lib/paperclip/upfile.rb