Skip to content

Commit 72ea53d

Browse files
committed
support query parameters in CLOUDINARY_URL. add support for overwrite in upload. add support for tags flag in resources_by_tag, add delete_all_resources. added deletion cursor
1 parent 62c4da6 commit 72ea53d

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

src/Api.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function resources($options=array()) {
5858
function resources_by_tag($tag, $options=array()) {
5959
$resource_type = \Cloudinary::option_get($options, "resource_type", "image");
6060
$uri = array("resources", $resource_type, "tags", $tag);
61-
return $this->call_api("get", $uri, $this->only($options, array("next_cursor", "max_results")), $options);
61+
return $this->call_api("get", $uri, $this->only($options, array("next_cursor", "max_results", "tags")), $options);
6262
}
6363

6464
function resource($public_id, $options=array()) {
@@ -79,13 +79,20 @@ function delete_resources_by_prefix($prefix, $options=array()) {
7979
$resource_type = \Cloudinary::option_get($options, "resource_type", "image");
8080
$type = \Cloudinary::option_get($options, "type", "upload");
8181
$uri = array("resources", $resource_type, $type);
82-
return $this->call_api("delete", $uri, array_merge(array("prefix"=>$prefix), $this->only($options, array("keep_original"))), $options);
82+
return $this->call_api("delete", $uri, array_merge(array("prefix"=>$prefix), $this->only($options, array("keep_original", "next_cursor"))), $options);
83+
}
84+
85+
function delete_all_resources($options=array()) {
86+
$resource_type = \Cloudinary::option_get($options, "resource_type", "image");
87+
$type = \Cloudinary::option_get($options, "type", "upload");
88+
$uri = array("resources", $resource_type, $type);
89+
return $this->call_api("delete", $uri, array_merge(array("all"=>True), $this->only($options, array("keep_original", "next_cursor"))), $options);
8390
}
8491

8592
function delete_resources_by_tag($tag, $options=array()) {
8693
$resource_type = \Cloudinary::option_get($options, "resource_type", "image");
8794
$uri = array("resources", $resource_type, "tags", $tag);
88-
return $this->call_api("delete", $uri, $this->only($options, array("keep_original")), $options);
95+
return $this->call_api("delete", $uri, $this->only($options, array("keep_original", "next_cursor")), $options);
8996
}
9097

9198
function delete_derived_resources($derived_resource_ids, $options=array()) {

src/Cloudinary.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@ public static function config_from_url($cloudinary_url) {
2727
self::$config = array();
2828
if ($cloudinary_url) {
2929
$uri = parse_url($cloudinary_url);
30-
$config = array("cloud_name" => $uri["host"],
30+
$q_params = array();
31+
parse_str($uri["query"], $q_params);
32+
$config = array_merge($q_params, array(
33+
"cloud_name" => $uri["host"],
3134
"api_key" => $uri["user"],
3235
"api_secret" => $uri["pass"],
33-
"private_cdn" => isset($uri["path"]));
36+
"private_cdn" => isset($uri["path"])));
3437
if (isset($uri["path"])) {
3538
$config["secure_distribution"] = substr($uri["path"], 1);
3639
}

tests/ApiTest.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@ function find_by_attr($elements, $attr, $value) {
3838
return NULL;
3939
}
4040

41-
function test01_resource_types() { // should allow listing resource_types
42-
$result = $this->api->resource_types();
41+
function test01_resource_types() {
42+
// should allow listing resource_types
43+
$result = $this->api->resource_types();
4344
$this->assertContains("image", $result["resource_types"]);
4445
}
4546

4647
function test02_resources() {
4748
// should allow listing resources
48-
$result = $this->api->resources();
49+
$result = $this->api->resources();
4950
$resource = $this->find_by_attr($result["resources"], "public_id", "api_test");
5051
$this->assertNotEquals($resource, NULL);
5152
$this->assertEquals($resource["type"], "upload");
5253
}
5354

5455
function test03_resources_cursor() {
55-
// should allow listing resources with cursor
56+
// should allow listing resources with cursor
5657
$result = $this->api->resources(array("max_results"=>1));
5758
$this->assertNotEquals($result["resources"], NULL);
5859
$this->assertEquals(count($result["resources"]), 1);
@@ -65,14 +66,14 @@ function test03_resources_cursor() {
6566
}
6667

6768
function test04_resources_by_type() {
68-
// should allow listing resources by type
69+
// should allow listing resources by type
6970
$result = $this->api->resources(array("type"=>"upload"));
7071
$resource = $this->find_by_attr($result["resources"], "public_id", "api_test");
7172
$this->assertNotEquals($resource, NULL);
7273
}
7374

7475
function test05_resources_by_prefix() {
75-
// should allow listing resources by prefix
76+
// should allow listing resources by prefix
7677
$result = $this->api->resources(array("type"=>"upload", "prefix"=>"api_test"));
7778
$func = function($resource) {
7879
return $resource["public_id"];
@@ -84,14 +85,14 @@ function test05_resources_by_prefix() {
8485
}
8586

8687
function test06_resources_tag() {
87-
// should allow listing resources by tag
88+
// should allow listing resources by tag
8889
$result = $this->api->resources_by_tag("api_test_tag");
8990
$resource = $this->find_by_attr($result["resources"], "public_id", "api_test");
9091
$this->assertNotEquals($resource, NULL);
9192
}
9293

9394
function test07_resource_metadata() {
94-
// should allow get resource metadata
95+
// should allow get resource metadata
9596
$resource = $this->api->resource("api_test");
9697
$this->assertNotEquals($resource, NULL);
9798
$this->assertEquals($resource["public_id"], "api_test");
@@ -100,7 +101,7 @@ function test07_resource_metadata() {
100101
}
101102

102103
function test08_delete_derived() {
103-
// should allow deleting derived resource
104+
// should allow deleting derived resource
104105
\Cloudinary\Uploader::upload("tests/logo.png", array("public_id"=>"api_test3", "eager"=>array("transformation"=>array("width"=> 101,"crop" => "scale"))));
105106
$resource = $this->api->resource("api_test3");
106107
$this->assertNotEquals($resource, NULL);
@@ -150,14 +151,14 @@ function test09b_delete_resources_by_tag() {
150151

151152
function test10_tags() {
152153
// should allow listing tags
153-
$result = $this->api->tags();
154+
$result = $this->api->tags();
154155
$tags = $result["tags"];
155156
$this->assertContains("api_test_tag", $tags);
156157
}
157158

158159
function test11_tags_prefix() {
159160
// should allow listing tag by prefix
160-
$result = $this->api->tags(array("prefix"=>"api_test"));
161+
$result = $this->api->tags(array("prefix"=>"api_test"));
161162
$tags = $result["tags"];
162163
$this->assertContains("api_test_tag", $tags);
163164
$result = $this->api->tags(array("prefix"=>"api_test_no_such_tag"));
@@ -167,15 +168,15 @@ function test11_tags_prefix() {
167168

168169
function test12_transformations() {
169170
// should allow listing transformations
170-
$result = $this->api->transformations();
171+
$result = $this->api->transformations();
171172
$transformation = $this->find_by_attr($result["transformations"], "name", "c_scale,w_100");
172173

173174
$this->assertNotEquals($transformation, NULL);
174175
$this->assertEquals($transformation["used"], TRUE);
175176
}
176177

177178
function test13_transformation_metadata() {
178-
// should allow getting transformation metadata
179+
// should allow getting transformation metadata
179180
$transformation = $this->api->transformation("c_scale,w_100");
180181
$this->assertNotEquals($transformation, NULL);
181182
$this->assertEquals($transformation["info"], array(array("crop"=> "scale", "width"=> 100)));
@@ -185,7 +186,7 @@ function test13_transformation_metadata() {
185186
}
186187

187188
function test14_transformation_update() {
188-
// should allow updating transformation allowed_for_strict
189+
// should allow updating transformation allowed_for_strict
189190
$this->api->update_transformation("c_scale,w_100", array("allowed_for_strict"=>TRUE));
190191
$transformation = $this->api->transformation("c_scale,w_100");
191192
$this->assertNotEquals($transformation, NULL);
@@ -197,7 +198,7 @@ function test14_transformation_update() {
197198
}
198199

199200
function test15_transformation_create() {
200-
// should allow creating named transformation
201+
// should allow creating named transformation
201202
$this->api->create_transformation("api_test_transformation", array("crop" => "scale", "width" => 102));
202203
$transformation = $this->api->transformation("api_test_transformation");
203204
$this->assertNotEquals($transformation, NULL);
@@ -217,7 +218,7 @@ function test15a_transformation_unsafe_update() {
217218
}
218219

219220
function test16a_transformation_delete() {
220-
// should allow deleting named transformation
221+
// should allow deleting named transformation
221222
$this->api->create_transformation("api_test_transformation2", array("crop" => "scale", "width" => 103));
222223
$this->api->transformation("api_test_transformation2");
223224
$this->api->delete_transformation("api_test_transformation2");
@@ -231,7 +232,7 @@ function test16b_transformation_delete() {
231232
}
232233

233234
function test17a_transformation_delete_implicit() {
234-
// should allow deleting implicit transformation
235+
// should allow deleting implicit transformation
235236
$this->api->transformation("c_scale,w_100");
236237
$this->api->delete_transformation("c_scale,w_100");
237238
}
@@ -249,4 +250,16 @@ function test18_usage() {
249250
$this->assertNotEquals($result["last_updated"], NULL);
250251
}
251252

253+
function test19_delete_derived() {
254+
// should allow deleting all resources
255+
\Cloudinary\Uploader::upload("tests/logo.png", array("public_id"=>"api_test5", "eager"=>array("transformation"=>array("width"=> 101,"crop" => "scale"))));
256+
$resource = $this->api->resource("api_test5");
257+
$this->assertNotEquals($resource, NULL);
258+
$this->assertEquals(count($resource["derived"]), 1);
259+
$this->api->delete_all_resources(array("keep_original" => True));
260+
$resource = $this->api->resource("api_test5");
261+
$this->assertNotEquals($resource, NULL);
262+
$this->assertEquals(count($resource["derived"]), 0);
263+
}
264+
252265
}

0 commit comments

Comments
 (0)