Skip to content

Commit 75f1e06

Browse files
Adding deleteMultipleAssets() method for deleting multiple assets (#35)
1 parent e01b75d commit 75f1e06

5 files changed

Lines changed: 137 additions & 37 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Changelog
22

3-
## 1.1.1 - WIP
3+
## 1.1.1 - 2026-01-30
44
- Adding `isExternalUrl()` helper method for Asset
5+
- Adding `getIds()` helper method for Assets
6+
- Adding `deleteMultipleAssets()` method for deleting multiple assets
57
- Full test coverage for Asset and Assets classes
68

79
## 1.1.0 - 2026-01-25

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,37 @@ $deletedAsset = $assetApi->delete($assetId)-data();
923923
echo "DELETED ASSET, ID : " . $deletedAsset->id() . PHP_EOL;
924924
```
925925

926+
927+
### Deleting Multiple Assets
928+
929+
To delete multiple assets, you can use the `deleteMultipleAssets()` method. It requires an array of asset ID strings as parameter:
930+
931+
```php
932+
$response = $assetApi->deleteMultipleAssets($ids);
933+
```
934+
935+
On success, the response will have a `200` status code.
936+
937+
Here is a more complete example that queries assets by tag, retrieves their IDs, and deletes them:
938+
939+
```php
940+
$assetApi = new AssetApi($client, $spaceId);
941+
$tags = ["some-tag"];
942+
$params = [
943+
"withTags" => $tags,
944+
];
945+
$assets = $assetApi->page(new AssetsParams(...$params))->data();
946+
$ids = $assets->getIds();
947+
948+
echo "Going to delete: " . implode(", ", $ids) . PHP_EOL;
949+
try {
950+
$response = $assetApi->deleteMultipleAssets($ids);
951+
echo "Status: " . $response->getResponseStatusCode() . PHP_EOL;
952+
} catch (Exception $e) {
953+
echo "Error deleting: " . $e->getMessage() . PHP_EOL;
954+
}
955+
```
956+
926957
## Handling tags
927958

928959
For using the `TagApi` class you have to import:

src/Data/Assets.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,20 @@ public static function makeFromResponse(array $data): self
2727
{
2828
return new self($data["assets"] ?? []);
2929
}
30+
31+
/**
32+
* Returns an array of the IDs of each Asset in the collection.
33+
*
34+
* @return array<string> Array of IDs strings indexed from 0
35+
*/
36+
public function getIds(): array
37+
{
38+
$array = [];
39+
/** @var Asset $asset */
40+
foreach ($this as $asset) {
41+
$array[] = $asset->id();
42+
}
43+
44+
return $array;
45+
}
3046
}

src/Endpoints/AssetApi.php

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Storyblok\ManagementApi\Response\AssetResponse;
1313
use Storyblok\ManagementApi\Response\AssetsResponse;
1414
use Storyblok\ManagementApi\Response\AssetUploadResponse;
15+
use Storyblok\ManagementApi\Response\StoryblokResponse;
1516
use Storyblok\ManagementApi\Response\StoryblokResponseInterface;
1617
use Symfony\Component\HttpClient\HttpClient;
1718

@@ -23,23 +24,31 @@ class AssetApi extends EndpointSpace
2324
* @param PaginationParams $page
2425
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
2526
*/
26-
public function page(?AssetsParams $params = null, ?PaginationParams $page = null): AssetsResponse
27-
{
28-
if (!$params instanceof \Storyblok\ManagementApi\QueryParameters\AssetsParams) {
27+
public function page(
28+
?AssetsParams $params = null,
29+
?PaginationParams $page = null,
30+
): AssetsResponse {
31+
if (
32+
!$params instanceof
33+
\Storyblok\ManagementApi\QueryParameters\AssetsParams
34+
) {
2935
$params = new AssetsParams();
3036
}
3137

32-
if (!$page instanceof \Storyblok\ManagementApi\QueryParameters\PaginationParams) {
38+
if (
39+
!$page instanceof
40+
\Storyblok\ManagementApi\QueryParameters\PaginationParams
41+
) {
3342
$page = new PaginationParams();
3443
}
3544

3645
$options = [
37-
'query' => array_merge($params->toArray(), $page->toArray()),
46+
"query" => array_merge($params->toArray(), $page->toArray()),
3847
];
3948
$httpResponse = $this->makeHttpRequest(
4049
"GET",
41-
'/v1/spaces/' . $this->spaceId . '/assets',
42-
options: $options
50+
"/v1/spaces/" . $this->spaceId . "/assets",
51+
options: $options,
4352
);
4453

4554
return new AssetsResponse($httpResponse);
@@ -54,7 +63,7 @@ public function get(string|int $assetId): AssetResponse
5463
{
5564
$httpResponse = $this->makeHttpRequest(
5665
"GET",
57-
'/v1/spaces/' . $this->spaceId . '/assets/' . $assetId
66+
"/v1/spaces/" . $this->spaceId . "/assets/" . $assetId,
5867
);
5968
return new AssetResponse($httpResponse);
6069
}
@@ -67,36 +76,39 @@ public function buildPayload(
6776
string|int|null $parent_id = null,
6877
): array {
6978
$payload = [
70-
'filename' => $filename,
79+
"filename" => $filename,
7180
//'size' => $width . 'x' . $height,
72-
'validate_upload' => 1,
73-
'parent_id' => $parent_id,
81+
"validate_upload" => 1,
82+
"parent_id" => $parent_id,
7483
];
7584
$size = getimagesize($filename);
7685
if ($size !== false) {
7786
$width = $size[0];
7887
$height = $size[1];
79-
$payload['size'] = $width . 'x' . $height;
88+
$payload["size"] = $width . "x" . $height;
8089
}
8190

8291
return $payload;
8392
}
8493

85-
public function upload(string $filename, string|int|null $parent_id = null): AssetUploadResponse
86-
{
94+
public function upload(
95+
string $filename,
96+
string|int|null $parent_id = null,
97+
): AssetUploadResponse {
8798
// =========== CREATE A SIGNED REQUEST
8899
$payload = $this->buildPayload($filename, $parent_id);
89100

90101
$signedResponse = $this->makeRequest(
91102
"POST",
92-
'/v1/spaces/' . $this->spaceId . '/assets/',
93-
[ 'body' => $payload ],
103+
"/v1/spaces/" . $this->spaceId . "/assets/bulk_destroy",
104+
["body" => $payload],
94105
);
95-
if (! $signedResponse->isOk()) {
106+
if (!$signedResponse->isOk()) {
96107
throw new \Exception(
97-
"Upload Asset, Signed Request call failed (Step 1) , "
98-
. $signedResponse->getResponseStatusCode() . " - "
99-
. $signedResponse->getErrorMessage(),
108+
"Upload Asset, Signed Request call failed (Step 1) , " .
109+
$signedResponse->getResponseStatusCode() .
110+
" - " .
111+
$signedResponse->getErrorMessage(),
100112
);
101113
}
102114

@@ -109,8 +121,8 @@ public function upload(string $filename, string|int|null $parent_id = null): Ass
109121
$postFields = $fields->toArray();
110122
}
111123

112-
$postFields['file'] = fopen($filename, 'r');
113-
$postUrl = $signedResponseData->getString('post_url');
124+
$postFields["file"] = fopen($filename, "r");
125+
$postUrl = $signedResponseData->getString("post_url");
114126

115127
/*
116128
$responseUpload = $this->makeHttpRequest(
@@ -123,26 +135,32 @@ public function upload(string $filename, string|int|null $parent_id = null): Ass
123135
);
124136
*/
125137

126-
$responseUpload = $this->managementClient->httpAssetClient()->request(
127-
"POST",
128-
$postUrl,
129-
[
138+
$responseUpload = $this->managementClient
139+
->httpAssetClient()
140+
->request("POST", $postUrl, [
130141
"body" => $postFields,
131-
],
132-
);
133-
134-
if (!($responseUpload->getStatusCode() >= 200 && $responseUpload->getStatusCode() < 300)) {
142+
]);
143+
144+
if (
145+
!(
146+
$responseUpload->getStatusCode() >= 200 &&
147+
$responseUpload->getStatusCode() < 300
148+
)
149+
) {
135150
//var_dump($responseUpload->getInfo());
136-
throw new \Exception("Upload Asset, Upload call failed (Step 2) , " . $responseUpload->getStatusCode());
151+
throw new \Exception(
152+
"Upload Asset, Upload call failed (Step 2) , " .
153+
$responseUpload->getStatusCode(),
154+
);
137155
}
138156

139157
$httpResponse = $this->makeHttpRequest(
140158
"GET",
141-
'/v1/spaces/' .
159+
"/v1/spaces/" .
142160
$this->spaceId .
143-
'/assets/' .
144-
$signedResponseData->getString('id') .
145-
'/finish_upload'
161+
"/assets/" .
162+
$signedResponseData->getString("id") .
163+
"/finish_upload",
146164
);
147165
return new AssetUploadResponse($httpResponse);
148166
}
@@ -154,8 +172,27 @@ public function delete(string $assetId): AssetResponse
154172
{
155173
$httpResponse = $this->makeHttpRequest(
156174
"DELETE",
157-
'/v1/spaces/' . $this->spaceId . '/assets/' . $assetId
175+
"/v1/spaces/" . $this->spaceId . "/assets/" . $assetId,
158176
);
159177
return new AssetResponse($httpResponse);
160178
}
179+
180+
/**
181+
* Delete multiple assets via their IDs.
182+
* @link https://www.storyblok.com/docs/api/management/core-resources/assets/delete-multiple-assets
183+
* @param string[] $assetIds
184+
*/
185+
public function deleteMultipleAssets(array $assetIds): StoryblokResponseInterface
186+
{
187+
$payload = [
188+
"ids" => $assetIds,
189+
];
190+
return $this->makeRequest(
191+
"POST",
192+
"/v1/spaces/" . $this->spaceId . "/assets/bulk_destroy",
193+
[
194+
"body" => json_encode($payload),
195+
],
196+
);
197+
}
161198
}

tests/Unit/AssetsDataTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@ public function testAssetsData(): void
2323
$assets = Assets::makeFromResponse($content);
2424
$this->assertCount(2, $assets);
2525
}
26+
27+
public function testAssetsGetIds(): void
28+
{
29+
$contentString = $this->mockData("list-assets");
30+
$content = json_decode($contentString, true);
31+
// chewck if is array
32+
$this->assertIsArray($content);
33+
$this->assertArrayHasKey("assets", $content);
34+
$assets = Assets::make($content["assets"]);
35+
36+
$this->assertCount(2, $assets);
37+
$ids = $assets->getIds();
38+
$this->assertCount(2, $ids);
39+
}
2640
}

0 commit comments

Comments
 (0)