Class: Rack::Multipart::UploadedFile
- Inherits:
-
Object
- Object
- Rack::Multipart::UploadedFile
- Defined in:
- lib/rack/multipart/uploaded_file.rb
Overview
Despite the misleading name, UploadedFile is designed for use for preparing multipart file upload bodies, generally for use in tests. It is not designed for and should not be used for handling uploaded files (there is no need for that, since Rack’s multipart parser already creates Tempfiles for that). Using this with non-trusted filenames can create a security vulnerability.
You should only use this class if you plan on passing the instances to Rack::MockRequest for use in creating multipart request bodies.
UploadedFile delegates most methods to the tempfile it contains.
Instance Attribute Summary collapse
-
#content_type ⇒ Object
The content type of the instance.
-
#original_filename ⇒ Object
readonly
The provided name of the file.
Instance Method Summary collapse
-
#initialize(filepath = nil, ct = "text/plain", bin = false, path: filepath, content_type: ct, binary: bin, filename: nil, io: nil) ⇒ UploadedFile
constructor
Create a new UploadedFile.
-
#method_missing(method_name, *args, &block) ⇒ Object
Delegate method missing calls to the tempfile.
-
#path ⇒ Object
(also: #local_path)
The path of the tempfile for the instance, if the tempfile has a path.
-
#respond_to_missing?(*args) ⇒ Boolean
Return true if the tempfile responds to the method.
Constructor Details
#initialize(filepath = nil, ct = "text/plain", bin = false, path: filepath, content_type: ct, binary: bin, filename: nil, io: nil) ⇒ UploadedFile
Create a new UploadedFile. For backwards compatibility, this accepts both positional and keyword versions of the same arguments:
- filepath/path
-
The path to the file
- ct/content_type
-
The content_type of the file
- bin/binary
-
Whether to set binmode on the file before copying data into it.
If both positional and keyword arguments are present, the keyword arguments take precedence.
The following keyword-only arguments are also accepted:
- filename
-
Override the filename to use for the file. This is so the filename for the upload does not need to match the basename of the file path. This should not contain slashes, unless you are trying to test how an application handles invalid filenames in multipart upload bodies.
- io
-
Use the given IO-like instance as the tempfile, instead of creating a Tempfile instance. This is useful for building multipart file upload bodies without a file being present on the filesystem. If you are providing this, you should also provide the filename argument.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rack/multipart/uploaded_file.rb', line 49 def initialize(filepath = nil, ct = "text/plain", bin = false, path: filepath, content_type: ct, binary: bin, filename: nil, io: nil) if io @tempfile = io @original_filename = filename else raise "#{path} file does not exist" unless ::File.exist?(path) @original_filename = filename || ::File.basename(path) @tempfile = Tempfile.new([@original_filename, ::File.extname(path)], encoding: Encoding::BINARY) @tempfile.binmode if binary FileUtils.copy_file(path, @tempfile.path) end @content_type = content_type end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Delegate method missing calls to the tempfile.
77 78 79 |
# File 'lib/rack/multipart/uploaded_file.rb', line 77 def method_missing(method_name, *args, &block) #:nodoc: @tempfile.__send__(method_name, *args, &block) end |
Instance Attribute Details
#content_type ⇒ Object
The content type of the instance.
26 27 28 |
# File 'lib/rack/multipart/uploaded_file.rb', line 26 def content_type @content_type end |
#original_filename ⇒ Object (readonly)
The provided name of the file. This generally is the basename of path provided during initialization, but it can contain slashes if they were present in the filename argument when the instance was created.
23 24 25 |
# File 'lib/rack/multipart/uploaded_file.rb', line 23 def original_filename @original_filename end |
Instance Method Details
#path ⇒ Object Also known as: local_path
The path of the tempfile for the instance, if the tempfile has a path. nil if the tempfile does not have a path.
66 67 68 |
# File 'lib/rack/multipart/uploaded_file.rb', line 66 def path @tempfile.path if @tempfile.respond_to?(:path) end |
#respond_to_missing?(*args) ⇒ Boolean
Return true if the tempfile responds to the method.
72 73 74 |
# File 'lib/rack/multipart/uploaded_file.rb', line 72 def respond_to_missing?(*args) @tempfile.respond_to?(*args) end |