Skip to content

Commit ffa5504

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
added transformation parameter
1 parent 858ddaa commit ffa5504

File tree

5 files changed

+318
-4
lines changed

5 files changed

+318
-4
lines changed

sample/upload_api/index.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,16 @@
1818
'folder' => 'sample-folder',
1919
'tags' => implode(['abd', 'def']),
2020
'useUniqueFileName' => false,
21-
'customCoordinates' => implode(',', ['10', '10', '100', '100'])
21+
'customCoordinates' => implode(',', ['10', '10', '100', '100']),
22+
'transformation' => [
23+
'pre' => 'l-text,i-Imagekit,fs-50,l-end',
24+
'post' => [
25+
[
26+
'type' => 'transformation',
27+
'value' => 'h-100'
28+
]
29+
]
30+
],
2231
]);
2332

2433
echo "\n\n";
@@ -34,7 +43,16 @@
3443
'folder' => 'sample-folder',
3544
'tags' => implode(['abd', 'def']),
3645
'useUniqueFileName' => true,
37-
'customCoordinates' => implode(',', ['10', '10', '100', '100'])
46+
'customCoordinates' => implode(',', ['10', '10', '100', '100']),
47+
'transformation' => [
48+
'pre' => 'l-text,i-Imagekit,fs-50,l-end',
49+
'post' => [
50+
[
51+
'type' => 'transformation',
52+
'value' => 'h-100'
53+
]
54+
]
55+
],
3856
]);
3957

4058
echo "\n\n";
@@ -51,7 +69,16 @@
5169
'folder' => 'sample-folder',
5270
'tags' => implode(['abd', 'def']),
5371
'useUniqueFileName' => true,
54-
'customCoordinates' => implode(',', ['10', '10', '100', '100'])
72+
'customCoordinates' => implode(',', ['10', '10', '100', '100']),
73+
'transformation' => [
74+
'pre' => 'l-text,i-Imagekit,fs-50,l-end',
75+
'post' => [
76+
[
77+
'type' => 'transformation',
78+
'value' => 'h-100'
79+
]
80+
]
81+
],
5582
]);
5683

5784
echo "\n\n";

src/ImageKit/Constants/ErrorMessages.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ class ErrorMessages
4646
public static $UPLOAD_FILE_PARAMETER_OPTIONS_OVERWRITECUSTOMMETADATA_INVALID = ['message' => 'overwriteCustomMetadata must be boolean', 'help' => 'For support kindly contact us at [email protected] .'];
4747
public static $UPLOAD_FILE_PARAMETER_OPTIONS_EXTENSIONS_INVALID = ['message' => 'extensions must be an array', 'help' => 'For support kindly contact us at [email protected] .'];
4848
public static $UPLOAD_FILE_PARAMETER_OPTIONS_CUSTOMMETADATA_INVALID = ['message' => 'customMetadata must be an array', 'help' => 'For support kindly contact us at [email protected] .'];
49+
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_TRANSFORMATION = [ "message" => "Invalid transformation parameter. Please include at least pre, post, or both.", "help" => "For support kindly contact us at [email protected] ."];
50+
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PRE_TRANSFORMATION = [ "message" => "Invalid pre transformation parameter.", "help" => "For support kindly contact us at [email protected] ."];
51+
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION = [ "message" => "Invalid post transformation parameter.", "help" => "For support kindly contact us at [email protected] ."];
4952
public static $MISSING_UPLOAD_DATA = ['message' => 'Missing data for upload', 'help' => 'For support kindly contact us at [email protected] .'];
5053
public static $MISSING_UPLOAD_FILE_PARAMETER = ['message' => 'Missing file parameter for upload', 'help' => 'For support kindly contact us at [email protected] .'];
5154
public static $MISSING_UPLOAD_FILENAME_PARAMETER = ['message' => 'Missing fileName parameter for upload', 'help' => 'For support kindly contact us at [email protected] .'];

src/ImageKit/ImageKit.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,28 @@ public function uploadFile($options=null)
196196
if(isset($options['customMetadata']) && !is_array($options['customMetadata'])){
197197
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_CUSTOMMETADATA_INVALID));
198198
}
199+
if (isset($options['transformation'])) {
200+
if (!isset($options['transformation']['pre']) && !isset($options['transformation']['post'])) {
201+
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_TRANSFORMATION));
202+
}
203+
if (isset($options['transformation']['pre']) && empty($options['transformation']['pre'])) {
204+
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PRE_TRANSFORMATION));
205+
}
206+
if (isset($options['transformation']['post'])) {
207+
if (is_array($options['transformation']['post'])) {
208+
foreach ($options['transformation']['post'] as $transformation) {
209+
if ($transformation['type'] === "abs" && (!isset($transformation['protocol']) || !isset($transformation['value']))) {
210+
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION));
211+
} else if ($transformation['type'] === "transformation" && empty($transformation['value'])) {
212+
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION));
213+
}
214+
}
215+
} else {
216+
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION));
217+
}
218+
}
219+
}
220+
199221

