Skip to content

Commit

Permalink
Merge pull request #14 from kintone/fileupload-multipart
Browse files Browse the repository at this point in the history
fix: Support multibyte characters for file upload
  • Loading branch information
0gr authored Aug 5, 2020
2 parents 63f78a8 + 637d0de commit 40cc120
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ API client library for Kintone REST APIs on Java.
Add dependency declaration in `build.gradle` of your project.
```
dependencies {
implementation 'com.kintone:kintone-java-client:1.0.1'
implementation 'com.kintone:kintone-java-client:1.0.2'
}
```
- For projects using Maven
Expand All @@ -17,7 +17,7 @@ API client library for Kintone REST APIs on Java.
<dependency>
<groupId>com.kintone</groupId>
<artifactId>kintone-java-client</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id 'com.diffplug.gradle.spotless' version '3.25.0'
}

version = '1.0.1'
version = '1.0.2'
sourceCompatibility = 1.8
targetCompatibility = 1.8

Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ client.close();
Add dependency declaration in `build.gradle` of your project.
```groovy
dependencies {
implementation 'com.kintone:kintone-java-client:1.0.1'
implementation 'com.kintone:kintone-java-client:1.0.2'
}
```

Expand All @@ -39,7 +39,7 @@ Add dependency declaration in `pom.xml` of your project.
<dependency>
<groupId>com.kintone</groupId>
<artifactId>kintone-java-client</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
```

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/kintone/client/InternalClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.CloseableHttpClient;
Expand Down Expand Up @@ -213,7 +214,9 @@ KintoneResponse<UploadFileResponseBody> upload(
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setCharset(StandardCharsets.UTF_8);
builder.setBoundary(boundary);
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setContentType(ContentType.MULTIPART_FORM_DATA);

builder.addPart(
"file", new InputStreamBody(content, ContentType.create(contentType), filename));

Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/kintone/client/InternalClientImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.base.Charsets;
import com.kintone.client.api.common.BulkRequestsRequest;
import com.kintone.client.api.common.BulkRequestsResponseBody;
import com.kintone.client.api.common.DownloadFileRequest;
Expand Down Expand Up @@ -360,6 +361,42 @@ public void upload() throws IOException {
assertThat(getMultipartContent(body)).isEqualTo("test");
}

@Test
public void upload_multibyte_character() throws IOException {
String boundary = "__END_OF_PART__";
String response = loadResource("InternalClientImplTest_upload_response.json");
HttpRequest httpRequest =
HttpRequest.request("/k/v1/file.json")
.withMethod("POST")
.withHeader("Content-Type", "multipart/form-data; boundary=" + boundary)
.withHeader("X-Cybozu-Authorization", passwordAuth);
server
.when(httpRequest)
.respond(
HttpResponse.response()
.withStatusCode(200)
.withHeader("Content-Type", "application/json")
.withBody(response));

InternalClientImpl sut = setupClient();
UploadFileResponseBody resp;
try (InputStream in = new ByteArrayInputStream("test".getBytes())) {
resp = sut.upload("日本語ファイル名.jpg", "image/jpeg", in, Collections.emptyList()).getBody();
}
server.verify(httpRequest, VerificationTimes.once());
assertThat(resp.getFileKey()).isEqualTo("testkey");

String body = server.retrieveRecordedRequests(httpRequest)[0].getBodyAsString();
assertThat(body).startsWith("--" + boundary + "\r\n");
assertThat(body).endsWith("\r\n--" + boundary + "--\r\n");
Map<String, String> headers = getMultipartHeaders(body);
assertThat(headers).containsEntry("Content-Type", "image/jpeg");
String expect = "form-data; name=\"file\"; filename=\"日本語ファイル名.jpg\"";
expect = new String(expect.getBytes(Charsets.UTF_8), Charsets.ISO_8859_1);
assertThat(headers).containsEntry("Content-Disposition", expect);
assertThat(getMultipartContent(body)).isEqualTo("test");
}

@Test
public void call_authByApiToken() {
String apiToken = "abcdefghijklmnopqrstuvwxyz1234567890ABCD";
Expand Down

0 comments on commit 40cc120

Please sign in to comment.