Skip to content

Commit 0158c30

Browse files
authored
feature KnpLabs#1075 Add the ability to download raw file, needed when size > 1MB (genintho)
This PR was squashed before being merged into the 3.4.x-dev branch. Discussion ---------- API DOC https://docs.github.com/en/rest/repos/contents The API call to return the content of a file return an empty content if a file is bigger than 1MB. To be able to download that file, we need to provide the header `application/vnd.github.VERSION.raw`. When doing so, the API return a the raw file, instead of a JSON object with some attributes + the file content. Because the API has a different behavior, I preferred to create a dedicated method as opposed to provide accept more option in the download method and having a bunch of if/else. Note: a 3rd behavior exists for that API call when downloading markdown and passing the `application/vnd.github.VERSION.html` header, which is not supported by this code change. Commits ------- 520ffb9 Add the ability to download raw file, needed when size > 1MB ff458a2 Restore modification committed by mistake e7c393f Style fix
1 parent a43662d commit 0158c30

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

lib/Github/Api/Repository/Contents.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ public function readme($username, $repository, $reference = null)
6161
*
6262
* @link http://developer.github.com/v3/repos/contents/
6363
*
64-
* @param string $username the user who owns the repository
65-
* @param string $repository the name of the repository
66-
* @param string|null $path path to file or directory
67-
* @param string|null $reference reference to a branch or commit
64+
* @param string $username the user who owns the repository
65+
* @param string $repository the name of the repository
66+
* @param string|null $path path to file or directory
67+
* @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
/**
@@ -294,4 +295,25 @@ public function download($username, $repository, $path, $reference = null)
294295

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

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,23 @@ 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+
322339
/**
323340
* @return string
324341
*/

0 commit comments

Comments
 (0)