Collecting and decoding streaming request body

Hi all, first post.

I got this working but I'm not sure this is a good way of doing this.

I'm processing a POST request on an async middleware's function. The body is encoded as a multipart form. If I use content.decode on the request I get nothing as the body is being streamed. I can collect the body into a ByteStream, make a new request with the collected body and then use content.decode. This works fine but seems a bit cumbersome...

Is there no way to collect the body "in place" and then just use content.decode on the original request? Or maybe set up the middleware so the body is collected before it's called (like in a route)

Here's the code:

let bodyBuffer:ByteBuffer? =
    if let buffer = req.body.data {
        buffer
    } else if let buffer = try? await req.body.collect(upTo: Int.max) {
        buffer
    } else {
        nil
    }
guard let bodyBuffer else {
    return k_response.emptyOk()
}
let requestWithBody = Request(application: req.application, method: req.method, url: req.url, version: req.version, headers: req.headers, collectedBody: bodyBuffer,on:req.eventLoop)
var info = try requestWithBody.content.decode(MyStruct.self)

Ok... Digging through the decode functions I see streaming body is not supported...

But I found it's possible to use the decode function directly so no need of making a new request:

let bodyBuffer:ByteBuffer? =
    if let buffer = req.body.data {
        buffer
    } else if let buffer = try? await req.body.collect(upTo: Int.max) {
        buffer
    } else {
         nil
    }
guard let bodyBuffer,
      let contentType = req.content.contentType else {
    return k_response.emptyOk()
}
let decoder = try ContentConfiguration.global.requireDecoder(for: contentType)
var info = try decoder.decode(MyStruct.self, from: bodyBuffer, headers: req.headers)

Edit: Formatting