Sha256: 8ced7eb7b1a1f81a139764a4fb7b4bcdd8d88caa02ce2e35bb7b4ccfd61946ba

Contents?: true

Size: 1.69 KB

Versions: 30

Compression:

Stored size: 1.69 KB

Contents

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
      type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
      case type
      when %r"jp(e|g|eg)"            then "image/jpeg"
      when %r"tiff?"                 then "image/tiff"
      when %r"png", "gif", "bmp"     then "image/#{type}"
      when "txt"                     then "text/plain"
      when %r"html?"                 then "text/html"
      when "js"                      then "application/js"
      when "csv", "xml", "css"       then "text/#{type}"
      else
        # On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
        content_type = (Paperclip.run("file", "-b --mime-type :file", :file => self.path).split(':').last.strip rescue "application/x-#{type}")
        content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/)
        content_type
      end
    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

30 entries across 30 versions & 13 rubygems

Version Path
dm-paperclip-2.5.0 lib/dm-paperclip/upfile.rb
smarter_paperclip-0.3.3 lib/paperclip/upfile.rb
smarter_paperclip-0.3.2 lib/paperclip/upfile.rb
smarter_paperclip-0.3.1 lib/paperclip/upfile.rb
smarter_paperclip-0.3.0 lib/paperclip/upfile.rb
cemeng-paperclip-2.3.6 lib/paperclip/upfile.rb
paperclip-cloudfiles-2.3.10.1 lib/paperclip/upfile.rb
paperclip-cloudfiles-2.3.8.6 lib/paperclip/upfile.rb
sayso-paperclip-2.3.10.001 lib/paperclip/upfile.rb
paperclip-cloudfiles-2.3.8.5 lib/paperclip/upfile.rb
ryansch-paperclip-2.3.10 lib/paperclip/upfile.rb
paperclip-2.3.11 lib/paperclip/upfile.rb
paperclip-2.3.10 lib/paperclip/upfile.rb
paperclip-2.3.9 lib/paperclip/upfile.rb
paperclip-cloudfiles-2.3.8.4 lib/paperclip/upfile.rb
paperclip-youtube-2.3.8.2 lib/paperclip/upfile.rb
paperclip-youtube-2.3.8.1 lib/paperclip/upfile.rb
paperclip-cloudfiles-2.3.8.3 lib/paperclip/upfile.rb
whitby3001-paperclip-cloudfiles-2.3.8.1 lib/paperclip/upfile.rb
organicinternet-paperclip-2.3.8.1 lib/paperclip/upfile.rb