Skip to content

Commit 156d828

Browse files
committed
Simplified uploading avatar for project (#284).
1 parent 6eb8bd7 commit 156d828

File tree

4 files changed

+14
-49
lines changed

4 files changed

+14
-49
lines changed

src/main/java/org/gitlab4j/api/AbstractApi.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,13 @@ protected Response putWithFormData(Response.Status expectedStatus, Form formData
442442
* @param expectedStatus the HTTP status that should be returned from the server
443443
* @param name the name for the form field that contains the file name
444444
* @param fileToUpload a File instance pointing to the file to upload
445-
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
446445
* @param pathArgs variable list of arguments used to build the URI
447446
* @return a ClientResponse instance with the data returned from the endpoint
448447
* @throws GitLabApiException if any exception occurs during execution
449448
*/
450-
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, Object... pathArgs) throws GitLabApiException {
449+
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, Object... pathArgs) throws GitLabApiException {
451450
try {
452-
return validate(getApiClient().putUpload(name, fileToUpload, mediaType, pathArgs), expectedStatus);
451+
return validate(getApiClient().putUpload(name, fileToUpload, pathArgs), expectedStatus);
453452
} catch (Exception e) {
454453
throw handle(e);
455454
}
@@ -462,14 +461,13 @@ protected Response putUpload(Response.Status expectedStatus, String name, File f
462461
* @param expectedStatus the HTTP status that should be returned from the server
463462
* @param name the name for the form field that contains the file name
464463
* @param fileToUpload a File instance pointing to the file to upload
465-
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
466464
* @param url the fully formed path to the GitLab API endpoint
467465
* @return a ClientResponse instance with the data returned from the endpoint
468466
* @throws GitLabApiException if any exception occurs during execution
469467
*/
470-
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, String mediaType, URL url) throws GitLabApiException {
468+
protected Response putUpload(Response.Status expectedStatus, String name, File fileToUpload, URL url) throws GitLabApiException {
471469
try {
472-
return validate(getApiClient().putUpload(name, fileToUpload, mediaType, url), expectedStatus);
470+
return validate(getApiClient().putUpload(name, fileToUpload, url), expectedStatus);
473471
} catch (Exception e) {
474472
throw handle(e);
475473
}

src/main/java/org/gitlab4j/api/GitLabApiClient.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -567,14 +567,13 @@ protected Response upload(String name, File fileToUpload, String mediaTypeString
567567
*
568568
* @param name the name for the form field that contains the file name
569569
* @param fileToUpload a File instance pointing to the file to upload
570-
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
571570
* @param pathArgs variable list of arguments used to build the URI
572571
* @return a ClientResponse instance with the data returned from the endpoint
573572
* @throws IOException if an error occurs while constructing the URL
574573
*/
575-
protected Response putUpload(String name, File fileToUpload, String mediaTypeString, Object... pathArgs) throws IOException {
574+
protected Response putUpload(String name, File fileToUpload, Object... pathArgs) throws IOException {
576575
URL url = getApiUrl(pathArgs);
577-
return (putUpload(name, fileToUpload, mediaTypeString, url));
576+
return (putUpload(name, fileToUpload, url));
578577
}
579578

580579
/**
@@ -583,21 +582,15 @@ protected Response putUpload(String name, File fileToUpload, String mediaTypeStr
583582
*
584583
* @param name the name for the form field that contains the file name
585584
* @param fileToUpload a File instance pointing to the file to upload
586-
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
585+
587586
* @param url the fully formed path to the GitLab API endpoint
588587
* @return a ClientResponse instance with the data returned from the endpoint
589588
* @throws IOException if an error occurs while constructing the URL
590589
*/
591-
protected Response putUpload(String name, File fileToUpload, String mediaTypeString, URL url) throws IOException {
592-
593-
MediaType mediaType = (mediaTypeString != null ? MediaType.valueOf(mediaTypeString) : null);
594-
try (MultiPart multiPart = new FormDataMultiPart()) {
595-
FileDataBodyPart filePart = mediaType != null ?
596-
new FileDataBodyPart(name, fileToUpload, mediaType) :
597-
new FileDataBodyPart(name, fileToUpload);
598-
multiPart.bodyPart(filePart);
599-
return (invocation(url, null).put(Entity.entity(multiPart, MULTIPART_FORM_DATA_TYPE)));
600-
}
590+
protected Response putUpload(String name, File fileToUpload, URL url) throws IOException {
591+
final FormDataMultiPart multiPart = new FormDataMultiPart();
592+
multiPart.bodyPart(new FileDataBodyPart(name, fileToUpload, MediaType.APPLICATION_OCTET_STREAM_TYPE));
593+
return (invocation(url, null).put(Entity.entity(multiPart, MULTIPART_FORM_DATA_TYPE)));
601594
}
602595

603596
/**

src/main/java/org/gitlab4j/api/ProjectApi.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,20 +2388,6 @@ public Project transferProject(Object projectIdOrPath, String namespace) throws
23882388
return (response.readEntity(Project.class));
23892389
}
23902390

2391-
/**
2392-
* Uploads and sets the project avatar for the specified project
2393-
*
2394-
* <pre><code>PUT /projects/:id/uploads</code></pre>
2395-
*
2396-
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
2397-
* @param avatarFile the File instance of the avatar file to upload
2398-
* @return the updated Project instance
2399-
* @throws GitLabApiException if any exception occurs
2400-
*/
2401-
public Project setProjectAvatar(Object projectIdOrPath, File avatarFile) throws GitLabApiException {
2402-
return (setProjectAvatar(projectIdOrPath, avatarFile, null));
2403-
}
2404-
24052391
/**
24062392
* Uploads and sets the project avatar for the specified project.
24072393
*
@@ -2413,8 +2399,8 @@ public Project setProjectAvatar(Object projectIdOrPath, File avatarFile) throws
24132399
* @return the updated Project instance
24142400
* @throws GitLabApiException if any exception occurs
24152401
*/
2416-
public Project setProjectAvatar(Object projectIdOrPath, File avatarFile, String mediaType) throws GitLabApiException {
2417-
Response response = putUpload(Response.Status.OK, "avatar", avatarFile, mediaType, "projects", getProjectIdOrPath(projectIdOrPath));
2402+
public Project setProjectAvatar(Object projectIdOrPath, File avatarFile) throws GitLabApiException {
2403+
Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "projects", getProjectIdOrPath(projectIdOrPath));
24182404
return (response.readEntity(Project.class));
24192405
}
24202406
}

src/test/java/org/gitlab4j/api/TestAvatarUpload.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,7 @@ public void testSetProjectAvatar() throws GitLabApiException {
8484
assertNotNull(project);
8585

8686
File avatarFile = new File("src/test/resources/org/gitlab4j/api/avatar.png");
87-
Project updatedProject = gitLabApi.getProjectApi().setProjectAvatar(project.getId(), avatarFile, null);
88-
assertNotNull(updatedProject);
89-
assertTrue(updatedProject.getAvatarUrl().endsWith("avatar.png"));
90-
}
91-
92-
@Test
93-
public void testSetProjectAvatarWithMediaType() throws GitLabApiException {
94-
95-
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
96-
assertNotNull(project);
97-
98-
File avatarFile = new File("src/test/resources/org/gitlab4j/api/avatar.png");
99-
Project updatedProject = gitLabApi.getProjectApi().setProjectAvatar(project.getId(), avatarFile, "image/png");
87+
Project updatedProject = gitLabApi.getProjectApi().setProjectAvatar(project.getId(), avatarFile);
10088
assertNotNull(updatedProject);
10189
assertTrue(updatedProject.getAvatarUrl().endsWith("avatar.png"));
10290
}

0 commit comments

Comments
 (0)