2010-01-16 07:17:03 -05:00
|
|
|
module ActionDispatch
|
|
|
|
module Http
|
2012-09-22 17:56:03 -04:00
|
|
|
# Models uploaded files.
|
|
|
|
#
|
|
|
|
# The actual file is accessible via the +tempfile+ accessor, though some
|
|
|
|
# of its interface is available directly for convenience.
|
|
|
|
#
|
|
|
|
# Uploaded files are temporary files whose lifespan is one request. When
|
|
|
|
# the object is finalized Ruby unlinks the file, so there is not need to
|
|
|
|
# clean them with a separate maintenance task.
|
2010-10-04 19:56:45 -04:00
|
|
|
class UploadedFile
|
2012-09-22 17:56:03 -04:00
|
|
|
# The basename of the file in the client.
|
|
|
|
attr_accessor :original_filename
|
2010-01-16 07:17:03 -05:00
|
|
|
|
2012-09-22 17:56:03 -04:00
|
|
|
# A string with the MIME type of the file.
|
|
|
|
attr_accessor :content_type
|
|
|
|
|
|
|
|
# A +Tempfile+ object with the actual uploaded file. Note that some of
|
|
|
|
# its interface is available directly.
|
|
|
|
attr_accessor :tempfile
|
|
|
|
|
2013-01-03 16:49:28 -05:00
|
|
|
# A string with the headers of the multipart request.
|
2012-09-22 17:56:03 -04:00
|
|
|
attr_accessor :headers
|
|
|
|
|
|
|
|
def initialize(hash) # :nodoc:
|
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
|
|
|
|
2012-09-22 17:56:03 -04:00
|
|
|
# Shortcut for +tempfile.read+.
|
|
|
|
def read(length=nil, buffer=nil)
|
|
|
|
@tempfile.read(length, buffer)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut for +tempfile.open+.
|
|
|
|
def open
|
|
|
|
@tempfile.open
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut for +tempfile.close+.
|
|
|
|
def close(unlink_now=false)
|
|
|
|
@tempfile.close(unlink_now)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut for +tempfile.path+.
|
|
|
|
def path
|
|
|
|
@tempfile.path
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut for +tempfile.rewind+.
|
|
|
|
def rewind
|
|
|
|
@tempfile.rewind
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut for +tempfile.size+.
|
|
|
|
def size
|
|
|
|
@tempfile.size
|
|
|
|
end
|
|
|
|
|
|
|
|
# Shortcut for +tempfile.eof?+.
|
|
|
|
def eof?
|
|
|
|
@tempfile.eof?
|
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
|
|
|
|
|
2012-09-22 17:56:03 -04:00
|
|
|
module Upload # :nodoc:
|
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
|