200222
$this->httpClient->setUri(Endpoints::getUploadFileEndpoint());
201223
return Upload::upload($options, $this->httpClient);

src/ImageKit/Upload/Upload.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public static function upload(array $uploadOptions, GuzzleHttpWrapper $resource)
5353
$payload['customMetadata'] = json_encode($payload['customMetadata']);
5454
}
5555

56+
if (isset($payload['transformation'])) {
57+
$payload['transformation'] = json_encode($payload['transformation']);
58+
}
59+
5660
$resource->setDatas((array)$payload);
5761
try {
5862
$res = $resource->postMultipart();

tests/ImageKit/Upload/UploadTest.php

Lines changed: 259 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,16 @@ public function testFileUploadIfSuccessful()
162162
"customMetadata" => [ // An array of created custom fields, for more details refer to docs
163163
"SKU" => "VS882HJ2JD",
164164
"price" => 599.99,
165-
]
165+
],
166+
'transformation' => [
167+
'pre' => 'l-text,i-Imagekit,fs-50,l-end',
168+
'post' => [
169+
[
170+
'type' => 'transformation',
171+
'value' => 'h-100'
172+
]
173+
]
174+
],
166175
];
167176

168177
$mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj));
@@ -205,6 +214,126 @@ public function testFileUploadIfSuccessful()
205214
$this->checkFormData($stream,$boundary,"overwriteAITags","false");
206215
$this->checkFormData($stream,$boundary,"overwriteCustomMetadata","true");
207216
$this->checkFormData($stream,$boundary,"customMetadata",json_encode($fileOptions['customMetadata']));
217+
$this->checkFormData($stream,$boundary,"transformation",json_encode($fileOptions['transformation']));
218+
219+
// Assert Method
220+
$requestMethod = $container[0]['request']->getMethod();
221+
UploadTest::assertEquals($requestMethod,'POST');
222+
223+
// Response Check
224+
UploadTest::assertEquals(json_encode($this->uploadSuccessResponseObj), json_encode($response->result));
225+
}
226+
227+
/**
228+
*
229+
*/
230+
public function testFileUploadWithOnlyPreTransformationIfSuccessful()
231+
{
232+
$fileOptions = [
233+
'file' => 'http://lorempixel.com/640/480/',
234+
'fileName' => 'test_file_name',
235+
"useUniqueFileName" => true, // true|false
236+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
237+
'transformation' => [
238+
'pre' => 'l-text,i-Imagekit,fs-50,l-end',
239+
],
240+
];
241+
242+
$mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj));
243+
244+
245+
$mock = new MockHandler([
246+
new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse)
247+
]);
248+
249+
$handlerStack = HandlerStack::create($mock);
250+
251+
$container = [];
252+
$history = Middleware::history($container);
253+
254+
$handlerStack->push($history);
255+
256+
$this->createMockClient($handlerStack);
257+
258+
$response = $this->mockClient->uploadFile($fileOptions);
259+
260+
$requestBody = $container[0]['request']->getBody();
261+
$requestHeaders = $container[0]['request']->getHeaders();
262+
$boundary = str_replace("multipart/form-data; boundary=","",$requestHeaders["Content-Type"][0]);
263+
264+
UploadTest::assertArrayHasKey("Content-Type",$requestHeaders);
265+
UploadTest::assertStringStartsWith("multipart/form-data; boundary=",$requestHeaders['Content-Type'][0]);
266+
267+
$stream = Utils::streamFor($requestBody);
268+
$stream = str_replace('\r\n',' ',json_encode($stream->getContents()));
269+
270+
$this->checkFormData($stream,$boundary,"file",$fileOptions['file']);
271+
$this->checkFormData($stream,$boundary,"fileName",$fileOptions['fileName']);
272+
$this->checkFormData($stream,$boundary,"useUniqueFileName","true");
273+
$this->checkFormData($stream,$boundary,"responseFields",implode(",", ["tags", "customMetadata"]));
274+
$this->checkFormData($stream,$boundary,"transformation",json_encode($fileOptions['transformation']));
275+
276+
// Assert Method
277+
$requestMethod = $container[0]['request']->getMethod();
278+
UploadTest::assertEquals($requestMethod,'POST');
279+
280+
// Response Check
281+
UploadTest::assertEquals(json_encode($this->uploadSuccessResponseObj), json_encode($response->result));
282+
}
283+
284+
/**
285+
*
286+
*/
287+
public function testFileUploadWithOnlyPostTransformationIfSuccessful()
288+
{
289+
$fileOptions = [
290+
'file' => 'http://lorempixel.com/640/480/',
291+
'fileName' => 'test_file_name',
292+
"useUniqueFileName" => true, // true|false
293+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
294+
'transformation' => [
295+
'post' => [
296+
[
297+
'type' => 'transformation',
298+
'value' => 'h-100'
299+
]
300+
]
301+
],
302+
];
303+
304+
$mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj));
305+
306+
307+
$mock = new MockHandler([
308+
new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse)
309+
]);
310+
311+
$handlerStack = HandlerStack::create($mock);
312+
313+
$container = [];
314+
$history = Middleware::history($container);
315+
316+
$handlerStack->push($history);
317+
318+
$this->createMockClient($handlerStack);
319+
320+
$response = $this->mockClient->uploadFile($fileOptions);
321+
322+
$requestBody = $container[0]['request']->getBody();
323+
$requestHeaders = $container[0]['request']->getHeaders();
324+
$boundary = str_replace("multipart/form-data; boundary=","",$requestHeaders["Content-Type"][0]);
325+
326+
UploadTest::assertArrayHasKey("Content-Type",$requestHeaders);
327+
UploadTest::assertStringStartsWith("multipart/form-data; boundary=",$requestHeaders['Content-Type'][0]);
328+
329+
$stream = Utils::streamFor($requestBody);
330+
$stream = str_replace('\r\n',' ',json_encode($stream->getContents()));
331+
332+
$this->checkFormData($stream,$boundary,"file",$fileOptions['file']);
333+
$this->checkFormData($stream,$boundary,"fileName",$fileOptions['fileName']);
334+
$this->checkFormData($stream,$boundary,"useUniqueFileName","true");
335+
$this->checkFormData($stream,$boundary,"responseFields",implode(",", ["tags", "customMetadata"]));
336+
$this->checkFormData($stream,$boundary,"transformation",json_encode($fileOptions['transformation']));
208337

