Skip to content

Commit 520ffb9

Browse files
committed
Add the ability to download raw file, needed when size > 1MB
1 parent d90368c commit 520ffb9

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

lib/Github/Api/Repository/Contents.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,11 @@ public function readme($username, $repository, $reference = null)
6565
* @param string $repository the name of the repository
6666
* @param string|null $path path to file or directory
6767
* @param string|null $reference reference to a branch or commit
68+
* @param array $requestHeaders request headers
6869
*
6970
* @return array|string information for file | information for each item in directory
7071
*/
71-
public function show($username, $repository, $path = null, $reference = null)
72+
public function show($username, $repository, $path = null, $reference = null, $requestHeaders = [])
7273
{
7374
$url = '/repos/'.rawurlencode($username).'/'.rawurlencode($repository).'/contents';
7475
if (null !== $path) {
@@ -77,7 +78,7 @@ public function show($username, $repository, $path = null, $reference = null)
7778

7879
return $this->get($url, [
7980
'ref' => $reference,
80-
]);
81+
], $requestHeaders);
8182
}
8283

8384
/**
@@ -272,9 +273,11 @@ public function archive($username, $repository, $format, $reference = null)
272273
*
273274
* @return string|null content of file, or null in case of base64_decode failure
274275
*/
275-
public function download($username, $repository, $path, $reference = null)
276+
public function download($username, $repository, $path, $reference = null, $bodyType = null)
276277
{
277-
$file = $this->show($username, $repository, $path, $reference);
278+
$file = $this->show($username, $repository, $path, $reference,[
279+
'Accept' => 'application/vnd.github.VERSION.raw'
280+
]);
278281

279282
if (!isset($file['type']) || !in_array($file['type'], ['file', 'symlink'], true)) {
280283
throw new InvalidArgumentException(sprintf('Path "%s" is not a file or a symlink to a file.', $path));
@@ -294,4 +297,24 @@ public function download($username, $repository, $path, $reference = null)
294297

295298
return base64_decode($file['content']) ?: null;
296299
}
300+
301+
/**
302+
* Get the raw content of a file in a repository
303+
*
304+
* Use this method instead of the download method if your file is bigger than 1MB
305+
*
306+
* @see https://docs.github.com/en/rest/repos/contents
307+
*
308+
* @param string $username the user who owns the repository
309+
* @param string $repository the name of the repository
310+
* @param string $path path to file
311+
* @param string|null $reference reference to a branch or commit
312+
*
313+
* @return array|string
314+
*/
315+
public function rawDownload($username, $repository, $path, $reference = null) {
316+
return $this->show($username, $repository, $path, $reference, [
317+
'Accept' => 'application/vnd.github.VERSION.raw'
318+
]);
319+
}
297320
}

test/Github/Tests/Api/Repository/ContentsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,24 @@ public function shouldDownloadForSpacedPath()
319319
$this->assertEquals($expectedValue, $api->download('mads379', 'scala.tmbundle', 'Syntaxes/Simple Build Tool.tmLanguage'));
320320
}
321321

322+
/**
323+
* @test
324+
*/
325+
public function shouldRawDownloadForGivenPath()
326+
{
327+
// The show() method return
328+
$getValue = include __DIR__.'/fixtures/ContentsDownloadFixture.php';
329+
330+
$api = $this->getApiMock();
331+
$api->expects($this->once())
332+
->method('get')
333+
->with('/repos/KnpLabs/php-github-api/contents/test%2FGithub%2FTests%2FApi%2FRepository%2FContentsTest.php', ['ref' => null])
334+
->will($this->returnValue($getValue));
335+
336+
$this->assertEquals($getValue, $api->rawDownload('KnpLabs', 'php-github-api', 'test/Github/Tests/Api/Repository/ContentsTest.php'));
337+
}
338+
339+
322340
/**
323341
* @return string
324342
*/

0 commit comments

Comments
 (0)