diff --git a/README.md b/README.md index 97253222..69c9d458 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,7 @@ echo $imageURL; $uploadFile = $imageKit->uploadFile([ 'file' => 'file-url', # required, "binary","base64" or "file url" 'fileName' => 'new-file' # required + 'checks' => '"file.size" < "1mb"' // optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library. ]); ``` @@ -562,6 +563,7 @@ The SDK provides a simple interface using the `$imageKit->uploadFile()` method t $uploadFile = $imageKit->uploadFile([ 'file' => 'your_file', // required, "binary","base64" or "file url" 'fileName' => 'your_file_name.jpg', // required + 'checks' => '"file.size" < "1mb"', // optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library. ]); ``` #### Response @@ -657,6 +659,8 @@ $uploadFile = $imageKit->uploadFile([ ] ] ], + 'checks' => '"file.size" < "1mb"', // optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library. + 'isPublished' => true, // "customMetadata" => [ // "SKU" => "VS882HJ2JD", // "price" => 599.99, @@ -773,6 +777,27 @@ $updateFileDetails = $imageKit->updateFileDetails( ); ``` +**Update publish status** + +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`. + +#### Example +```php +// Update parameters +$updateData = [ + "publish" => [ + "isPublished" => true, + "includeFileVersions" => true + ] +]; + +// Attempt Update +$updateFileDetails = $imageKit->updateFileDetails( + 'file_id', + $updateData +); +``` + ### 6. Add Tags (Bulk) API 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. diff --git a/src/ImageKit/Constants/ErrorMessages.php b/src/ImageKit/Constants/ErrorMessages.php index e1534ab2..835751da 100644 --- a/src/ImageKit/Constants/ErrorMessages.php +++ b/src/ImageKit/Constants/ErrorMessages.php @@ -49,6 +49,8 @@ class ErrorMessages 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 support@imagekit.io ."]; public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PRE_TRANSFORMATION = [ "message" => "Invalid pre transformation parameter.", "help" => "For support kindly contact us at support@imagekit.io ."]; public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION = [ "message" => "Invalid post transformation parameter.", "help" => "For support kindly contact us at support@imagekit.io ."]; + 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 support@imagekit.io ."]; + public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PUBLISH_STATUS = [ "message" => "isPublished must be boolean.", "help" => "For support kindly contact us at support@imagekit.io ."]; public static $MISSING_UPLOAD_DATA = ['message' => 'Missing data for upload', 'help' => 'For support kindly contact us at support@imagekit.io .']; public static $MISSING_UPLOAD_FILE_PARAMETER = ['message' => 'Missing file parameter for upload', 'help' => 'For support kindly contact us at support@imagekit.io .']; public static $MISSING_UPLOAD_FILENAME_PARAMETER = ['message' => 'Missing fileName parameter for upload', 'help' => 'For support kindly contact us at support@imagekit.io .']; diff --git a/src/ImageKit/ImageKit.php b/src/ImageKit/ImageKit.php index fe9a97d5..33089341 100644 --- a/src/ImageKit/ImageKit.php +++ b/src/ImageKit/ImageKit.php @@ -217,8 +217,12 @@ public function uploadFile($options=null) } } } - - + if(isset($options['checks']) && !is_string($options['checks'])){ + return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS)); + } + if(isset($options['isPublished']) && !is_bool($options['isPublished'])){ + return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PUBLISH_STATUS)); + } $this->httpClient->setUri(Endpoints::getUploadFileEndpoint()); return Upload::upload($options, $this->httpClient); } diff --git a/tests/ImageKit/Manage/FileTest.php b/tests/ImageKit/Manage/FileTest.php index 7f81c96d..d53ecfd0 100644 --- a/tests/ImageKit/Manage/FileTest.php +++ b/tests/ImageKit/Manage/FileTest.php @@ -517,6 +517,69 @@ public function testUpdateFileDetails() FileTest::assertEquals($requestMethod,'PATCH'); } + /** + * + */ + public function testUpdateFilePublishStatus() + { + $fileId = '5df36759adf3f523d81dd94f'; + + $updateData = [ + "publish" => [ + "isPublished" => true, + "includeFileVersions" => true + ] + ]; + + $responseBody = [ + 'fileId' => '598821f949c0a938d57563bd', + 'type' => 'file', + 'name' => 'file1.jpg', + 'filePath' => '/images/products/file1.jpg', + 'tags' => ['t-shirt', 'round-neck', 'sale2019'], + 'isPrivateFile' => false, + 'isPublished' => true, + 'customCoordinates' => null, + 'url' => 'https://ik.imagekit.io/your_imagekit_id/images/products/file1.jpg', + 'thumbnail' => 'https://ik.imagekit.io/your_imagekit_id/tr:n-media_library_thumbnail/images/products/file1.jpg', + 'fileType' => 'image' + ]; + + $mockBodyResponse = Utils::streamFor(json_encode($responseBody)); + + $mock = new MockHandler([ + new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse) + ]); + + $handlerStack = HandlerStack::create($mock); + + $container = []; + $history = Middleware::history($container); + + $handlerStack->push($history); + + $this->createMockClient($handlerStack); + + $response = $this->mockClient->updateFileDetails($fileId, $updateData); + + $request = $container[0]['request']; + $requestPath = $request->getUri()->getPath(); + $requestBody = $request->getBody(); + $stream = Utils::streamFor($requestBody)->getContents(); + + // Request Check + FileTest::assertEquals("/v1/files/{$fileId}/details",$requestPath); + FileTest::assertEquals($stream,json_encode($updateData)); + + // Response Check + FileTest::assertNull($response->error); + FileTest::assertEquals(json_encode($responseBody), json_encode($response->result)); + + // Assert Method + $requestMethod = $container[0]['request']->getMethod(); + FileTest::assertEquals($requestMethod,'PATCH'); + } + public function testUpdateFileDetailsWithInvalidTags() { $fileId = '5df36759adf3f523d81dd94f'; diff --git a/tests/ImageKit/Upload/UploadTest.php b/tests/ImageKit/Upload/UploadTest.php index 4e97c19a..8c8b1df9 100644 --- a/tests/ImageKit/Upload/UploadTest.php +++ b/tests/ImageKit/Upload/UploadTest.php @@ -172,6 +172,8 @@ public function testFileUploadIfSuccessful() ] ] ], + 'checks' => "'request.folder' : '/sample-folder'", + 'isPublished' => true ]; $mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj)); @@ -215,6 +217,8 @@ public function testFileUploadIfSuccessful() $this->checkFormData($stream,$boundary,"overwriteCustomMetadata","true"); $this->checkFormData($stream,$boundary,"customMetadata",json_encode($fileOptions['customMetadata'])); $this->checkFormData($stream,$boundary,"transformation",json_encode($fileOptions['transformation'])); + $this->checkFormData($stream,$boundary,"checks",$fileOptions['checks']); + $this->checkFormData($stream,$boundary,"isPublished","true"); // Assert Method $requestMethod = $container[0]['request']->getMethod(); @@ -785,7 +789,56 @@ public function testFileUploadWithInvalidTransformationTypePostTransformation() UploadTest::assertEquals(json_encode($error),json_encode($response->error)); } + + /** + * + */ + public function testFileUploadWithInvalidChecks() + { + $fileOptions = [ + 'file' => 'http://lorempixel.com/640/480/', + 'fileName' => 'test_file_name', + "useUniqueFileName" => true, // true|false + "responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields + 'checks' => true + ]; + + $error = [ + "message" => "The value provided for the checks parameter is invalid.", + "help" => "For support kindly contact us at support@imagekit.io ." + ]; + + $this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error))); + + $response = $this->client->uploadFile($fileOptions); + + // Request Body Check + UploadTest::assertEquals(json_encode($error),json_encode($response->error)); + } + public function testFileUploadWithInvalidPublishStatus() + { + $fileOptions = [ + 'file' => 'http://lorempixel.com/640/480/', + 'fileName' => 'test_file_name', + "useUniqueFileName" => true, // true|false + "responseFields" => implode(",", ["tags", "customMetadata"]), // Comma Separated, check docs for more responseFields + 'isPublished' => '' + ]; + + $error = [ + "message" => "isPublished must be boolean.", + "help" => "For support kindly contact us at support@imagekit.io ." + ]; + + $this->stubHttpClient(new Response(403, ['X-Foo' => 'Bar'], json_encode($error))); + + $response = $this->client->uploadFile($fileOptions); + + // Request Body Check + UploadTest::assertEquals(json_encode($error),json_encode($response->error)); + } + protected function setUp() { $this->client = new ImageKit(