extract method

  1. @override
Future<Result<StreamedMultipart, Rejection>> extract(
  1. Request request
)
override

Produces the value, or the rejection that stops the handler.

Implementation

@override
Future<Result<StreamedMultipart, Rejection>> extract(Request request) async {
  final parts = RequestParts.of(request);
  if (parts.mediaType != 'multipart/form-data') {
    return const Err(
      Rejection.unsupportedMediaType('expected multipart/form-data'),
    );
  }

  final boundary = parts.contentType?.parameters['boundary'];
  if (boundary == null) {
    return const Err(
      Rejection.badRequest('multipart body declares no boundary'),
    );
  }

  final effective = effectiveBodyLimit(request, limit);

  // Checked up front when the client declared one, so an oversized upload is
  // refused before a byte of it is read.
  final declared = parts.contentLength;
  if (declared != null && declared > effective) {
    return Err(_tooLarge(effective));
  }

  return Ok(
    StreamedMultipart(
      _partsOf(_capped(request.read(), effective), boundary),
    ),
  );
}