Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
156d828f
Commit
156d828f
authored
Dec 21, 2018
by
Greg Messner
Browse files
Simplified uploading avatar for project (#284).
parent
6eb8bd73
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/AbstractApi.java
View file @
156d828f
...
...
@@ -442,14 +442,13 @@ public abstract class AbstractApi implements Constants {
* @param expectedStatus the HTTP status that should be returned from the server
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution
*/
protected
Response
putUpload
(
Response
.
Status
expectedStatus
,
String
name
,
File
fileToUpload
,
String
mediaType
,
Object
...
pathArgs
)
throws
GitLabApiException
{
protected
Response
putUpload
(
Response
.
Status
expectedStatus
,
String
name
,
File
fileToUpload
,
Object
...
pathArgs
)
throws
GitLabApiException
{
try
{
return
validate
(
getApiClient
().
putUpload
(
name
,
fileToUpload
,
mediaType
,
pathArgs
),
expectedStatus
);
return
validate
(
getApiClient
().
putUpload
(
name
,
fileToUpload
,
pathArgs
),
expectedStatus
);
}
catch
(
Exception
e
)
{
throw
handle
(
e
);
}
...
...
@@ -462,14 +461,13 @@ public abstract class AbstractApi implements Constants {
* @param expectedStatus the HTTP status that should be returned from the server
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaType the content-type of the uploaded file, if null will be determined from fileToUpload
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws GitLabApiException if any exception occurs during execution
*/
protected
Response
putUpload
(
Response
.
Status
expectedStatus
,
String
name
,
File
fileToUpload
,
String
mediaType
,
URL
url
)
throws
GitLabApiException
{
protected
Response
putUpload
(
Response
.
Status
expectedStatus
,
String
name
,
File
fileToUpload
,
URL
url
)
throws
GitLabApiException
{
try
{
return
validate
(
getApiClient
().
putUpload
(
name
,
fileToUpload
,
mediaType
,
url
),
expectedStatus
);
return
validate
(
getApiClient
().
putUpload
(
name
,
fileToUpload
,
url
),
expectedStatus
);
}
catch
(
Exception
e
)
{
throw
handle
(
e
);
}
...
...
src/main/java/org/gitlab4j/api/GitLabApiClient.java
View file @
156d828f
...
...
@@ -567,14 +567,13 @@ public class GitLabApiClient {
*
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
* @param pathArgs variable list of arguments used to build the URI
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected
Response
putUpload
(
String
name
,
File
fileToUpload
,
String
mediaTypeString
,
Object
...
pathArgs
)
throws
IOException
{
protected
Response
putUpload
(
String
name
,
File
fileToUpload
,
Object
...
pathArgs
)
throws
IOException
{
URL
url
=
getApiUrl
(
pathArgs
);
return
(
putUpload
(
name
,
fileToUpload
,
mediaTypeString
,
url
));
return
(
putUpload
(
name
,
fileToUpload
,
url
));
}
/**
...
...
@@ -583,21 +582,15 @@ public class GitLabApiClient {
*
* @param name the name for the form field that contains the file name
* @param fileToUpload a File instance pointing to the file to upload
* @param mediaTypeString the content-type of the uploaded file, if null will be determined from fileToUpload
* @param url the fully formed path to the GitLab API endpoint
* @return a ClientResponse instance with the data returned from the endpoint
* @throws IOException if an error occurs while constructing the URL
*/
protected
Response
putUpload
(
String
name
,
File
fileToUpload
,
String
mediaTypeString
,
URL
url
)
throws
IOException
{
MediaType
mediaType
=
(
mediaTypeString
!=
null
?
MediaType
.
valueOf
(
mediaTypeString
)
:
null
);
try
(
MultiPart
multiPart
=
new
FormDataMultiPart
())
{
FileDataBodyPart
filePart
=
mediaType
!=
null
?
new
FileDataBodyPart
(
name
,
fileToUpload
,
mediaType
)
:
new
FileDataBodyPart
(
name
,
fileToUpload
);
multiPart
.
bodyPart
(
filePart
);
return
(
invocation
(
url
,
null
).
put
(
Entity
.
entity
(
multiPart
,
MULTIPART_FORM_DATA_TYPE
)));
}
protected
Response
putUpload
(
String
name
,
File
fileToUpload
,
URL
url
)
throws
IOException
{
final
FormDataMultiPart
multiPart
=
new
FormDataMultiPart
();
multiPart
.
bodyPart
(
new
FileDataBodyPart
(
name
,
fileToUpload
,
MediaType
.
APPLICATION_OCTET_STREAM_TYPE
));
return
(
invocation
(
url
,
null
).
put
(
Entity
.
entity
(
multiPart
,
MULTIPART_FORM_DATA_TYPE
)));
}
/**
...
...
src/main/java/org/gitlab4j/api/ProjectApi.java
View file @
156d828f
...
...
@@ -2388,20 +2388,6 @@ public class ProjectApi extends AbstractApi implements Constants {
return
(
response
.
readEntity
(
Project
.
class
));
}
/**
* Uploads and sets the project avatar for the specified project
*
* <pre><code>PUT /projects/:id/uploads</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param avatarFile the File instance of the avatar file to upload
* @return the updated Project instance
* @throws GitLabApiException if any exception occurs
*/
public
Project
setProjectAvatar
(
Object
projectIdOrPath
,
File
avatarFile
)
throws
GitLabApiException
{
return
(
setProjectAvatar
(
projectIdOrPath
,
avatarFile
,
null
));
}
/**
* Uploads and sets the project avatar for the specified project.
*
...
...
@@ -2413,8 +2399,8 @@ public class ProjectApi extends AbstractApi implements Constants {
* @return the updated Project instance
* @throws GitLabApiException if any exception occurs
*/
public
Project
setProjectAvatar
(
Object
projectIdOrPath
,
File
avatarFile
,
String
mediaType
)
throws
GitLabApiException
{
Response
response
=
putUpload
(
Response
.
Status
.
OK
,
"avatar"
,
avatarFile
,
mediaType
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
));
public
Project
setProjectAvatar
(
Object
projectIdOrPath
,
File
avatarFile
)
throws
GitLabApiException
{
Response
response
=
putUpload
(
Response
.
Status
.
OK
,
"avatar"
,
avatarFile
,
"projects"
,
getProjectIdOrPath
(
projectIdOrPath
));
return
(
response
.
readEntity
(
Project
.
class
));
}
}
\ No newline at end of file
src/test/java/org/gitlab4j/api/TestAvatarUpload.java
View file @
156d828f
...
...
@@ -84,19 +84,7 @@ public class TestAvatarUpload {
assertNotNull
(
project
);
File
avatarFile
=
new
File
(
"src/test/resources/org/gitlab4j/api/avatar.png"
);
Project
updatedProject
=
gitLabApi
.
getProjectApi
().
setProjectAvatar
(
project
.
getId
(),
avatarFile
,
null
);
assertNotNull
(
updatedProject
);
assertTrue
(
updatedProject
.
getAvatarUrl
().
endsWith
(
"avatar.png"
));
}
@Test
public
void
testSetProjectAvatarWithMediaType
()
throws
GitLabApiException
{
Project
project
=
gitLabApi
.
getProjectApi
().
getProject
(
TEST_NAMESPACE
,
TEST_PROJECT_NAME
);
assertNotNull
(
project
);
File
avatarFile
=
new
File
(
"src/test/resources/org/gitlab4j/api/avatar.png"
);
Project
updatedProject
=
gitLabApi
.
getProjectApi
().
setProjectAvatar
(
project
.
getId
(),
avatarFile
,
"image/png"
);
Project
updatedProject
=
gitLabApi
.
getProjectApi
().
setProjectAvatar
(
project
.
getId
(),
avatarFile
);
assertNotNull
(
updatedProject
);
assertTrue
(
updatedProject
.
getAvatarUrl
().
endsWith
(
"avatar.png"
));
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment