Skip to content

Commit 01b87a3

Browse files
authored
Merge pull request #50 from imagekit-developer/dev
Dev
2 parents beaef09 + 5e345f7 commit 01b87a3

File tree

4 files changed

+135
-19
lines changed

4 files changed

+135
-19
lines changed

src/ImageKit/Constants/ErrorMessages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,5 @@ class ErrorMessages
144144
public static $DELETE_CUSTOM_METADATA_ID_MISSING = ['message' => 'Missing Custom Metadata Field ID parameter for this request', 'help' => 'For support kindly contact us at support@imagekit.io .'];
145145
public static $PHASH_DISTANCE_FIRST_PHASH_MISSING = ['message' => 'Missing First pHash parameter for this request', 'help' => 'For support kindly contact us at support@imagekit.io .'];
146146
public static $PHASH_DISTANCE_SECOND_PHASH_MISSING = ['message' => 'Missing Second pHash parameter for this request', 'help' => 'For support kindly contact us at support@imagekit.io .'];
147-
147+
public static $URL_GENERATION_EXPIRESECONDS_PARAMETER_INVALID = ['message' => 'expireSeconds accepts an integer value, non integer value provided.', 'help' => 'For support kindly contact us at support@imagekit.io .'];
148148
}

src/ImageKit/ImageKit.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
*/
2626
class ImageKit
2727
{
28-
/**
29-
* ImageKit SDK VERSION
30-
*
31-
* @var string
32-
*/
33-
const SDK_VERSION = '3.0.0';
34-
3528
/**
3629
* @var Configuration
3730
*/
@@ -127,6 +120,12 @@ public function url($options=null)
127120
return json_encode(Response::respond(true, ((object)ErrorMessages::$URL_GENERATION_TRANSFORMATION_PARAMETER_INVALID)));
128121
}
129122

123+
if (isset($options['signed']) && $options['signed'] === true){
124+
if(isset($options['expireSeconds']) && $options['expireSeconds'] !== '' && !is_numeric($options['expireSeconds'])){
125+
return json_encode(Response::respond(true, ((object)ErrorMessages::$URL_GENERATION_EXPIRESECONDS_PARAMETER_INVALID)));
126+
}
127+
}
128+
130129
$urlInstance = new Url();
131130
return $urlInstance->buildURL(array_merge((array)$this->configuration, $options));
132131
}

src/ImageKit/Url/Url.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ public function buildURL($urlOptions)
4040

4141
if (isset($obj->expireSeconds)) {
4242
$expireSeconds = $obj->expireSeconds;
43-
44-
if ($signed === true && $expireSeconds !== '' && !is_numeric($expireSeconds)) {
45-
throw new \InvalidArgumentException('expireSeconds should be numeric');
46-
}
4743
}
4844

4945
if (isset($obj->path)) {
@@ -340,10 +336,12 @@ public function unparsed_url(array $parsed)
340336
(strlen($host) > 0 ? "//$host" : '') .
341337
(strlen($pathname) > 0 ? "$pathname" : '') .
342338
(strlen($search) > 0 ? str_replace('=',':',$search).'/' : '') .
343-
$file_name .
344-
(strlen($queryParameters) > 0 ? ( '?' . $queryParameters): '') .
345-
((!empty($timestampParameterString) && strlen($timestampParameterString) > 0) ? (((!empty($queryParameters) && strlen($queryParameters)>0)?'&':'?') . $timestampParameterString): '') .
346-
((!empty($signatureParameterString) && strlen($signatureParameterString) > 0) ? ('&' . $signatureParameterString): '');
339+
$file_name .
340+
(strlen($queryParameters) > 0 ? ( '?' . $queryParameters): '') .
341+
((!empty($timestampParameterString) && strlen($timestampParameterString) > 0) ? (((!empty($queryParameters) && strlen($queryParameters)>0)?'&':'?') . $timestampParameterString): '') .
342+
((!empty($signatureParameterString) && strlen($signatureParameterString) > 0) ? (((!empty($queryParameters) && strlen($queryParameters)>0)?'&':
343+
((!empty($timestampParameterString) && strlen($timestampParameterString) > 0)?'&':'?')
344+
) . $signatureParameterString): '');
347345

348346
}
349347
}

