1
1
/*
2
- * Copyright 2002-2020 the original author or authors.
2
+ * Copyright 2002-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .http .codec .multipart ;
18
18
19
19
import java .io .IOException ;
20
+ import java .io .UncheckedIOException ;
20
21
import java .nio .channels .Channels ;
21
22
import java .nio .channels .FileChannel ;
22
23
import java .nio .channels .ReadableByteChannel ;
23
24
import java .nio .charset .Charset ;
24
25
import java .nio .charset .StandardCharsets ;
26
+ import java .nio .file .Files ;
25
27
import java .nio .file .OpenOption ;
26
28
import java .nio .file .Path ;
27
29
import java .nio .file .StandardOpenOption ;
@@ -82,13 +84,17 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
82
84
// Static DataBufferFactory to copy from FileInputStream or wrap bytes[].
83
85
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory ();
84
86
87
+ private static final String FILE_STORAGE_DIRECTORY_PREFIX = "synchronoss-file-upload-" ;
88
+
85
89
86
90
private int maxInMemorySize = 256 * 1024 ;
87
91
88
92
private long maxDiskUsagePerPart = -1 ;
89
93
90
94
private int maxParts = -1 ;
91
95
96
+ private Path fileStorageDirectory = createTempDirectory ();
97
+
92
98
93
99
/**
94
100
* Configure the maximum amount of memory that is allowed to use per part.
@@ -172,7 +178,7 @@ public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType
172
178
173
179
@ Override
174
180
public Flux <Part > read (ResolvableType elementType , ReactiveHttpInputMessage message , Map <String , Object > hints ) {
175
- return Flux .create (new SynchronossPartGenerator (message ))
181
+ return Flux .create (new SynchronossPartGenerator (message , this . fileStorageDirectory ))
176
182
.doOnNext (part -> {
177
183
if (!Hints .isLoggingSuppressed (hints )) {
178
184
LogFormatUtils .traceDebug (logger , traceOn -> Hints .getLogPrefix (hints ) + "Parsed " +
@@ -188,6 +194,15 @@ public Mono<Part> readMono(ResolvableType elementType, ReactiveHttpInputMessage
188
194
return Mono .error (new UnsupportedOperationException ("Cannot read multipart request body into single Part" ));
189
195
}
190
196
197
+ private static Path createTempDirectory () {
198
+ try {
199
+ return Files .createTempDirectory (FILE_STORAGE_DIRECTORY_PREFIX );
200
+ }
201
+ catch (IOException ex ) {
202
+ throw new UncheckedIOException (ex );
203
+ }
204
+ }
205
+
191
206
192
207
/**
193
208
* Subscribe to the input stream and feed the Synchronoss parser. Then listen
@@ -199,14 +214,17 @@ private class SynchronossPartGenerator extends BaseSubscriber<DataBuffer> implem
199
214
200
215
private final LimitedPartBodyStreamStorageFactory storageFactory = new LimitedPartBodyStreamStorageFactory ();
201
216
217
+ private final Path fileStorageDirectory ;
218
+
202
219
@ Nullable
203
220
private NioMultipartParserListener listener ;
204
221
205
222
@ Nullable
206
223
private NioMultipartParser parser ;
207
224
208
- public SynchronossPartGenerator (ReactiveHttpInputMessage inputMessage ) {
225
+ public SynchronossPartGenerator (ReactiveHttpInputMessage inputMessage , Path fileStorageDirectory ) {
209
226
this .inputMessage = inputMessage ;
227
+ this .fileStorageDirectory = fileStorageDirectory ;
210
228
}
211
229
212
230
@ Override
@@ -223,6 +241,7 @@ public void accept(FluxSink<Part> sink) {
223
241
224
242
this .parser = Multipart
225
243
.multipart (context )
244
+ .saveTemporaryFilesTo (this .fileStorageDirectory .toString ())
226
245
.usePartBodyStreamStorageFactory (this .storageFactory )
227
246
.forNIO (this .listener );
228
247
0 commit comments