aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/lib/action_dispatch/http/upload.rb
blob: d50c8cad175d1dca4b8343608f4412f0170021a5 (plain) (tree)
1
2
3
4
5
6
7
8
9

                     
                      
                                                                          
 
                          


                                                                      
                                                             

                                        
         
 
                                               

                                                                           
         
 
             
 
                                   
                                                                    
                                                            
         


                 

                                                                    

                                                      
                                 






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

      def initialize(hash)
        @tempfile          = hash[:tempfile]
        raise(ArgumentError, ':tempfile is required') unless @tempfile

        @original_filename = encode_filename(hash[:filename])
        @content_type      = hash[:type]
        @headers           = hash[:head]
      end

      # Delegate these methods to the tempfile.
      [:read, :open, :close, :path, :rewind, :size, :eof?].each do |method|
        class_eval "def #{method}(*args); @tempfile.#{method}(*args); end"
      end

      private

      def encode_filename(filename)
        # Encode the filename in the utf8 encoding, unless it is nil
        filename.force_encoding("UTF-8").encode! if filename
      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