Skip to content

Commit 92bed61

Browse files
[11.10] Add support for Packages::addGenericFile() (GitLabPHP#721)
1 parent 00d5d0f commit 92bed61

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Api/AbstractApi.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,28 @@ protected function put(string $uri, array $params = [], array $headers = [], arr
153153
return ResponseMediator::getContent($response);
154154
}
155155

156+
/**
157+
* @param string $uri
158+
* @param string $file
159+
* @param array<string,string> $headers
160+
* @param array<string,mixed> $uriParams
161+
*
162+
* @return mixed
163+
*/
164+
protected function putFile(string $uri, string $file, array $headers = [], array $uriParams = [])
165+
{
166+
$resource = self::tryFopen($file, 'r');
167+
$body = $this->client->getStreamFactory()->createStreamFromResource($resource);
168+
169+
if ($body->isReadable()) {
170+
$headers = \array_merge([ResponseMediator::CONTENT_TYPE_HEADER => self::guessFileContentType($file)], $headers);
171+
}
172+
173+
$response = $this->client->getHttpClient()->put(self::prepareUri($uri, $uriParams), $headers, $body);
174+
175+
return ResponseMediator::getContent($response);
176+
}
177+
156178
/**
157179
* @param string $uri
158180
* @param array<string,mixed> $params

src/Api/Packages.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,28 @@ public function removeFile($project_id, int $package_id, int $package_file_id)
110110
);
111111
}
112112

113+
/**
114+
* @param int|string $project_id
115+
* @param string $package_name
116+
* @param string $package_version
117+
* @param string $file
118+
* @param string $status
119+
*
120+
* @return mixed
121+
*/
122+
public function addGenericFile($project_id, string $package_name, string $package_version, string $file, string $status = 'default')
123+
{
124+
return $this->putFile(
125+
$this->getProjectPath(
126+
$project_id,
127+
'packages/generic/'.self::encodePath($package_name).'/'.self::encodePath($package_version).'/'.self::encodePath(\basename($file))
128+
),
129+
$file,
130+
[],
131+
['status' => $status]
132+
);
133+
}
134+
113135
/**
114136
* @param int|string $project_id
115137
* @param int $package_id

0 commit comments

Comments
 (0)