Class: Protocol::Content::Parameters::Upload
- Inherits:
-
Object
- Object
- Protocol::Content::Parameters::Upload
- Includes:
- Multipart::Readable
- Defined in:
- lib/protocol/content/parameters/upload.rb
Overview
A field-constrained streaming upload.
Defined Under Namespace
Classes: LimitError
Instance Attribute Summary collapse
-
#declared_media_type ⇒ Object
readonly
The media type declared by the submitting client, if present.
-
#media_type ⇒ Object
readonly
The declared media type, or the type inferred from the filename when the declaration is absent or generic.
-
#size ⇒ Object
readonly
The number of bytes consumed through this constrained upload.
-
#size_limit ⇒ Object
readonly
The maximum accepted size, if configured.
Instance Method Summary collapse
-
#discard ⇒ Object
Consume any unread upload content while enforcing the field size limit.
-
#each(chunk_size = 8192) ⇒ Object
Iterate over the upload while enforcing its field size limit.
-
#ended? ⇒ Boolean
Whether the complete upload has been consumed.
-
#filename ⇒ Object
The submitted filename.
-
#headers ⇒ Object
The multipart headers associated with this upload.
-
#initialize(delegate, size_limit: nil) ⇒ Upload
constructor
Initialize a constrained upload.
Constructor Details
#initialize(delegate, size_limit: nil) ⇒ Upload
Initialize a constrained upload.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/protocol/content/parameters/upload.rb', line 27 def initialize(delegate, size_limit: nil) @delegate = delegate @size_limit = size_limit @size = 0 @declared_media_type = nil if header = delegate.headers["content-type"] @declared_media_type = Protocol::Media::Type.parse(header.to_s) end @media_type = @declared_media_type # Fall back to the submitted filename when the declared type carries no useful classification: if !@media_type || @media_type.name == GENERIC_MEDIA_TYPE if record = Protocol::Media::Registry.for_path(self.filename) if record.type.name != GENERIC_MEDIA_TYPE @media_type = record.type end end end end |
Instance Attribute Details
#declared_media_type ⇒ Object (readonly)
The media type declared by the submitting client, if present.
60 61 62 |
# File 'lib/protocol/content/parameters/upload.rb', line 60 def declared_media_type @declared_media_type end |
#media_type ⇒ Object (readonly)
The declared media type, or the type inferred from the filename when the declaration is absent or generic.
63 64 65 |
# File 'lib/protocol/content/parameters/upload.rb', line 63 def media_type @media_type end |
#size ⇒ Object (readonly)
The number of bytes consumed through this constrained upload.
69 70 71 |
# File 'lib/protocol/content/parameters/upload.rb', line 69 def size @size end |
#size_limit ⇒ Object (readonly)
The maximum accepted size, if configured.
66 67 68 |
# File 'lib/protocol/content/parameters/upload.rb', line 66 def size_limit @size_limit end |
Instance Method Details
#discard ⇒ Object
Consume any unread upload content while enforcing the field size limit.
99 100 101 102 |
# File 'lib/protocol/content/parameters/upload.rb', line 99 def discard each {|_chunk|} return nil end |
#each(chunk_size = 8192) ⇒ Object
Iterate over the upload while enforcing its field size limit.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/protocol/content/parameters/upload.rb', line 81 def each(chunk_size = 8192) return to_enum(:each, chunk_size) unless block_given? @delegate.each(chunk_size) do |chunk| @size += chunk.bytesize if @size_limit && @size > @size_limit raise LimitError, "Upload size exceeded field limit of #{@size_limit}!" end yield chunk end return self end |
#ended? ⇒ Boolean
Whether the complete upload has been consumed.
72 73 74 |
# File 'lib/protocol/content/parameters/upload.rb', line 72 def ended? return @delegate.ended? end |
#filename ⇒ Object
The submitted filename.
50 51 52 |
# File 'lib/protocol/content/parameters/upload.rb', line 50 def filename return @delegate.filename end |
#headers ⇒ Object
The multipart headers associated with this upload.
55 56 57 |
# File 'lib/protocol/content/parameters/upload.rb', line 55 def headers return @delegate.headers end |