class UploadField < Field
- Inherits from
Field
Definitions
def process(name, upload)
Process an upload while its multipart input is available.
The upload handler must consume or store the streaming upload before parsing can continue. Its return value, or any validation failure, is preserved as an internal value for the later field validation phase.
Implementation
def process(name, upload)
upload = Upload.new(upload, size_limit: @size_limit)
if @accept
media_type = upload.media_type
unless media_type
return Value::Invalid.new(:unsupported_media_type, media_type:, accepted: @accept)
end
unless @accept.include?(media_type)
return Value::Invalid.new(:unsupported_media_type, media_type:, accepted: @accept)
end
end
begin
stored = yield(name, upload)
upload.discard
return Value::Uploaded.new(stored)
rescue Upload::LimitError
return Value::Invalid.new(:too_large, limit: upload.size_limit, size: upload.size)
end
end
def apply_upload(value, errors, path, &block)
Apply a streaming upload outcome, rejecting ordinary parameter values:
Implementation
def apply_upload(value, errors, path, &block)
if value.respond_to?(:apply_upload)
return value.apply_upload(errors, path, &block)
end
errors << Error.new(path, :invalid_type, expected: :upload, value: Value.materialize(value))
return false
end