From 3aa9dcc60e8fbf4e63f4764cc8be4f5bf9aa333e Mon Sep 17 00:00:00 2001 From: ToshY <31921460+ToshY@users.noreply.github.com> Date: Fri, 16 Aug 2024 18:43:50 +0200 Subject: [PATCH] fixes 127; edge storage api download as zip --- docs/edge-storage-api.md | 36 ++++++++++++++++ phpmd.baseline.xml | 1 + src/EdgeStorageAPI.php | 29 +++++++++++++ .../EdgeStorage/ManageFiles/DownloadZip.php | 42 +++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 src/Model/API/EdgeStorage/ManageFiles/DownloadZip.php diff --git a/docs/edge-storage-api.md b/docs/edge-storage-api.md index fac02e8..88b8aec 100644 --- a/docs/edge-storage-api.md +++ b/docs/edge-storage-api.md @@ -58,6 +58,42 @@ $edgeStorageApi->downloadFile( ); ``` +#### Download Zip + +```php +// Root directory. +$edgeStorageApi->downloadZip( + storageZoneName: 'my-storage-zone-1', + body: [ + 'RootPath' => '/my-storage-zone-1/', + 'Paths' => [ + '/my-storage-zone-1/', + ] + ], +); + +// Subdirectory. +$edgeStorageApi->downloadZip( + storageZoneName: 'my-storage-zone-1', + body: [ + 'RootPath' => '/my-storage-zone-1/', + 'Paths' => [ + '/my-storage-zone-1/images/', + ] + ], +); +``` + +!!! note + - Make sure your `RootPath` and `Paths` contain **leading** and **trailing** slashes. + - If you omit the slashes in `RootPath` this will result in a `400` status code. + - If you omit the slashes in `Paths` this will result in a `200` status code with an empty ZIP file. + +!!! warning + + - This endpoint (with method `POST`) is currently not documented in the API specifications. + - This request may fail or timeout if the requested directory has too many files or is too big. + #### [Upload File](https://docs.bunny.net/reference/put_-storagezonename-path-filename) ```php diff --git a/phpmd.baseline.xml b/phpmd.baseline.xml index bc5fd1e..73356bf 100644 --- a/phpmd.baseline.xml +++ b/phpmd.baseline.xml @@ -6,6 +6,7 @@ + diff --git a/src/EdgeStorageAPI.php b/src/EdgeStorageAPI.php index e6887c3..e5e2c8f 100644 --- a/src/EdgeStorageAPI.php +++ b/src/EdgeStorageAPI.php @@ -7,11 +7,14 @@ use Psr\Http\Client\ClientExceptionInterface; use ToshY\BunnyNet\Client\BunnyClient; use ToshY\BunnyNet\Enum\Region; +use ToshY\BunnyNet\Helper\BodyContentHelper; use ToshY\BunnyNet\Model\API\EdgeStorage\BrowseFiles\ListFiles; use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DeleteFile; use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DownloadFile; +use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\DownloadZip; use ToshY\BunnyNet\Model\API\EdgeStorage\ManageFiles\UploadFile; use ToshY\BunnyNet\Model\Client\Interface\BunnyClientResponseInterface; +use ToshY\BunnyNet\Validator\ParameterValidator; class EdgeStorageAPI { @@ -52,6 +55,32 @@ public function downloadFile( ); } + /** + * @throws ClientExceptionInterface + * @throws Exception\BunnyClientResponseException + * @throws Exception\InvalidTypeForKeyValueException + * @throws Exception\InvalidTypeForListValueException + * @throws Exception\JSONException + * @throws Exception\ParameterIsRequiredException + * @param string $storageZoneName + * @param mixed $body + * @return BunnyClientResponseInterface + */ + public function downloadZip( + string $storageZoneName, + mixed $body, + ): BunnyClientResponseInterface { + $endpoint = new DownloadZip(); + + ParameterValidator::validate($body, $endpoint->getBody()); + + return $this->client->request( + endpoint: $endpoint, + parameters: [$storageZoneName], + body: BodyContentHelper::getBody($body), + ); + } + /** * @throws ClientExceptionInterface * @throws Exception\BunnyClientResponseException diff --git a/src/Model/API/EdgeStorage/ManageFiles/DownloadZip.php b/src/Model/API/EdgeStorage/ManageFiles/DownloadZip.php new file mode 100644 index 0000000..2322293 --- /dev/null +++ b/src/Model/API/EdgeStorage/ManageFiles/DownloadZip.php @@ -0,0 +1,42 @@ +