Skip to content

Commit 7ba3f29

Browse files
authored
Merge pull request #128 from ToshY/feature/127
Add Edge Storage API download as ZIP
2 parents e8f0a7f + 3aa9dcc commit 7ba3f29

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

docs/edge-storage-api.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ $edgeStorageApi->downloadFile(
5858
);
5959
```
6060

61+
#### Download Zip
62+
63+
```php
64+
// Root directory.
65+
$edgeStorageApi->downloadZip(
66+
storageZoneName: 'my-storage-zone-1',
67+
body: [
68+
'RootPath' => '/my-storage-zone-1/',
69+
'Paths' => [
70+
'/my-storage-zone-1/',
71+
]
72+
],
73+
);
74+
75+
// Subdirectory.
76+
$edgeStorageApi->downloadZip(
77+
storageZoneName: 'my-storage-zone-1',
78+
body: [
79+
'RootPath' => '/my-storage-zone-1/',
80+
'Paths' => [
81+
'/my-storage-zone-1/images/',
82+
]
83+
],
84+
);
85+
```
86+
87+
!!! note
88+
- Make sure your `RootPath` and `Paths` contain **leading** and **trailing** slashes.
89+
- If you omit the slashes in `RootPath` this will result in a `400` status code.
90+
- If you omit the slashes in `Paths` this will result in a `200` status code with an empty ZIP file.
91+
92+
!!! warning
93+
94+
- This endpoint (with method `POST`) is currently not documented in the API specifications.
95+
- This request may fail or timeout if the requested directory has too many files or is too big.
96+
6197
#### [Upload File](https://docs.bunny.net/reference/put_-storagezonename-path-filename)
6298

6399
```php

phpmd.baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<violation rule="PHPMD\Rule\Design\TooManyPublicMethods" file="src/BaseAPI.php"/>
77
<violation rule="PHPMD\Rule\Design\WeightedMethodCount" file="src/BaseAPI.php"/>
88
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/BaseAPI.php"/>
9+
<violation rule="PHPMD\Rule\Design\CouplingBetweenObjects" file="src/EdgeStorageAPI.php"/>
910
<violation rule="PHPMD\Rule\Design\LongMethod" file="src/Model/API/Base/PullZone/AddPullZone.php" method="getBody"/>
1011
<violation rule="PHPMD\Rule\Design\LongMethod" file="src/Model/API/Base/PullZone/UpdatePullZone.php" method="getBody"/>
1112
<violation rule="PHPMD\Rule\CleanCode\BooleanArgumentFlag" file="src/Model/AbstractParameter.php"/>

src/EdgeStorageAPI.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use Psr\Http\Client\ClientExceptionInterface;
88
use ToshY\BunnyNet\Client\BunnyClient;
99
use ToshY\BunnyNet\Enum\Region;
10+
use ToshY\BunnyNet\Helper\BodyContentHelper;
1011
use ToshY\BunnyNet\Model\API\EdgeStorage\BrowseFiles\ListFiles;
1112
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DeleteFile;
1213
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DownloadFile;
14+
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DownloadZip;
1315
use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\UploadFile;
1416
use ToshY\BunnyNet\Model\Client\Interface\BunnyClientResponseInterface;
17+
use ToshY\BunnyNet\Validator\ParameterValidator;
1518

1619
class EdgeStorageAPI
1720
{
@@ -52,6 +55,32 @@ public function downloadFile(
5255
);
5356
}
5457

58+
/**
59+
* @throws ClientExceptionInterface
60+
* @throws Exception\BunnyClientResponseException
61+
* @throws Exception\InvalidTypeForKeyValueException
62+
* @throws Exception\InvalidTypeForListValueException
63+
* @throws Exception\JSONException
64+
* @throws Exception\ParameterIsRequiredException
65+
* @param string $storageZoneName
66+
* @param mixed $body
67+
* @return BunnyClientResponseInterface
68+
*/
69+
public function downloadZip(
70+
string $storageZoneName,
71+
mixed $body,
72+
): BunnyClientResponseInterface {
73+
$endpoint = new DownloadZip();
74+
75+
ParameterValidator::validate($body, $endpoint->getBody());
76+
77+
return $this->client->request(
78+
endpoint: $endpoint,
79+
parameters: [$storageZoneName],
80+
body: BodyContentHelper::getBody($body),
81+
);
82+
}
83+
5584
/**
5685
* @throws ClientExceptionInterface
5786
* @throws Exception\BunnyClientResponseException
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles;
6+
7+
use ToshY\BunnyNet\Enum\Header;
8+
use ToshY\BunnyNet\Enum\Method;
9+
use ToshY\BunnyNet\Enum\Type;
10+
use ToshY\BunnyNet\Model\AbstractParameter;
11+
use ToshY\BunnyNet\Model\EndpointBodyInterface;
12+
use ToshY\BunnyNet\Model\EndpointInterface;
13+
14+
class DownloadZip implements EndpointInterface, EndpointBodyInterface
15+
{
16+
public function getMethod(): Method
17+
{
18+
return Method::POST;
19+
}
20+
21+
public function getPath(): string
22+
{
23+
return '%s/';
24+
}
25+
26+
public function getBody(): array
27+
{
28+
return [
29+
new AbstractParameter(name: 'RootPath', type: Type::STRING_TYPE),
30+
new AbstractParameter(name: 'Paths', type: Type::ARRAY_TYPE, required: true, children: [
31+
new AbstractParameter(name: null, type: Type::STRING_TYPE),
32+
]),
33+
];
34+
}
35+
36+
public function getHeaders(): array
37+
{
38+
return [
39+
Header::CONTENT_TYPE_JSON,
40+
];
41+
}
42+
}

0 commit comments

Comments
 (0)