Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ fileserver/
gradlew.bat
gradle/
gradle/
/target/
.classpath
.factorypath
*.prefs
.project
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public record Part(String contentType, String filename, String data, File file)

}

private record PartMetadata(String name, String filename) {
private record PartMetadata(String contentType, String name, String filename) {

}

Expand Down Expand Up @@ -120,12 +120,12 @@ public static Map<String, List<Part>> parse(String encoding, String content_type
if (meta.filename == null) {
var bos = new ByteArrayOutputStream();
os = bos;
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(null, null, bos.toString(charset), null));
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(meta.contentType, null, bos.toString(charset), null));
} else {
File file = Path.of(storage.toString(), meta.filename).toFile();
file.deleteOnExit();
os = new NoSyncBufferedOutputStream(new FileOutputStream(file));
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(null, meta.filename, null, file));
addToResults = () -> results.computeIfAbsent(meta.name, k -> new LinkedList<Part>()).add(new Part(meta.contentType, meta.filename, null, file));
}

try (os) {
Expand Down Expand Up @@ -170,6 +170,7 @@ public static Map<String, List<Part>> parse(String encoding, String content_type
private static PartMetadata parseHeaders(List<String> headers) {
String name = null;
String filename = null;
String contentType = null;
for (var header : headers) {
String[] parts = header.split(":", 2);
if ("content-disposition".equalsIgnoreCase(parts[0])) {
Expand All @@ -188,9 +189,11 @@ private static PartMetadata parseHeaders(List<String> headers) {
}
}

} else if ("content-type".equalsIgnoreCase(parts[0])) {
contentType = parts[1].trim();
}
}
return new PartMetadata(name, filename);
return new PartMetadata(contentType, name, filename);
}

private static String readLine(Charset charset, InputStream is) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ public void testFiles() throws UnsupportedEncodingException, IOException {
s += "111Y\r\n";
s += "111Z\rCCCC\nCCCC\r\nCCCCC@\r\n";

Assert.assertEquals(values.get(0).contentType(),"text/plain");
Assert.assertEquals(s.getBytes("UTF-8"), Files.readAllBytes((values.get(0).file()).toPath()), "file1 failed");


s = "\r\n";
s += "@22X";
s += "222Y\r\n";
s += "222Z\r222W\n2220\r\n666@";

Assert.assertEquals(values.get(1).contentType(),"text/plain");
Assert.assertEquals(s.getBytes("UTF-8"), Files.readAllBytes((values.get(1).file()).toPath()), "file2 failed");
}

Expand Down Expand Up @@ -118,6 +121,7 @@ public void testFormSample() throws IOException {

Assert.assertEquals(results.size(), 1);
List<Part> values = results.get("myfile");
Assert.assertEquals(values.get(0).contentType(), "text/plain");
Assert.assertEquals(values.size(), 1);
}

Expand All @@ -133,6 +137,12 @@ public void testMultiFileFormSample() throws IOException {

Assert.assertEquals(results.size(), 2);
List<Part> values = results.get("myfile");
Assert.assertEquals(values.get(0).contentType(), "text/plain");
Assert.assertEquals(values.size(), 1);

values = results.get("myfile2");
Assert.assertEquals(values.get(0).contentType(), "image/png");
Assert.assertEquals(values.size(), 1);

}
}