extract method

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

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

Implementation

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

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

  switch (await readBody(request, limit: limit)) {
    case Err(:final error):
      return Err(error);
    case Ok(:final value):
      try {
        return Ok(MultipartForm(await _readParts(value, boundary)));
      } on Object catch (error) {
        return Err(Rejection.badRequest('malformed multipart body: $error'));
      }
  }
}