Skip to content

Commit 37f4fa7

Browse files
committed
Access to multipart form data file names fix jooby-project#1676
1 parent b85cbc1 commit 37f4fa7

File tree

7 files changed

+62
-4
lines changed

7 files changed

+62
-4
lines changed

jooby/src/main/java/io/jooby/FileUpload.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
* @author edgar
3232
*/
3333
public interface FileUpload {
34+
35+
/**
36+
* File key. That's the field form name, not the file name. Example:
37+
*
38+
* @return File key. That's the field form name, not the file name.
39+
*/
40+
@Nonnull String getName();
41+
3442
/**
3543
* Name of file upload.
3644
* @return Name of file upload.

modules/jooby-jetty/src/main/java/io/jooby/internal/jetty/JettyFileUpload.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.jooby.SneakyThrows;
1010
import org.eclipse.jetty.http.MultiPartFormInputStream;
1111

12+
import javax.annotation.Nonnull;
1213
import java.io.IOException;
1314
import java.io.InputStream;
1415
import java.nio.file.Files;
@@ -21,6 +22,10 @@ public JettyFileUpload(MultiPartFormInputStream.MultiPart upload) {
2122
this.upload = upload;
2223
}
2324

25+
@Nonnull @Override public String getName() {
26+
return upload.getName();
27+
}
28+
2429
@Override public String getFileName() {
2530
return upload.getSubmittedFileName();
2631
}

modules/jooby-netty/src/main/java/io/jooby/internal/netty/NettyFileUpload.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.netty.buffer.ByteBufInputStream;
1111
import io.netty.handler.codec.http.multipart.DiskFileUpload;
1212

13+
import javax.annotation.Nonnull;
1314
import java.io.IOException;
1415
import java.io.InputStream;
1516
import java.nio.file.Files;
@@ -26,6 +27,10 @@ public NettyFileUpload(Path basedir,
2627
this.upload = upload;
2728
}
2829

30+
@Nonnull @Override public String getName() {
31+
return upload.getName();
32+
}
33+
2934
@Override public byte[] bytes() {
3035
try {
3136
if (upload.isInMemory()) {

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ private void formData(Formdata form, FormData data) {
535535
Deque<FormData.FormValue> values = data.get(path);
536536
for (FormData.FormValue value : values) {
537537
if (value.isFileItem()) {
538-
((Multipart) form).put(path, new UtowFileUpload(value));
538+
((Multipart) form).put(path, new UtowFileUpload(path, value));
539539
} else {
540540
form.put(path, value.getValue());
541541
}

modules/jooby-utow/src/main/java/io/jooby/internal/utow/UtowFileUpload.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.undertow.server.handlers.form.FormData;
1212
import io.undertow.util.Headers;
1313

14+
import javax.annotation.Nonnull;
1415
import java.io.ByteArrayOutputStream;
1516
import java.io.IOException;
1617
import java.io.InputStream;
@@ -19,8 +20,10 @@
1920
public class UtowFileUpload implements FileUpload {
2021

2122
private final FormData.FormValue upload;
23+
private final String name;
2224

23-
public UtowFileUpload(FormData.FormValue upload) {
25+
public UtowFileUpload(String name, FormData.FormValue upload) {
26+
this.name = name;
2427
this.upload = upload;
2528
}
2629

@@ -46,6 +49,10 @@ public UtowFileUpload(FormData.FormValue upload) {
4649
}
4750
}
4851

52+
@Nonnull @Override public String getName() {
53+
return name;
54+
}
55+
4956
@Override public String getFileName() {
5057
return upload.getFileName();
5158
}

modules/jooby-utow/src/test/java/io/jooby/internal/utow/UtowFileUploadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void shouldGetFileSize() throws IOException {
2222
FormData.FormValue upload = mock(FormData.FormValue.class);
2323
when(upload.getFileItem()).thenReturn(fileItem);
2424

25-
assertEquals(fileSize, new UtowFileUpload(upload).getFileSize());
25+
assertEquals(fileSize, new UtowFileUpload("file", upload).getFileSize());
2626
}
2727

2828
@Test
@@ -35,6 +35,6 @@ public void shouldGetUnknownFileSize() throws IOException {
3535
FormData.FormValue upload = mock(FormData.FormValue.class);
3636
when(upload.getFileItem()).thenReturn(fileItem);
3737

38-
assertEquals(fileSize, new UtowFileUpload(upload).getFileSize());
38+
assertEquals(fileSize, new UtowFileUpload("file", upload).getFileSize());
3939
}
4040
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package io.jooby;
2+
3+
import io.jooby.junit.ServerTest;
4+
import io.jooby.junit.ServerTestRunner;
5+
import okhttp3.MultipartBody;
6+
7+
import java.util.stream.Collectors;
8+
9+
import static okhttp3.RequestBody.create;
10+
import static org.junit.jupiter.api.Assertions.assertEquals;
11+
12+
public class Issue1676 {
13+
@ServerTest
14+
public void shouldParseFileKeyFromMultipartUpload(ServerTestRunner runner) {
15+
String content = "Proin pulvinar purus ac lacus pharetra, tristique fringilla lacus finibus. "
16+
+ "Praesent bibendum tellus feugiat euismod sollicitudin. Integer sed sem vestibulum sem "
17+
+ "imperdiet blandit. Vivamus in lorem quis orci gravida hendrerit.";
18+
runner.define(app -> {
19+
app.post("/1676", ctx ->
20+
ctx.files().stream()
21+
.collect(Collectors.toMap(FileUpload::getName, FileUpload::getFileName))
22+
);
23+
}).ready(client -> {
24+
client.post("/1676", new MultipartBody.Builder()
25+
.setType(MultipartBody.FORM)
26+
.addFormDataPart("filekey", "19kb.txt",
27+
create(content, okhttp3.MediaType.parse("text/plain")))
28+
.build(), rsp -> {
29+
assertEquals("{filekey=19kb.txt}", rsp.body().string());
30+
});
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)