Sha256: 282650a98c25f7b3083d34cfaa0916c8f187fff1468780a03bc973e1e749cd8d

Contents?: true

Size: 1.33 KB

Versions: 37

Compression:

Stored size: 1.33 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 open
        @tempfile.open
      end

      def path
        @tempfile.path
      end

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

      def rewind
        @tempfile.rewind
      end

      def size
        @tempfile.size
      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

37 entries across 37 versions & 1 rubygems

Version Path
actionpack-3.1.12 lib/action_dispatch/http/upload.rb
actionpack-3.1.11 lib/action_dispatch/http/upload.rb
actionpack-3.0.20 lib/action_dispatch/http/upload.rb
actionpack-3.1.10 lib/action_dispatch/http/upload.rb
actionpack-3.0.19 lib/action_dispatch/http/upload.rb
actionpack-3.1.9 lib/action_dispatch/http/upload.rb
actionpack-3.0.18 lib/action_dispatch/http/upload.rb
actionpack-3.1.8 lib/action_dispatch/http/upload.rb
actionpack-3.0.17 lib/action_dispatch/http/upload.rb
actionpack-3.1.7 lib/action_dispatch/http/upload.rb
actionpack-3.0.16 lib/action_dispatch/http/upload.rb
actionpack-3.0.15 lib/action_dispatch/http/upload.rb
actionpack-3.1.6 lib/action_dispatch/http/upload.rb
actionpack-3.0.14 lib/action_dispatch/http/upload.rb
actionpack-3.1.5 lib/action_dispatch/http/upload.rb
actionpack-3.0.13 lib/action_dispatch/http/upload.rb
actionpack-3.1.5.rc1 lib/action_dispatch/http/upload.rb
actionpack-3.0.13.rc1 lib/action_dispatch/http/upload.rb
actionpack-3.1.4 lib/action_dispatch/http/upload.rb
actionpack-3.0.12 lib/action_dispatch/http/upload.rb