1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionpack/lib/action_dispatch/http/upload.rb

46 lines
1.2 KiB
Ruby
Raw Normal View History

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
def read(*args)
@tempfile.read(*args)
2010-10-04 20:11:50 -04:00
end
# Delegate these methods to the tempfile.
[:open, :path, :rewind, :size, :eof?].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
filename.force_encoding("UTF-8").encode! if filename
end
end
module Upload
2010-06-11 06:15:34 -04:00
# 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