Skip to content

Commit d418c98

Browse files
Merge pull request #6 from VeiligLanceren-nl/@feature/zip-multiple-files
@feature/zip multiple files
2 parents ddfc70f + 4acb5a4 commit d418c98

File tree

5 files changed

+72
-26
lines changed

5 files changed

+72
-26
lines changed

README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ Create a ZIP archive containing a file from a GitHub repository:
6060
$zipPath = GithubFile::zip('owner/repo', 'path/to/file.txt');
6161
```
6262

63-
The ZIP file will be stored in the default disk's `zips` directory.
63+
The ZIP file will be stored in the default disk's `zips` directory. Or zip multiple files at the same times.
64+
65+
```php
66+
$zipPath = GithubFile::zip('owner/repo', ['path/to/file.txt', 'path/to/file2.txt']);
67+
```
6468

6569
## Testing
6670

src/Facades/GithubFile.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/**
99
* @method static string get(string $repository, string $filePath, string $branch = 'main')
1010
* @method static string download(string $repository, string $filePath, string $disk = 'local', string $branch = 'main')
11-
* @method static string zip(string $repository, string $filePath, string $disk = 'local', string $branch = 'main')
11+
* @method static string zip(string $repository, string|array $filePath, string $disk = 'local', string $branch = 'main')
1212
*
1313
* @see \VeiligLanceren\GithubFile\Services\GithubFileService
1414
*/

src/Interfaces/IGithubFileService.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ public function download(string $repository, string $filePath, string $disk = 'l
3131
* Create a ZIP archive containing a file from a GitHub repository.
3232
*
3333
* @param string $repository The GitHub repository in the format 'owner/repo'.
34-
* @param string $filePath The path to the file within the repository.
35-
* @param string $disk The disk where the ZIP file should be stored.
36-
* @param string $branch The branch name. Defaults to 'main'.
34+
* @param string|array $filePaths
35+
* @param string $disk The disk where the ZIP file should be stored.
36+
* @param string $branch The branch name. Defaults to 'main'.
3737
*
3838
* @return string The path where the ZIP file was stored.
3939
*/
40-
public function zip(string $repository, string $filePath, string $disk = 'local', string $branch = 'main'): string;
40+
public function zip(string $repository, string|array $filePaths, string $disk = 'local', string $branch = 'main'): string;
4141
}

src/Services/GithubFileService.php

+21-17
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,38 @@ public function download(
5959
/**
6060
* {@inheritDoc}
6161
*/
62-
public function zip(string $repository, string $filePath, string $disk = 'local', string $branch = 'main'): string
62+
public function zip(string $repository, string|array $filePaths, string $disk = 'local', string $branch = 'main'): string
6363
{
64-
$files = [];
65-
$filename = basename($filePath);
64+
$filePaths = is_array($filePaths) ? $filePaths : [$filePaths];
65+
$allFiles = [];
6666

67-
if (substr($filePath, -1) === '/') {
68-
$filePath = rtrim($filePath, '/');
69-
$url = "https://api.github.com/repos/{$repository}/contents/{$filePath}?ref={$branch}";
67+
foreach ($filePaths as $filePath) {
68+
$cleanPath = rtrim($filePath, '/');
69+
$url = "https://api.github.com/repos/{$repository}/contents/{$cleanPath}?ref={$branch}";
7070
$response = Http::get($url);
7171

72-
if ($response->successful()) {
73-
$filesData = $response->json();
72+
if (! $response->successful()) {
73+
throw new RuntimeException("Failed to fetch file from GitHub: {$url}");
74+
}
75+
76+
$data = $response->json();
7477

75-
foreach ($filesData as $file) {
76-
if ($file['type'] === 'file') {
77-
$fileContent = Http::get($file['download_url'])->body();
78-
$files[] = ['name' => $file['name'], 'content' => $fileContent];
78+
if (array_is_list($data)) {
79+
foreach ($data as $item) {
80+
if ($item['type'] === 'file') {
81+
$fileContent = Http::get($item['download_url'])->body();
82+
$allFiles[] = ['name' => $item['name'], 'content' => $fileContent];
7983
}
8084
}
8185
} else {
82-
throw new RuntimeException("Failed to fetch directory contents from GitHub: {$url}");
86+
$content = base64_decode($data['content'] ?? '');
87+
$allFiles[] = ['name' => basename($filePath), 'content' => $content];
8388
}
84-
} else {
85-
$fileContent = $this->get($repository, $filePath, $branch);
86-
$files[] = ['name' => $filename, 'content' => $fileContent];
8789
}
8890

89-
return $this->fileZipService->createZip($filePath, $files, $disk);
91+
$zipName = 'github-files';
92+
93+
return $this->fileZipService->createZip($zipName, $allFiles, $disk);
9094
}
9195

9296
}

tests/Unit/Services/GithubFileServiceTest.php

+41-3
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,49 @@
3131
});
3232

3333
it('creates a zip archive containing the file', function () {
34+
Http::fake([
35+
'https://api.github.com/repos/owner/repo/contents/path/to/file.txt?ref=main' => Http::response([
36+
'content' => base64_encode('file content'),
37+
], 200),
38+
]);
39+
3440
$service = new GithubFileService();
3541
$zipPath = $service->zip('owner/repo', 'path/to/file.txt');
42+
$relativePath = str_replace(Storage::disk('local')->path(''), '', $zipPath);
43+
44+
Storage::disk('local')->assertExists($relativePath);
45+
expect($zipPath)->toBe(Storage::disk('local')->path($relativePath));
46+
});
47+
48+
it('creates a zip archive from multiple GitHub file and folder paths', function () {
49+
Http::fake([
50+
'https://api.github.com/repos/owner/repo/contents/folder?ref=main' => Http::response([
51+
[
52+
'type' => 'file',
53+
'name' => 'file1.txt',
54+
'download_url' => 'https://raw.githubusercontent.com/owner/repo/main/folder/file1.txt',
55+
],
56+
[
57+
'type' => 'file',
58+
'name' => 'file2.txt',
59+
'download_url' => 'https://raw.githubusercontent.com/owner/repo/main/folder/file2.txt',
60+
],
61+
]),
3662

37-
Storage::disk('local')->assertExists('zips/file.txt.zip');
63+
'https://raw.githubusercontent.com/owner/repo/main/folder/file1.txt' => Http::response('content 1', 200),
64+
'https://raw.githubusercontent.com/owner/repo/main/folder/file2.txt' => Http::response('content 2', 200),
65+
'https://api.github.com/repos/owner/repo/contents/README.md?ref=main' => Http::response([
66+
'content' => base64_encode('readme content'),
67+
]),
68+
]);
69+
70+
$service = new GithubFileService();
71+
$zipPath = $service->zip('owner/repo', ['folder/', 'README.md']);
72+
73+
expect($zipPath)->toBe(Storage::disk('local')->path('zips/github-files.zip'));
3874

39-
expect($zipPath)
40-
->toBe(Storage::disk('local')->path('zips/file.txt.zip'));
75+
Storage::disk('local')->assertExists('zips/github-files.zip');
4176
});
77+
78+
79+

0 commit comments

Comments
 (0)