Skip to content

Commit 7b34bf2

Browse files
committed
Synchronoss should create temp directory lazily
The SynchronossPartHttpMessageReader should only create temp directory when needed, not at startup. Closes gh-27092
1 parent c5a138a commit 7b34bf2

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

spring-web/src/main/java/org/springframework/http/codec/multipart/SynchronossPartHttpMessageReader.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.http.codec.multipart;
1818

1919
import java.io.IOException;
20-
import java.io.UncheckedIOException;
2120
import java.nio.channels.Channels;
2221
import java.nio.channels.FileChannel;
2322
import java.nio.channels.ReadableByteChannel;
@@ -31,6 +30,7 @@
3130
import java.util.Map;
3231
import java.util.Optional;
3332
import java.util.concurrent.atomic.AtomicInteger;
33+
import java.util.concurrent.atomic.AtomicReference;
3434
import java.util.function.Consumer;
3535

3636
import org.synchronoss.cloud.nio.multipart.DefaultPartBodyStreamStorageFactory;
@@ -46,6 +46,7 @@
4646
import reactor.core.publisher.FluxSink;
4747
import reactor.core.publisher.Mono;
4848
import reactor.core.publisher.SignalType;
49+
import reactor.core.scheduler.Schedulers;
4950

5051
import org.springframework.core.ResolvableType;
5152
import org.springframework.core.codec.DecodingException;
@@ -93,7 +94,7 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
9394

9495
private int maxParts = -1;
9596

96-
private Path fileStorageDirectory = createTempDirectory();
97+
private final AtomicReference<Path> fileStorageDirectory = new AtomicReference<>();
9798

9899

99100
/**
@@ -178,29 +179,46 @@ public boolean canRead(ResolvableType elementType, @Nullable MediaType mediaType
178179

179180
@Override
180181
public Flux<Part> read(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
181-
return Flux.create(new SynchronossPartGenerator(message, this.fileStorageDirectory))
182-
.doOnNext(part -> {
183-
if (!Hints.isLoggingSuppressed(hints)) {
184-
LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Parsed " +
185-
(isEnableLoggingRequestDetails() ?
186-
LogFormatUtils.formatValue(part, !traceOn) :
187-
"parts '" + part.name() + "' (content masked)"));
188-
}
189-
});
182+
return getFileStorageDirectory().flatMapMany(directory ->
183+
Flux.create(new SynchronossPartGenerator(message, directory))
184+
.doOnNext(part -> {
185+
if (!Hints.isLoggingSuppressed(hints)) {
186+
LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Parsed " +
187+
(isEnableLoggingRequestDetails() ?
188+
LogFormatUtils.formatValue(part, !traceOn) :
189+
"parts '" + part.name() + "' (content masked)"));
190+
}
191+
}));
190192
}
191193

192194
@Override
193195
public Mono<Part> readMono(ResolvableType elementType, ReactiveHttpInputMessage message, Map<String, Object> hints) {
194196
return Mono.error(new UnsupportedOperationException("Cannot read multipart request body into single Part"));
195197
}
196198

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-
}
199+
private Mono<Path> getFileStorageDirectory() {
200+
return Mono.defer(() -> {
201+
Path directory = this.fileStorageDirectory.get();
202+
if (directory != null) {
203+
return Mono.just(directory);
204+
}
205+
else {
206+
return Mono.fromCallable(() -> {
207+
Path tempDirectory = Files.createTempDirectory(FILE_STORAGE_DIRECTORY_PREFIX);
208+
if (this.fileStorageDirectory.compareAndSet(null, tempDirectory)) {
209+
return tempDirectory;
210+
}
211+
else {
212+
try {
213+
Files.delete(tempDirectory);
214+
}
215+
catch (IOException ignored) {
216+
}
217+
return this.fileStorageDirectory.get();
218+
}
219+
}).subscribeOn(Schedulers.boundedElastic());
220+
}
221+
});
204222
}
205223

206224

0 commit comments

Comments
 (0)