Sha256: cdd87c7a648906fc1c7528b699cad984ce025e97d57f4abe046c3fbbd08404fe

Contents?: true

Size: 1.3 KB

Versions: 78

Compression:

Stored size: 1.3 KB

Contents

module ActionDispatch
  module Http
    class UploadedFile
      attr_accessor :original_filename, :content_type, :tempfile, :headers

      def initialize(hash)
        @original_filename = encode_filename(hash[:filename])
        @content_type      = hash[:type]
        @headers           = hash[:head]
        @tempfile          = hash[:tempfile]
        raise(ArgumentError, ':tempfile is required') unless @tempfile
      end

      def read(*args)
        @tempfile.read(*args)
      end

      # Delegate these methods to the tempfile.
      [:open, :path, :rewind, :size].each do |method|
        class_eval "def #{method}; @tempfile.#{method}; end"
      end

      private
      def encode_filename(filename)
        # Encode the filename in the utf8 encoding, unless it is nil or we're in 1.8
        if "ruby".encoding_aware? && filename
          filename.force_encoding("UTF-8").encode!
        else
          filename
        end
      end
    end

    module Upload
      # Convert nested Hash to HashWithIndifferentAccess and replace
      # file upload hash with UploadedFile objects
      def normalize_parameters(value)
        if Hash === value && value.has_key?(:tempfile)
          UploadedFile.new(value)
        else
          super
        end
      end
      private :normalize_parameters
    end
  end
end

Version data entries

78 entries across 63 versions & 16 rubygems

Version Path
actionpack-3.2.14.rc1 lib/action_dispatch/http/upload.rb
swipe-rails-0.0.5 vendor/bundle/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
font-awesome-rails-3.1.1.2 vendor/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
font-awesome-rails-3.1.1.2 vendor/ruby/2.0.0/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
font-awesome-rails-3.1.1.1 vendor/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
font-awesome-rails-3.1.1.1 vendor/ruby/2.0.0/gems/actionpack-3.2.12/lib/action_dispatch/http/upload.rb
challah-1.0.0.beta3 vendor/bundle/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
fc-webicons-0.0.4 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
challah-1.0.0.beta2 vendor/bundle/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
challah-1.0.0.beta vendor/bundle/gems/actionpack-3.2.11/lib/action_dispatch/http/upload.rb
challah-1.0.0.beta vendor/bundle/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
fc-webicons-0.0.3 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
fc-webicons-0.0.2 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb
fc-webicons-0.0.1 vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.13/lib/action_dispatch/http/upload.rb