Skip to content

Commit 662fef2

Browse files
committed
Normalize filename for single file downloads
Closes gh-50087
1 parent 43a5932 commit 662fef2

4 files changed

Lines changed: 37 additions & 20 deletions

File tree

spring-boot-project/spring-boot-tools/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.cli.command.init;
1818

19+
import java.io.File;
1920
import java.io.IOException;
2021
import java.net.URI;
2122
import java.nio.charset.Charset;
@@ -242,7 +243,7 @@ private String extractFileName(Header header) {
242243
value = value.substring(start + FILENAME_HEADER_PREFIX.length());
243244
int end = value.indexOf('\"');
244245
if (end != -1) {
245-
return value.substring(0, end);
246+
return new File(value.substring(0, end)).getName();
246247
}
247248
}
248249
}

spring-boot-project/spring-boot-tools/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private void extractFromStream(ZipInputStream zipStream, boolean overwrite, File
131131
}
132132

133133
private void writeProject(ProjectGenerationResponse entity, String output, boolean overwrite) throws IOException {
134-
File outputFile = new File(output);
134+
File outputFile = new File(System.getProperty("user.dir"), output);
135135
if (outputFile.exists()) {
136136
if (!overwrite) {
137137
throw new ReportableException(

spring-boot-project/spring-boot-tools/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import joptsimple.OptionSet;
2828
import org.apache.hc.core5.http.HttpHost;
29+
import org.assertj.core.api.SoftAssertionsProvider.ThrowingRunnable;
2930
import org.junit.jupiter.api.Test;
3031
import org.junit.jupiter.api.extension.ExtendWith;
3132
import org.junit.jupiter.api.io.TempDir;
@@ -213,26 +214,30 @@ void generateProjectAndExtractUnknownContentType(@TempDir File tempDir) {
213214

214215
@Test
215216
void fileNotOverwrittenByDefault(@TempDir File tempDir) throws Exception {
216-
File file = new File(tempDir, "test.file");
217-
file.createNewFile();
218-
long fileLength = file.length();
219-
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip",
220-
file.getAbsolutePath());
221-
mockSuccessfulProjectGeneration(request);
222-
assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR);
223-
assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength);
217+
withUserDir(tempDir, () -> {
218+
File file = new File(tempDir, "test.file");
219+
file.createNewFile();
220+
long fileLength = file.length();
221+
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip",
222+
file.getAbsolutePath());
223+
mockSuccessfulProjectGeneration(request);
224+
assertThat(this.command.run()).as("Should have failed").isEqualTo(ExitStatus.ERROR);
225+
assertThat(file.length()).as("File should not have changed").isEqualTo(fileLength);
226+
});
224227
}
225228

226229
@Test
227230
void overwriteFile(@TempDir File tempDir) throws Exception {
228-
File file = new File(tempDir, "test.file");
229-
file.createNewFile();
230-
long fileLength = file.length();
231-
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip",
232-
file.getAbsolutePath());
233-
mockSuccessfulProjectGeneration(request);
234-
assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK);
235-
assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length());
231+
withUserDir(tempDir, () -> {
232+
File file = new File(tempDir, "test.file");
233+
file.createNewFile();
234+
long fileLength = file.length();
235+
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest("application/zip",
236+
file.getAbsolutePath());
237+
mockSuccessfulProjectGeneration(request);
238+
assertThat(this.command.run("--force")).isEqualTo(ExitStatus.OK);
239+
assertThat(fileLength).as("File should have changed").isNotEqualTo(file.length());
240+
});
236241
}
237242

238243
@Test
@@ -399,6 +404,17 @@ void userAgent() throws Exception {
399404
request.getHeaders("User-Agent")[0].getValue().startsWith("SpringBootCli/"))), isNull());
400405
}
401406

407+
private void withUserDir(File userDir, ThrowingRunnable action) throws Exception {
408+
String previous = System.getProperty("user.dir");
409+
System.setProperty("user.dir", userDir.getAbsolutePath());
410+
try {
411+
action.run();
412+
}
413+
finally {
414+
System.setProperty("user.dir", previous);
415+
}
416+
}
417+
402418
private byte[] createFakeZipArchive(String fileName, String content) throws IOException {
403419
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
404420
try (ZipOutputStream zos = new ZipOutputStream(bos)) {

spring-boot-project/spring-boot-tools/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ void loadMetadata() throws Exception {
4949
void generateSimpleProject() throws Exception {
5050
ProjectGenerationRequest request = new ProjectGenerationRequest();
5151
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest("application/xml",
52-
"foo.zip");
52+
"nested/path/foo.zip");
5353
ProjectGenerationResponse entity = generateProject(request, mockHttpRequest);
54-
assertProjectEntity(entity, mockHttpRequest.contentType, mockHttpRequest.fileName);
54+
assertProjectEntity(entity, mockHttpRequest.contentType, "foo.zip");
5555
}
5656

5757
@Test

0 commit comments

Comments
 (0)