@Override
    @NonNull
    public Mono<Void> filter(@NonNull final ServerWebExchange exchange, @NonNull final WebFilterChain chain) {
        MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
        if (MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)) {
            ServerRequest serverRequest = ServerRequest.create(exchange,
                    messageReaders);
            return serverRequest.bodyToMono(DataBuffer.class)
                    .flatMap(dataBuffer -> {
                        if (dataBuffer.capacity() > Constants.BYTES_PER_MB * fileMaxSize) {
                            ServerHttpResponse response = exchange.getResponse();
                            response.setStatusCode(HttpStatus.BAD_REQUEST);
                            Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.PAYLOAD_TOO_LARGE);
                            LOG.info("The file size exceeds the limit. The actual size is {}M , response:{}", dataBuffer.capacity() / Constants.BYTES_PER_MB, error);
                            return WebFluxResultUtils.result(exchange, error);
                        }
                        BodyInserter<Mono<DataBuffer>, ReactiveHttpOutputMessage> bodyInsert = BodyInserters.fromPublisher(Mono.just(dataBuffer), DataBuffer.class);
                        HttpHeaders headers = new HttpHeaders();
                        headers.putAll(exchange.getRequest().getHeaders());
                        headers.remove(HttpHeaders.CONTENT_LENGTH);
                        CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(
                                exchange, headers);
                        return bodyInsert.insert(outputMessage, new BodyInserterContext())
                                .then(Mono.defer(() -> {
                                    ServerHttpRequest decorator = decorate(exchange, outputMessage);
                                    return chain.filter(exchange.mutate().request(decorator).build());
                                })).doFinally(signalType -> DataBufferUtils.release(dataBuffer))
                                .onErrorResume((Function<Throwable, Mono<Void>>) throwable -> ResponseUtils.release(outputMessage, throwable));
                    });
        }
        return chain.filter(exchange);

    }
@Test
    public void testFilter() {
        ServerWebExchange webExchange =
                MockServerWebExchange.from(MockServerHttpRequest
                        .post("http://localhost:8080")
                        .contentType(MediaType.TEXT_PLAIN)
                        .contentLength(4)
                        .body("test"));

        ServerHttpRequest mutatedRequest = webExchange.getRequest().mutate().header(CONTENT_TYPE,
                String.valueOf(MULTIPART_FORM_DATA)).build();
        webExchange = webExchange.mutate().request(mutatedRequest).build();

        WebFilterChain webFilterChain = mock(WebFilterChain.class);
        when(webFilterChain.filter(any())).thenReturn(Mono.empty());

        FileSizeFilter fileSizeFilter = new FileSizeFilter(10);
        Mono<Void> voidMono = fileSizeFilter.filter(webExchange, webFilterChain);
        StepVerifier.create(voidMono).expectSubscription().verifyComplete();

        final ServerWebExchange webExchangeTextPlain =
                MockServerWebExchange.from(MockServerHttpRequest
                        .post("http://localhost:8080")
                        .contentType(MediaType.TEXT_PLAIN)
                        .contentLength(4)
                        .body("test"));
        FileSizeFilter fileSizeFilterTextPlain = new FileSizeFilter(10);
        Mono<Void> voidMonoTextPlain = fileSizeFilterTextPlain.filter(webExchangeTextPlain, webFilterChain);
        StepVerifier.create(voidMonoTextPlain).expectSubscription().verifyComplete();

        fileSizeFilter = new FileSizeFilter(1);
        voidMono = fileSizeFilter.filter(webExchange, webFilterChain);
        StepVerifier.create(voidMono).expectSubscription().verifyComplete();

        final ServerWebExchange webExchangeError =
                MockServerWebExchange.from(MockServerHttpRequest
                        .post("http://localhost:8081")
                        .contentType(MediaType.MULTIPART_FORM_DATA)
                        .body("test"));
        // hit `size.capacity() > BYTES_PER_MB * fileMaxSize`
        FileSizeFilter fileSizeFilterError = new FileSizeFilter(-1);
        Mono<Void> voidMonoError = fileSizeFilterError.filter(webExchangeError, webFilterChain);
        StepVerifier.create(voidMonoError).expectSubscription().verifyComplete();
    }