Skip to content

Commit 2cc20ea

Browse files
authored
refactor!: change Upload to use raw instead of multipart requests (#8242)
Change the Upload component to send files as "raw" requests with the file contents as body, instead of using multipart requests.
1 parent 3b25637 commit 2cc20ea

File tree

7 files changed

+55
-11
lines changed

7 files changed

+55
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ error-screenshots
3636
**/frontend/*.tsx
3737
dev-bundle
3838
prod.bundle
39+
.vaadin-node-tasks.lock

vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/main/java/com/vaadin/flow/component/upload/tests/UploadView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ private void createSimpleUpload() {
5151
upload.addSucceededListener(event -> {
5252
try {
5353
output.add(event.getFileName());
54+
output.add(event.getMIMEType());
5455
output.add(IOUtils.toString(
5556
buffer.getInputStream(event.getFileName()), "UTF-8"));
5657
} catch (IOException e) {

vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/main/java/com/vaadin/flow/component/upload/tests/UploadWithHandlerView.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ public UploadWithHandlerView() {
3838
Div handlerEventsOutput = new Div();
3939

4040
InMemoryUploadHandler handler = UploadHandler
41-
.inMemory((metaData, bytes) -> UI.getCurrent().access(
42-
() -> output.setText(String.valueOf(bytes.length))))
43-
.whenStart(() -> handlerEventsOutput.add("started"))
41+
.inMemory((metaData, bytes) -> UI.getCurrent().access(() -> {
42+
String metaInfo = "%s-%s-%s".formatted(metaData.fileName(),
43+
metaData.contentType(),
44+
String.valueOf(bytes.length));
45+
output.setText(metaInfo);
46+
})).whenStart(() -> handlerEventsOutput.add("started"))
4447
.onProgress((transferred, total) -> handlerEventsOutput
4548
.add("-progress"))
4649
.whenComplete(success -> {

vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/AbstractUploadIT.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,22 @@ public abstract class AbstractUploadIT extends AbstractComponentIT {
3333
* @throws IOException
3434
*/
3535
File createTempFile(String extension) throws IOException {
36-
File tempFile = File.createTempFile("TestFileUpload", "." + extension);
36+
return createTempFile("TestFileUpload", extension);
37+
}
38+
39+
/**
40+
* Creates a temp file with the provided name and extension for testing
41+
* purposes.
42+
*
43+
* @param fileName
44+
* the temp file name prefix
45+
* @param extension
46+
* the temp file extension without leading dot
47+
* @return The generated temp file handle
48+
* @throws IOException
49+
*/
50+
File createTempFile(String fileName, String extension) throws IOException {
51+
File tempFile = File.createTempFile(fileName, "." + extension);
3752
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
3853
writer.write(getTempFileContents());
3954
writer.close();

vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/UploadIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ public void init() {
5151

5252
@Test
5353
public void testUploadAnyFile() throws Exception {
54-
File tempFile = createTempFile("txt");
54+
File tempFile = createTempFile("some file", "txt");
5555
getUpload().upload(tempFile);
5656

5757
String content = uploadOutput.getText();
5858

59+
String expectedContents = tempFile.getName() + "text/plain"
60+
+ getTempFileContents();
61+
5962
Assert.assertTrue("Upload content does not contain file details",
60-
content.contains(tempFile.getName() + getTempFileContents()));
63+
content.contains(expectedContents));
6164
Assert.assertTrue("Progress update event was not fired properly",
6265
content.contains("PROGRESS:" + tempFile.getName()));
6366
}

vaadin-upload-flow-parent/vaadin-upload-flow-integration-tests/src/test/java/com/vaadin/flow/component/upload/tests/UploadWithHandlerIT.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,27 @@ public void init() {
5050
}
5151

5252
@Test
53-
public void uploadWithHandler_uploadSingleFile_fileUploadedAndNoErrorThrown()
53+
public void uploadWithHandler_uploadTextFile_fileUploadedAndNoErrorThrown()
54+
throws Exception {
55+
File tempFile = createTempFile("some file", "txt");
56+
upload.upload(tempFile);
57+
58+
checkLogsForErrors();
59+
60+
Assert.assertEquals(
61+
"Unexpected handler events for upload with using upload handler",
62+
"started-completed", eventsOutput.getText());
63+
64+
String expectedMetadata = "%s-%s-%s".formatted(tempFile.getName(),
65+
"text/plain", getTempFileContents().getBytes().length);
66+
67+
Assert.assertEquals(
68+
"Unexpected file metadata for upload with upload handler",
69+
expectedMetadata, uploadOutput.getText());
70+
}
71+
72+
@Test
73+
public void uploadWithHandler_withLargeBinaryFile_fileUploadedAndNoErrorThrown()
5474
throws Exception {
5575
File tempFile = createTempBinaryFile();
5676
upload.upload(tempFile);
@@ -61,9 +81,12 @@ public void uploadWithHandler_uploadSingleFile_fileUploadedAndNoErrorThrown()
6181
"Unexpected handler events for upload with using upload handler",
6282
"started-progress-completed", eventsOutput.getText());
6383

84+
String expectedMetadata = "%s-%s-%s".formatted(tempFile.getName(),
85+
"application/octet-stream", FILE_SIZE);
86+
6487
Assert.assertEquals(
65-
"Unexpected file size for upload with upload handler",
66-
uploadOutput.getText(), String.valueOf(FILE_SIZE));
88+
"Unexpected file metadata for upload with upload handler",
89+
uploadOutput.getText(), expectedMetadata);
6790
}
6891

6992
private File createTempBinaryFile() throws IOException {

vaadin-upload-flow-parent/vaadin-upload-flow/src/main/java/com/vaadin/flow/component/upload/Upload.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ public Upload() {
170170

171171
defaultDropLabelIcon = new UploadIcon();
172172
setDropLabelIcon(defaultDropLabelIcon);
173-
174-
getElement().setProperty("uploadFormat", "multipart");
175173
}
176174

177175
/**

0 commit comments

Comments
 (0)