Skip to content

Commit 6313b8e

Browse files
ANKUR DWIVEDIANKUR DWIVEDI
authored andcommitted
added publish
1 parent 3e84921 commit 6313b8e

File tree

5 files changed

+118
-6
lines changed

5 files changed

+118
-6
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ $uploadFile = $imageKit->uploadFile([
660660
]
661661
],
662662
'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,
663664
// "customMetadata" => [
664665
// "SKU" => "VS882HJ2JD",
665666
// "price" => 599.99,
@@ -776,6 +777,27 @@ $updateFileDetails = $imageKit->updateFileDetails(
776777
);
777778
```
778779

780+
**Update publish status**
781+
782+
If any parameter other than `publish` is included in the update options, 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+
779801
### 6. Add Tags (Bulk) API
780802

781803
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class ErrorMessages
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] ."];
5252
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] ."];
5354
public static $MISSING_UPLOAD_DATA = ['message' => 'Missing data for upload', 'help' => 'For support kindly contact us at [email protected] .'];
5455
public static $MISSING_UPLOAD_FILE_PARAMETER = ['message' => 'Missing file parameter for upload', 'help' => 'For support kindly contact us at [email protected] .'];
5556
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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,13 @@ public function uploadFile($options=null)
216216
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION));
217217
}
218218
}
219-
if(isset($options['checks']) && !is_string($options['checks'])){
220-
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS));
221-
}
222219
}
223-
224-
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+
}
225226
$this->httpClient->setUri(Endpoints::getUploadFileEndpoint());
226227
return Upload::upload($options, $this->httpClient);
227228
}

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: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ public function testFileUploadIfSuccessful()
172172
]
173173
]
174174
],
175-
'checks' => "'request.folder' : '/sample-folder'"
175+
'checks' => "'request.folder' : '/sample-folder'",
176+
'isPublished' => true
176177
];
177178

178179
$mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj));
@@ -217,6 +218,7 @@ public function testFileUploadIfSuccessful()
217218
$this->checkFormData($stream,$boundary,"customMetadata",json_encode($fileOptions['customMetadata']));
218219
$this->checkFormData($stream,$boundary,"transformation",json_encode($fileOptions['transformation']));
219220
$this->checkFormData($stream,$boundary,"checks",$fileOptions['checks']);
221+
$this->checkFormData($stream,$boundary,"isPublished","true");
220222

221223
// Assert Method
222224
$requestMethod = $container[0]['request']->getMethod();
@@ -814,6 +816,29 @@ public function testFileUploadWithInvalidChecks()
814816
UploadTest::assertEquals(json_encode($error),json_encode($response->error));
815817
}
816818

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+
817842
protected function setUp()
818843
{
819844
$this->client = new ImageKit(

0 commit comments

Comments
 (0)