tests/ImageKit/Url/UrlTest.php

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,133 @@ public function testUrlSignedUrlWithEmptyExpiryString()
115115
*/
116116
public function testUrlSignedUrlWithInvalidExpiryString()
117117
{
118-
$this->expectException(\InvalidArgumentException::class);
119-
$this->expectExceptionMessage('expireSeconds should be numeric');
120118
$url = $this->client->url([
121119
'path' => '/default-image.jpg',
122120
'signed' => true,
123121
'expireSeconds' => 'asdad'
124122
]);
125-
// UrlTest::assertEquals('expireSeconds should be numericasd',$url);
123+
UrlTest::assertEquals('expireSeconds accepts an integer value, non integer value provided.',json_decode($url)->error->message);
124+
}
125+
126+
/**
127+
*
128+
*/
129+
public function testUrlSignedUrlWithoutExpiry()
130+
{
131+
$url = $this->client->url([
132+
'path' => '/default-image.jpg',
133+
'signed' => true
134+
]);
135+
UrlTest::assertStringContainsString(
136+
'?ik-s=',
137+
$url
138+
);
139+
}
140+
141+
/**
142+
*
143+
*/
144+
public function testUrlSignedUrlWithExpiryWithTransformation()
145+
{
146+
$url = $this->client->url([
147+
'path' => '/default-image.jpg',
148+
"transformation" => [
149+
[
150+
"height" => "300",
151+
]
152+
],
153+
'signed' => true,
154+
'expireSeconds' => 300
155+
]);
156+
UrlTest::assertStringContainsString(
157+
'?ik-t=',
158+
$url
159+
);
160+
UrlTest::assertStringContainsString(
161+
'&ik-s=',
162+
$url
163+
);
164+
}
165+
166+
/**
167+
*
168+
*/
169+
public function testUrlSignedUrlWithExpiryWithQueryParameters()
170+
{
171+
$url = $this->client->url([
172+
'path' => '/default-image.jpg',
173+
"queryParameters" =>
174+
[
175+
"key" => "value"
176+
],
177+
'signed' => true,
178+
'expireSeconds' => 300
179+
]);
180+
UrlTest::assertStringContainsString(
181+
'&ik-t=',
182+
$url
183+
);
184+
UrlTest::assertStringContainsString(
185+
'&ik-s=',
186+
$url
187+
);
188+
}
189+
190+
/**
191+
*
192+
*/
193+
public function testUrlSignedUrlWithExpiryWithTransformationWithQueryParameters()
194+
{
195+
$url = $this->client->url([
196+
'path' => '/default-image.jpg',
197+
"queryParameters" =>
198+
[
199+
"key" => "value"
200+
],
201+
"transformation" => [
202+
[
203+
"height" => "300",
204+
]
205+
],
206+
'signed' => true,
207+
'expireSeconds' => 300
208+
]);
209+
UrlTest::assertStringContainsString(
210+
'&ik-t=',
211+
$url
212+
);
213+
UrlTest::assertStringContainsString(
214+
'&ik-s=',
215+
$url
216+
);
217+
}
218+
219+
/**
220+
*
221+
*/
222+
public function testUrlSignedUrlWithoutExpiryWithTransformationWithQueryParameters()
223+
{
224+
$url = $this->client->url([
225+
'path' => '/default-image.jpg',
226+
"queryParameters" =>
227+
[
228+
"key" => "value"
229+
],
230+
"transformation" => [
231+
[
232+
"height" => "300",
233+
]
234+
],
235+
'signed' => true
236+
]);
237+
UrlTest::assertStringNotContainsString(
238+
'&ik-t=',
239+
$url
240+
);
241+
UrlTest::assertStringContainsString(
242+
'&ik-s=',
243+
$url
244+
);
126245
}
127246

128247
/**

0 commit comments

Comments
 (0)