209338
// Assert Method
210339
$requestMethod = $container[0]['request']->getMethod();
@@ -527,6 +656,135 @@ public function testServerSideError()
527656
// Request Body Check
528657
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
529658
}
659+
660+
/**
661+
*
662+
*/
663+
public function testFileUploadWithInvalidTransformation()
664+
{
665+
$fileOptions = [
666+
'file' => 'http://lorempixel.com/640/480/',
667+
'fileName' => 'test_file_name',
668+
"useUniqueFileName" => true, // true|false
669+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
670+
'transformation' => [],
671+
];
672+
673+
$error = [
674+
"message" => "Invalid transformation parameter. Please include at least pre, post, or both.",
675+
"help" => "For support kindly contact us at [email protected] ."
676+
];
677+
678+
$mockBodyResponse = Utils::streamFor(json_encode($fileOptions));
679+
680+
$this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error)));
681+
682+
$response = $this->client->uploadFile($fileOptions);
683+
684+
// Request Body Check
685+
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
686+
}
687+
688+
/**
689+
*
690+
*/
691+
public function testFileUploadWithInvalidPreTransformation()
692+
{
693+
$fileOptions = [
694+
'file' => 'http://lorempixel.com/640/480/',
695+
'fileName' => 'test_file_name',
696+
"useUniqueFileName" => true, // true|false
697+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
698+
'transformation' => [
699+
'pre' => '',
700+
],
701+
];
702+
703+
$error = [
704+
"message" => "Invalid pre transformation parameter.",
705+
"help" => "For support kindly contact us at [email protected] ."
706+
];
707+
708+
$mockBodyResponse = Utils::streamFor(json_encode($fileOptions));
709+
710+
$this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error)));
711+
712+
$response = $this->client->uploadFile($fileOptions);
713+
714+
// Request Body Check
715+
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
716+
}
717+
718+
/**
719+
*
720+
*/
721+
public function testFileUploadWithInvalidAbsTypePostTransformation()
722+
{
723+
$fileOptions = [
724+
'file' => 'http://lorempixel.com/640/480/',
725+
'fileName' => 'test_file_name',
726+
"useUniqueFileName" => true, // true|false
727+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
728+
'transformation' => [
729+
'post' => [
730+
[
731+
'type' => 'abs',
732+
'value' => ''
733+
]
734+
]
735+
],
736+
];
737+
738+
$error = [
739+
"message" => "Invalid post transformation parameter.",
740+
"help" => "For support kindly contact us at [email protected] ."
741+
];
742+
743+
$mockBodyResponse = Utils::streamFor(json_encode($fileOptions));
744+
745+
$this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error)));
746+
747+
$response = $this->client->uploadFile($fileOptions);
748+
749+
// Request Body Check
750+
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
751+
}
752+
753+
/**
754+
*
755+
*/
756+
public function testFileUploadWithInvalidTransformationTypePostTransformation()
757+
{
758+
$fileOptions = [
759+
'file' => 'http://lorempixel.com/640/480/',
760+
'fileName' => 'test_file_name',
761+
"useUniqueFileName" => true, // true|false
762+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
763+
'transformation' => [
764+
'post' => [
765+
[
766+
'type' => 'transformation',
767+
'value' => ''
768+
]
769+
]
770+
],
771+
];
772+
773+
$error = [
774+
"message" => "Invalid post transformation parameter.",
775+
"help" => "For support kindly contact us at [email protected] ."
776+
];
777+
778+
$mockBodyResponse = Utils::streamFor(json_encode($fileOptions));
779+
780+
$this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error)));
781+
782+
$response = $this->client->uploadFile($fileOptions);
783+
784+
// Request Body Check
785+
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
786+
}
787+
530788

531789
protected function setUp(): void
532790
{

0 commit comments

Comments
 (0)