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)
|
2012-03-06 16:34:20 -05:00
|
|
|
@tempfile = hash[:tempfile]
|
|
|
|
raise(ArgumentError, ':tempfile is required') unless @tempfile
|
|
|
|
|
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-01-16 07:17:03 -05:00
|
|
|
end
|
2010-09-22 20:35:21 -04:00
|
|
|
|
2010-10-04 22:28:40 -04:00
|
|
|
def read(*args)
|
|
|
|
@tempfile.read(*args)
|
2010-10-04 20:11:50 -04:00
|
|
|
end
|
|
|
|
|
2011-08-28 00:24:58 -04:00
|
|
|
# Delegate these methods to the tempfile.
|
|
|
|
[:open, :path, :rewind, :size].each do |method|
|
|
|
|
class_eval "def #{method}; @tempfile.#{method}; end"
|
2010-10-04 19:56:45 -04:00
|
|
|
end
|
2012-03-06 16:34:20 -05:00
|
|
|
|
2011-06-14 03:59:18 -04:00
|
|
|
private
|
2012-03-06 16:34:20 -05:00
|
|
|
|
2011-06-14 03:59:18 -04:00
|
|
|
def encode_filename(filename)
|
2011-12-24 07:57:54 -05:00
|
|
|
# Encode the filename in the utf8 encoding, unless it is nil
|
2012-03-06 16:34:20 -05:00
|
|
|
filename.force_encoding("UTF-8").encode! if filename
|
2011-06-14 03:59:18 -04:00
|
|
|
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
|