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
|
2013-03-19 13:40:54 -04:00
|
|
|
# the object is finalized Ruby unlinks the file, so there is no need to
|
2012-09-22 17:56:03 -04:00
|
|
|
# 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
|
2014-04-17 13:48:51 -04:00
|
|
|
alias :to_io :tempfile
|
2012-09-22 17:56:03 -04:00
|
|
|
|
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
|
|
|
|
|
2014-07-15 18:31:31 -04:00
|
|
|
@original_filename = hash[:filename]
|
2014-07-16 14:35:27 -04:00
|
|
|
@original_filename &&= @original_filename.encode "UTF-8"
|
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
|
2010-01-16 07:17:03 -05:00
|
|
|
end
|
|
|
|
end
|
2010-09-22 20:35:21 -04:00
|
|
|
end
|