Skip to content

Commit b3ae1b6

Browse files
authored
Merge pull request #67 from imagekit-developer/IK-1499
Added checks and isPublished parameter for uplaod
2 parents 4f2194f + 1244cc6 commit b3ae1b6

File tree

5 files changed

+149
-2
lines changed

5 files changed

+149
-2
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ echo $imageURL;
122122
$uploadFile = $imageKit->uploadFile([
123123
'file' => 'file-url', # required, "binary","base64" or "file url"
124124
'fileName' => 'new-file' # required
125+
'checks' => '"file.size" < "1mb"' // optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
125126
]);
126127
```
127128

@@ -562,6 +563,7 @@ The SDK provides a simple interface using the `$imageKit->uploadFile()` method t
562563
$uploadFile = $imageKit->uploadFile([
563564
'file' => 'your_file', // required, "binary","base64" or "file url"
564565
'fileName' => 'your_file_name.jpg', // required
566+
'checks' => '"file.size" < "1mb"', // optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
565567
]);
566568
```
567569
#### Response
@@ -657,6 +659,8 @@ $uploadFile = $imageKit->uploadFile([
657659
]
658660
]
659661
],
662+
'checks' => '"file.size" < "1mb"', // optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
663+
'isPublished' => true,
660664
// "customMetadata" => [
661665
// "SKU" => "VS882HJ2JD",
662666
// "price" => 599.99,
@@ -773,6 +777,27 @@ $updateFileDetails = $imageKit->updateFileDetails(
773777
);
774778
```
775779

780+
**Update publish status**
781+
782+
If `publish` is included in the update options, no other parameters are allowed. If any are present, an error will be returned: `Your request cannot contain any other parameters when publish is present`.
783+
784+
#### Example
785+
```php
786+
// Update parameters
787+
$updateData = [
788+
"publish" => [
789+
"isPublished" => true,
790+
"includeFileVersions" => true
791+
]
792+
];
793+
794+
// Attempt Update
795+
$updateFileDetails = $imageKit->updateFileDetails(
796+
'file_id',
797+
$updateData
798+
);
799+
```
800+
776801
### 6. Add Tags (Bulk) API
777802

778803
Add tags to multiple files in a single request. The method accepts an array of `fileIds` of the files and an array of `tags` that have to be added to those files.

src/ImageKit/Constants/ErrorMessages.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class ErrorMessages
4949
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] ."];
5050
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PRE_TRANSFORMATION = [ "message" => "Invalid pre transformation parameter.", "help" => "For support kindly contact us at [email protected] ."];
5151
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION = [ "message" => "Invalid post transformation parameter.", "help" => "For support kindly contact us at [email protected] ."];
52+
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS = [ "message" => "The value provided for the checks parameter is invalid.", "help" => "For support kindly contact us at [email protected] ."];
53+
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PUBLISH_STATUS = [ "message" => "isPublished must be boolean.", "help" => "For support kindly contact us at [email protected] ."];
5254
public static $MISSING_UPLOAD_DATA = ['message' => 'Missing data for upload', 'help' => 'For support kindly contact us at [email protected] .'];
5355
public static $MISSING_UPLOAD_FILE_PARAMETER = ['message' => 'Missing file parameter for upload', 'help' => 'For support kindly contact us at [email protected] .'];
5456
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,12 @@ public function uploadFile($options=null)
217217
}
218218
}
219219
}
220-
221-
220+
if(isset($options['checks']) && !is_string($options['checks'])){
221+
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS));
222+
}
223+
if(isset($options['isPublished']) && !is_bool($options['isPublished'])){
224+
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PUBLISH_STATUS));
225+
}
222226
$this->httpClient->setUri(Endpoints::getUploadFileEndpoint());
223227
return Upload::upload($options, $this->httpClient);
224228
}

tests/ImageKit/Manage/FileTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,69 @@ public function testUpdateFileDetails()
517517
FileTest::assertEquals($requestMethod,'PATCH');
518518
}
519519

520+
/**
521+
*
522+
*/
523+
public function testUpdateFilePublishStatus()
524+
{
525+
$fileId = '5df36759adf3f523d81dd94f';
526+
527+
$updateData = [
528+
"publish" => [
529+
"isPublished" => true,
530+
"includeFileVersions" => true
531+
]
532+
];
533+
534+
$responseBody = [
535+
'fileId' => '598821f949c0a938d57563bd',
536+
'type' => 'file',
537+
'name' => 'file1.jpg',
538+
'filePath' => '/images/products/file1.jpg',
539+
'tags' => ['t-shirt', 'round-neck', 'sale2019'],
540+
'isPrivateFile' => false,
541+
'isPublished' => true,
542+
'customCoordinates' => null,
543+
'url' => 'https://ik.imagekit.io/your_imagekit_id/images/products/file1.jpg',
544+
'thumbnail' => 'https://ik.imagekit.io/your_imagekit_id/tr:n-media_library_thumbnail/images/products/file1.jpg',
545+
'fileType' => 'image'
546+
];
547+
548+
$mockBodyResponse = Utils::streamFor(json_encode($responseBody));
549+
550+
$mock = new MockHandler([
551+
new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse)
552+
]);
553+
554+
$handlerStack = HandlerStack::create($mock);
555+
556+
$container = [];
557+
$history = Middleware::history($container);
558+
559+
$handlerStack->push($history);
560+
561+
$this->createMockClient($handlerStack);
562+
563+
$response = $this->mockClient->updateFileDetails($fileId, $updateData);
564+
565+
$request = $container[0]['request'];
566+
$requestPath = $request->getUri()->getPath();
567+
$requestBody = $request->getBody();
568+
$stream = Utils::streamFor($requestBody)->getContents();
569+
570+
// Request Check
571+
FileTest::assertEquals("/v1/files/{$fileId}/details",$requestPath);
572+
FileTest::assertEquals($stream,json_encode($updateData));
573+
574+
// Response Check
575+
FileTest::assertNull($response->error);
576+
FileTest::assertEquals(json_encode($responseBody), json_encode($response->result));
577+
578+
// Assert Method
579+
$requestMethod = $container[0]['request']->getMethod();
580+
FileTest::assertEquals($requestMethod,'PATCH');
581+
}
582+
520583
public function testUpdateFileDetailsWithInvalidTags()
521584
{
522585
$fileId = '5df36759adf3f523d81dd94f';

tests/ImageKit/Upload/UploadTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ public function testFileUploadIfSuccessful()
172172
]
173173
]
174174
],
175+
'checks' => "'request.folder' : '/sample-folder'",
176+
'isPublished' => true
175177
];
176178

177179
$mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj));
@@ -215,6 +217,8 @@ public function testFileUploadIfSuccessful()
215217
$this->checkFormData($stream,$boundary,"overwriteCustomMetadata","true");
216218
$this->checkFormData($stream,$boundary,"customMetadata",json_encode($fileOptions['customMetadata']));
217219
$this->checkFormData($stream,$boundary,"transformation",json_encode($fileOptions['transformation']));
220+
$this->checkFormData($stream,$boundary,"checks",$fileOptions['checks']);
221+
$this->checkFormData($stream,$boundary,"isPublished","true");
218222

219223
// Assert Method
220224
$requestMethod = $container[0]['request']->getMethod();
@@ -785,7 +789,56 @@ public function testFileUploadWithInvalidTransformationTypePostTransformation()
785789
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
786790
}
787791

792+
793+
/**
794+
*
795+
*/
796+
public function testFileUploadWithInvalidChecks()
797+
{
798+
$fileOptions = [
799+
'file' => 'http://lorempixel.com/640/480/',
800+
'fileName' => 'test_file_name',
801+
"useUniqueFileName" => true, // true|false
802+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
803+
'checks' => true
804+
];
805+
806+
$error = [
807+
"message" => "The value provided for the checks parameter is invalid.",
808+
"help" => "For support kindly contact us at [email protected] ."
809+
];
810+
811+
$this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error)));
812+
813+
$response = $this->client->uploadFile($fileOptions);
814+
815+
// Request Body Check
816+
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
817+
}
788818

819+
public function testFileUploadWithInvalidPublishStatus()
820+
{
821+
$fileOptions = [
822+
'file' => 'http://lorempixel.com/640/480/',
823+
'fileName' => 'test_file_name',
824+
"useUniqueFileName" => true, // true|false
825+
"responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields
826+
'isPublished' => ''
827+
];
828+
829+
$error = [
830+
"message" => "isPublished must be boolean.",
831+
"help" => "For support kindly contact us at [email protected] ."
832+
];
833+
834+
$this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error)));
835+
836+
$response = $this->client->uploadFile($fileOptions);
837+
838+
// Request Body Check
839+
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
840+
}
841+
789842
protected function setUp()
790843
{
791844
$this->client = new ImageKit(

0 commit comments

Comments
 (0)