Automatically clean up multipart temp files
This commit ensures that any resources created for multipart handling, obtained via ServerWebExchange.getMultipartData(), are automatically deleted after handling the completing the response. Resource for parts obtained via BodyExtractors::toMultipartData and BodyExtractors::toParts are not cleaned automatically, and should be cleaned via Part::delete. Closes gh-27633
This commit is contained in:
@@ -137,6 +137,9 @@ public abstract class BodyExtractors {
|
||||
|
||||
/**
|
||||
* Extractor to read multipart data into a {@code MultiValueMap<String, Part>}.
|
||||
* <p><strong>Note:</strong> that resources used for part handling,
|
||||
* like storage for the uploaded files, is not deleted automatically, but
|
||||
* should be done via {@link Part#delete()}.
|
||||
* @return {@code BodyExtractor} for multipart data
|
||||
*/
|
||||
// Parameterized for server-side use
|
||||
@@ -151,6 +154,9 @@ public abstract class BodyExtractors {
|
||||
|
||||
/**
|
||||
* Extractor to read multipart data into {@code Flux<Part>}.
|
||||
* <p><strong>Note:</strong> that resources used for part handling,
|
||||
* like storage for the uploaded files, is not deleted automatically, but
|
||||
* should be done via {@link Part#delete()}.
|
||||
* @return {@code BodyExtractor} for multipart request parts
|
||||
*/
|
||||
// Parameterized for server-side use
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.core.scheduler.Schedulers;
|
||||
import reactor.test.StepVerifier;
|
||||
@@ -168,7 +169,9 @@ class MultipartIntegrationTests extends AbstractRouterFunctionIntegrationTests {
|
||||
assertThat(parts.size()).isEqualTo(2);
|
||||
assertThat(((FilePart) parts.get("fooPart")).filename()).isEqualTo("foo.txt");
|
||||
assertThat(((FormFieldPart) parts.get("barPart")).value()).isEqualTo("bar");
|
||||
return ServerResponse.ok().build();
|
||||
return Flux.fromIterable(parts.values())
|
||||
.concatMap(Part::delete)
|
||||
.then(ServerResponse.ok().build());
|
||||
}
|
||||
catch(Exception e) {
|
||||
return Mono.error(e);
|
||||
@@ -183,7 +186,9 @@ class MultipartIntegrationTests extends AbstractRouterFunctionIntegrationTests {
|
||||
assertThat(parts.size()).isEqualTo(2);
|
||||
assertThat(((FilePart) parts.get(0)).filename()).isEqualTo("foo.txt");
|
||||
assertThat(((FormFieldPart) parts.get(1)).value()).isEqualTo("bar");
|
||||
return ServerResponse.ok().build();
|
||||
return Flux.fromIterable(parts)
|
||||
.concatMap(Part::delete)
|
||||
.then(ServerResponse.ok().build());
|
||||
}
|
||||
catch(Exception e) {
|
||||
return Mono.error(e);
|
||||
|
||||
Reference in New Issue
Block a user