-
Notifications
You must be signed in to change notification settings - Fork 253
上传文件接口支持自定义meta字段 #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
上传文件接口支持自定义meta字段 #103
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,49 +31,59 @@ public static class Builder { | |
|
|
||
| private final URI uri; | ||
| private String fileName; | ||
| private String fileSha256; | ||
| private InputStream fileInputStream; | ||
| private ContentType fileContentType; | ||
| private String meta; | ||
|
|
||
| public Builder(URI uri) { | ||
| if (uri == null) { | ||
| throw new IllegalArgumentException("上传文件接口URL为空"); | ||
| } | ||
| this.uri = uri; | ||
| } | ||
|
|
||
| public Builder withImage(String fileName, String fileSha256, InputStream inputStream) { | ||
| if (fileSha256 == null || fileSha256.isEmpty()) { | ||
| throw new IllegalArgumentException("文件摘要为空"); | ||
| } | ||
| meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256); | ||
| return withFile(fileName, meta, inputStream); | ||
| } | ||
|
|
||
| public Builder withFile(String fileName, String meta, InputStream inputStream) { | ||
EmmetZC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.fileName = fileName; | ||
| this.fileSha256 = fileSha256; | ||
| this.fileInputStream = inputStream; | ||
|
|
||
| String mimeType = URLConnection.guessContentTypeFromName(fileName); | ||
| if (mimeType == null) { | ||
| // guess this is a video uploading | ||
| this.fileContentType = ContentType.APPLICATION_OCTET_STREAM; | ||
| } else { | ||
| this.fileContentType = ContentType.create(mimeType); | ||
| } | ||
| this.meta = meta; | ||
| return this; | ||
| } | ||
|
|
||
| public WechatPayUploadHttpPost build() { | ||
| if (fileName == null || fileSha256 == null || fileInputStream == null) { | ||
Eric-Lee-Handyman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new IllegalArgumentException("缺少待上传图片文件信息"); | ||
| if (fileName == null || fileName.isEmpty()) { | ||
| throw new IllegalArgumentException("文件名称为空"); | ||
| } | ||
|
|
||
| if (uri == null) { | ||
| throw new IllegalArgumentException("缺少上传图片接口URL"); | ||
| if (fileInputStream == null) { | ||
| throw new IllegalArgumentException("文件为空"); | ||
| } | ||
| if (fileContentType == null) { | ||
| throw new IllegalArgumentException("文件类型为空"); | ||
| } | ||
| if (meta == null || meta.isEmpty()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这些检查还是需要吧,除了fileSha256之外。开发者要是忘记调用
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 现在虽然我们的设计meta就意味着有其他参数,但是未来可能放开了呢?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有道理。之前没有考虑未来放开的场景,已修改。 |
||
| throw new IllegalArgumentException("媒体文件元信息为空"); | ||
| } | ||
|
|
||
| String meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256); | ||
| WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta); | ||
|
|
||
| MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create(); | ||
| entityBuilder.setMode(HttpMultipartMode.RFC6532) | ||
| .addBinaryBody("file", fileInputStream, fileContentType, fileName) | ||
| .addTextBody("meta", meta, APPLICATION_JSON); | ||
|
|
||
| request.setEntity(entityBuilder.build()); | ||
| request.addHeader(ACCEPT, APPLICATION_JSON.toString()); | ||
|
|
||
| return request; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么不实现成
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
从逻辑的角度,withImage调用wtihFile来实现更合理。已修改