Skip to content

Commit 85ad63b

Browse files
committed
Ensure Synchronoss temp directories do not collide
This commit makes sure that Synchronoss uses a random temporary directory to store uploaded files, so that two instances do not collide. Closes gh-26931
1 parent a2ff769 commit 85ad63b

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,11 +17,13 @@
1717
package org.springframework.http.codec.multipart;
1818

1919
import java.io.IOException;
20+
import java.io.UncheckedIOException;
2021
import java.nio.channels.Channels;
2122
import java.nio.channels.FileChannel;
2223
import java.nio.channels.ReadableByteChannel;
2324
import java.nio.charset.Charset;
2425
import java.nio.charset.StandardCharsets;
26+
import java.nio.file.Files;
2527
import java.nio.file.OpenOption;
2628
import java.nio.file.Path;
2729
import java.nio.file.StandardOpenOption;
@@ -82,13 +84,17 @@ public class SynchronossPartHttpMessageReader extends LoggingCodecSupport implem
8284
// Static DataBufferFactory to copy from FileInputStream or wrap bytes[].
8385
private static final DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
8486

87+
private static final String FILE_STORAGE_DIRECTORY_PREFIX = "synchronoss-file-upload-";
88+
8589

8690
private int maxInMemorySize = 256 * 1024;
8791

8892
private long maxDiskUsagePerPart = -1;
8993

9094
private int maxParts = -1;
9195

96+
private Path fileStorageDirectory = createTempDirectory();
97+
9298

9399
/**
94100
* 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
172178

173179
@Override
174180
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))
176182
.doOnNext(part -> {
177183
if (!Hints.isLoggingSuppressed(hints)) {
178184
LogFormatUtils.traceDebug(logger, traceOn -> Hints.getLogPrefix(hints) + "Parsed " +
@@ -188,6 +194,15 @@ public Mono<Part> readMono(ResolvableType elementType, ReactiveHttpInputMessage
188194
return Mono.error(new UnsupportedOperationException("Cannot read multipart request body into single Part"));
189195
}
190196

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+
191206

192207
/**
193208
* Subscribe to the input stream and feed the Synchronoss parser. Then listen
@@ -199,14 +214,17 @@ private class SynchronossPartGenerator extends BaseSubscriber<DataBuffer> implem
199214

200215
private final LimitedPartBodyStreamStorageFactory storageFactory = new LimitedPartBodyStreamStorageFactory();
201216

217+
private final Path fileStorageDirectory;
218+
202219
@Nullable
203220
private NioMultipartParserListener listener;
204221

205222
@Nullable
206223
private NioMultipartParser parser;
207224

208-
public SynchronossPartGenerator(ReactiveHttpInputMessage inputMessage) {
225+
public SynchronossPartGenerator(ReactiveHttpInputMessage inputMessage, Path fileStorageDirectory) {
209226
this.inputMessage = inputMessage;
227+
this.fileStorageDirectory = fileStorageDirectory;
210228
}
211229

212230
@Override
@@ -223,6 +241,7 @@ public void accept(FluxSink<Part> sink) {
223241

224242
this.parser = Multipart
225243
.multipart(context)
244+
.saveTemporaryFilesTo(this.fileStorageDirectory.toString())
226245
.usePartBodyStreamStorageFactory(this.storageFactory)
227246
.forNIO(this.listener);
228247

0 commit comments

Comments
 (0)