2010-01-16 07:17:03 -05:00
|
|
|
module ActionDispatch
|
|
|
|
module Http
|
2010-10-04 19:56:45 -04:00
|
|
|
class UploadedFile
|
2010-09-22 20:35:21 -04:00
|
|
|
attr_accessor :original_filename, :content_type, :tempfile, :headers
|
2010-01-16 07:17:03 -05:00
|
|
|
|
2010-09-22 20:35:21 -04:00
|
|
|
def initialize(hash)
|
2011-06-14 03:59:18 -04:00
|
|
|
@original_filename = encode_filename(hash[:filename])
|
2010-09-22 20:35:21 -04:00
|
|
|
@content_type = hash[:type]
|
|
|
|
@headers = hash[:head]
|
2010-10-04 19:56:45 -04:00
|
|
|
@tempfile = hash[:tempfile]
|
2010-10-04 20:08:25 -04:00
|
|
|
raise(ArgumentError, ':tempfile is required') unless @tempfile
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
2010-09-22 20:35:21 -04:00
|
|
|
|
2010-11-18 13:10:18 -05:00
|
|
|
def open
|
|
|
|
@tempfile.open
|
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
@tempfile.path
|
|
|
|
end
|
|
|
|
|
2010-10-04 22:28:40 -04:00
|
|
|
def read(*args)
|
|
|
|
@tempfile.read(*args)
|
2010-10-04 20:11:50 -04:00
|
|
|
end
|
|
|
|
|
2010-10-04 22:28:40 -04:00
|
|
|
def rewind
|
|
|
|
@tempfile.rewind
|
|
|
|
end
|
|
|
|
|
|
|
|
def size
|
|
|
|
@tempfile.size
|
2010-10-04 19:56:45 -04:00
|
|
|
end
|
2011-06-14 03:59:18 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
def encode_filename(filename)
|
2011-06-14 10:58:06 -04:00
|
|
|
# Encode the filename in the utf8 encoding, unless it is nil or we're in 1.8
|
2011-06-14 03:59:18 -04:00
|
|
|
if "ruby".encoding_aware? && filename
|
2011-06-14 10:58:06 -04:00
|
|
|
filename.force_encoding("UTF-8").encode!
|
2011-06-14 03:59:18 -04:00
|
|
|
else
|
|
|
|
filename
|
|
|
|
end
|
|
|
|
end
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
module Upload
|
2010-06-11 06:15:34 -04:00
|
|
|
# Convert nested Hash to HashWithIndifferentAccess and replace
|
|
|
|
# file upload hash with UploadedFile objects
|
2010-01-16 07:17:03 -05:00
|
|
|
def normalize_parameters(value)
|
|
|
|
if Hash === value && value.has_key?(:tempfile)
|
2010-09-22 20:35:21 -04:00
|
|
|
UploadedFile.new(value)
|
2010-01-16 07:17:03 -05:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :normalize_parameters
|
|
|
|
end
|
|
|
|
end
|
2010-09-22 20:35:21 -04:00
|
|
|
end
|