Class: Protocol::Content::Parameters::Upload

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_typeObject (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_typeObject (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

#sizeObject (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_limitObject (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

#discardObject

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.

Returns:

  • (Boolean)


72
73
74
# File 'lib/protocol/content/parameters/upload.rb', line 72

def ended?
	return @delegate.ended?
end

#filenameObject

The submitted filename.



50
51
52
# File 'lib/protocol/content/parameters/upload.rb', line 50

def filename
	return @delegate.filename
end

#headersObject

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