-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: If a file is uploaded to a server using an annotated service, the actual filename specified in the Content-Disposition header is lost because a random file is created to avoid duplicate file names. Modifications: - Add `MultipartFile` to get the actual filename of a `BodyPart`. - Add documentation for the usage of `Multipart` - How to encode and decode `Multipart` from and to `HttpRequest` - How to bind `multipart/form-data` in annotated services Result: You can now use `MultipartFile` to get the actual filename of an uploaded file through `multipart/form-data`. ```java @consumes(MediaTypeNames.MULTIPART_FORM_DATA) @post("/upload") public HttpResponse upload(@param MultipartFile multipartFile) { // The name parameter of the "content-disposition" header String name = multipartFile.name(); // The filename parameter of the "content-disposition" header String filename = multipartFile.filename(); // The file that stores the part content. File file = multipartFile.file(); ... } ```
- Loading branch information
Showing
8 changed files
with
446 additions
and
85 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
core/src/main/java/com/linecorp/armeria/common/multipart/DefaultMultipartFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.common.multipart; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
import com.google.common.base.MoreObjects; | ||
|
||
final class DefaultMultipartFile implements MultipartFile { | ||
|
||
private final String name; | ||
private final String filename; | ||
private final Path path; | ||
|
||
DefaultMultipartFile(String name, String filename, Path path) { | ||
this.name = name; | ||
this.filename = filename; | ||
this.path = path; | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String filename() { | ||
return filename; | ||
} | ||
|
||
@Override | ||
public Path path() { | ||
return path; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
|
||
if (!(o instanceof MultipartFile)) { | ||
return false; | ||
} | ||
|
||
final MultipartFile that = (MultipartFile) o; | ||
return name.equals(that.name()) && | ||
filename.equals(that.filename()) && | ||
path.equals(that.path()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(name, filename, path); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("name", name) | ||
.add("filename", filename) | ||
.add("path", path) | ||
.toString(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
core/src/main/java/com/linecorp/armeria/common/multipart/MultipartFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2022 LINE Corporation | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.linecorp.armeria.common.multipart; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* A file uploaded from a {@link Multipart} request. | ||
* | ||
* @see <a href="https://datatracker.ietf.org/doc/html/rfc7578#section-4.2"> | ||
* Content-Disposition Header Field for Each Part</a> | ||
*/ | ||
public interface MultipartFile { | ||
|
||
/** | ||
* Creates a new {@link MultipartFile}. | ||
* @param name the name parameter of the {@code "content-disposition"} | ||
* @param filename the filename parameter of the {@code "content-disposition"} | ||
* header. | ||
* @param file the file that stores the {@link BodyPart#content()}. | ||
*/ | ||
static MultipartFile of(String name, String filename, File file) { | ||
requireNonNull(name, "name"); | ||
requireNonNull(filename, "filename"); | ||
requireNonNull(file, "file"); | ||
return of(name, filename, file.toPath()); | ||
} | ||
|
||
/** | ||
* Creates a new {@link MultipartFile}. | ||
* @param name the name parameter of the {@code "content-disposition"} | ||
* @param filename the filename parameter of the {@code "content-disposition"} | ||
* header. | ||
* @param path the path that stores the {@link BodyPart#content()}. | ||
*/ | ||
static MultipartFile of(String name, String filename, Path path) { | ||
requireNonNull(name, "name"); | ||
requireNonNull(filename, "filename"); | ||
requireNonNull(path, "path"); | ||
return new DefaultMultipartFile(name, filename, path); | ||
} | ||
|
||
/** | ||
* Returns the {@code name} parameter of the {@code "content-disposition"} header. | ||
* @see BodyPart#name() | ||
*/ | ||
String name(); | ||
|
||
/** | ||
* Returns the {@code filename} parameter of the {@code "content-disposition"} header. | ||
* @see BodyPart#filename() | ||
*/ | ||
String filename(); | ||
|
||
/** | ||
* Returns the file that stores the {@link BodyPart#content()}. | ||
*/ | ||
default File file() { | ||
return path().toFile(); | ||
} | ||
|
||
/** | ||
* Returns the path that stores the {@link BodyPart#content()}. | ||
*/ | ||
Path path(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.