@@ -93,6 +93,8 @@ public class DefaultServerWebExchange implements ServerWebExchange {
93
93
94
94
private final Mono <MultiValueMap <String , Part >> multipartDataMono ;
95
95
96
+ private volatile boolean multipartRead = false ;
97
+
96
98
@ Nullable
97
99
private final ApplicationContext applicationContext ;
98
100
@@ -131,7 +133,7 @@ public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse re
131
133
this .sessionMono = sessionManager .getSession (this ).cache ();
132
134
this .localeContextResolver = localeContextResolver ;
133
135
this .formDataMono = initFormData (request , codecConfigurer , getLogPrefix ());
134
- this .multipartDataMono = initMultipartData (request , codecConfigurer , getLogPrefix ());
136
+ this .multipartDataMono = initMultipartData (codecConfigurer , getLogPrefix ());
135
137
this .applicationContext = applicationContext ;
136
138
}
137
139
@@ -154,10 +156,9 @@ private static Mono<MultiValueMap<String, String>> initFormData(ServerHttpReques
154
156
.cache ();
155
157
}
156
158
157
- private static Mono <MultiValueMap <String , Part >> initMultipartData (ServerHttpRequest request ,
158
- ServerCodecConfigurer configurer , String logPrefix ) {
159
+ private Mono <MultiValueMap <String , Part >> initMultipartData (ServerCodecConfigurer configurer , String logPrefix ) {
159
160
160
- MediaType contentType = getContentType (request );
161
+ MediaType contentType = getContentType (this . request );
161
162
if (contentType == null || !contentType .getType ().equalsIgnoreCase ("multipart" )) {
162
163
return EMPTY_MULTIPART_DATA ;
163
164
}
@@ -168,7 +169,8 @@ private static Mono<MultiValueMap<String, Part>> initMultipartData(ServerHttpReq
168
169
}
169
170
170
171
return reader
171
- .readMono (MULTIPART_DATA_TYPE , request , Hints .from (Hints .LOG_PREFIX_HINT , logPrefix ))
172
+ .readMono (MULTIPART_DATA_TYPE , this .request , Hints .from (Hints .LOG_PREFIX_HINT , logPrefix ))
173
+ .doOnNext (ignored -> this .multipartRead = true )
172
174
.switchIfEmpty (EMPTY_MULTIPART_DATA )
173
175
.cache ();
174
176
}
@@ -243,6 +245,22 @@ public Mono<MultiValueMap<String, Part>> getMultipartData() {
243
245
return this .multipartDataMono ;
244
246
}
245
247
248
+ @ Override
249
+ public Mono <Void > cleanupMultipart () {
250
+ if (this .multipartRead ) {
251
+ return getMultipartData ()
252
+ .onErrorResume (t -> Mono .empty ()) // ignore errors reading multipart data
253
+ .flatMapIterable (Map ::values )
254
+ .flatMapIterable (Function .identity ())
255
+ .flatMap (part -> part .delete ()
256
+ .onErrorResume (ex -> Mono .empty ()))
257
+ .then ();
258
+ }
259
+ else {
260
+ return Mono .empty ();
261
+ }
262
+ }
263
+
246
264
@ Override
247
265
public LocaleContext getLocaleContext () {
248
266
return this .localeContextResolver .resolveLocaleContext (this );
0 commit comments