Skip to content

Commit 66fb676

Browse files
drummm1guelpf
authored andcommitted
Support user avatar file upload
1 parent 5b465f8 commit 66fb676

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/Gitlab/Api/AbstractApi.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,32 @@ protected function post($path, array $parameters = array(), $requestHeaders = ar
123123
* @param array $requestHeaders
124124
* @return mixed
125125
*/
126-
protected function put($path, array $parameters = array(), $requestHeaders = array())
126+
protected function put($path, array $parameters = array(), $requestHeaders = array(), array $files = array())
127127
{
128128
$path = $this->preparePath($path);
129129

130130
$body = null;
131-
if (!empty($parameters)) {
131+
if (empty($files) && !empty($parameters)) {
132132
$body = $this->prepareBody($parameters);
133133
$requestHeaders['Content-Type'] = 'application/x-www-form-urlencoded';
134+
} elseif (!empty($files)) {
135+
$builder = new MultipartStreamBuilder($this->streamFactory);
136+
137+
foreach ($parameters as $name => $value) {
138+
$builder->addResource($name, $value);
139+
}
140+
141+
foreach ($files as $name => $file) {
142+
$builder->addResource($name, fopen($file, 'r'), [
143+
'headers' => [
144+
'Content-Type' => $this->guessContentType($file),
145+
],
146+
'filename' => basename($file),
147+
]);
148+
}
149+
150+
$body = $builder->build();
151+
$requestHeaders['Content-Type'] = 'multipart/form-data; boundary='.$builder->getBoundary();
134152
}
135153

136154
$response = $this->client->getHttpClient()->put($path, $requestHeaders, $body);

lib/Gitlab/Api/Users.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ public function create($email, $password, array $params = array())
9898
/**
9999
* @param int $id
100100
* @param array $params
101+
* @param array $files
101102
* @return mixed
102103
*/
103-
public function update($id, array $params)
104+
public function update($id, array $params, array $files = array())
104105
{
105-
return $this->put('users/'.$this->encodePath($id), $params);
106+
return $this->put('users/'.$this->encodePath($id), $params, array(), $files);
106107
}
107108

108109
/**

0 commit comments

Comments
 (0)