Class: Protocol::Multipart::FormData::Upload

Inherits:
Object
  • Object
show all
Includes:
Readable
Defined in:
lib/protocol/multipart/form_data.rb

Overview

A file upload yielded while parsing form data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Readable

#copy_to, #save

Constructor Details

#initialize(part, filename, size_limit, total_size_limit) ⇒ Upload

Initialize a streamed file upload.



38
39
40
41
42
43
# File 'lib/protocol/multipart/form_data.rb', line 38

def initialize(part, filename, size_limit, total_size_limit)
	@part = part
	@filename = filename
	@size_limit = ByteLimit.new(size_limit, name: :upload_size)
	@total_size_limit = total_size_limit
end

Instance Attribute Details

#filenameObject (readonly)

The submitted filename.



46
47
48
# File 'lib/protocol/multipart/form_data.rb', line 46

def filename
  @filename
end

Instance Method Details

#discardObject

Consume any unread upload body while applying its limits.



78
79
80
81
# File 'lib/protocol/multipart/form_data.rb', line 78

def discard
	self.each{|chunk|}
	return nil
end

#each(chunk_size = 8192) ⇒ Object

Iterate over the upload body.



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/protocol/multipart/form_data.rb', line 65

def each(chunk_size = 8192)
	return to_enum(:each, chunk_size) unless block_given?
	
	@part.each(chunk_size) do |chunk|
		@size_limit.consume(chunk.bytesize)
		@total_size_limit.consume(chunk.bytesize)
		yield chunk
	end
	
	return self
end

#ended?Boolean

Whether the complete upload has been consumed.

Returns:

  • (Boolean)


59
60
61
# File 'lib/protocol/multipart/form_data.rb', line 59

def ended?
	@part.ended?
end

#headersObject

The multipart headers associated with this upload.



49
50
51
# File 'lib/protocol/multipart/form_data.rb', line 49

def headers
	@part.headers
end

#sizeObject

The number of upload bytes consumed so far.



54
55
56
# File 'lib/protocol/multipart/form_data.rb', line 54

def size
	@size_limit.size
end