Skip to content

Commit

Permalink
added publish
Browse files Browse the repository at this point in the history
  • Loading branch information
ANKUR DWIVEDI authored and ANKUR DWIVEDI committed Aug 23, 2024
1 parent 3e84921 commit 6313b8e
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 6 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ $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,
Expand Down Expand Up @@ -776,6 +777,27 @@ $updateFileDetails = $imageKit->updateFileDetails(
);
```

**Update publish status**

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.`.

#### 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.
Expand Down
1 change: 1 addition & 0 deletions src/ImageKit/Constants/ErrorMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ErrorMessages
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PRE_TRANSFORMATION = [ "message" => "Invalid pre transformation parameter.", "help" => "For support kindly contact us at [email protected] ."];
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION = [ "message" => "Invalid post transformation parameter.", "help" => "For support kindly contact us at [email protected] ."];
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] ."];
public static $UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_PUBLISH_STATUS = [ "message" => "isPublished must be boolean.", "help" => "For support kindly contact us at [email protected] ."];
public static $MISSING_UPLOAD_DATA = ['message' => 'Missing data for upload', 'help' => 'For support kindly contact us at [email protected] .'];
public static $MISSING_UPLOAD_FILE_PARAMETER = ['message' => 'Missing file parameter for upload', 'help' => 'For support kindly contact us at [email protected] .'];
public static $MISSING_UPLOAD_FILENAME_PARAMETER = ['message' => 'Missing fileName parameter for upload', 'help' => 'For support kindly contact us at [email protected] .'];
Expand Down
11 changes: 6 additions & 5 deletions src/ImageKit/ImageKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ public function uploadFile($options=null)
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_POST_TRANSFORMATION));
}
}
if(isset($options['checks']) && !is_string($options['checks'])){
return Response::respond(true, ((object)ErrorMessages::$UPLOAD_FILE_PARAMETER_OPTIONS_INVALID_CHECKS));
}
}


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);
}
Expand Down
63 changes: 63 additions & 0 deletions tests/ImageKit/Manage/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
27 changes: 26 additions & 1 deletion tests/ImageKit/Upload/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public function testFileUploadIfSuccessful()
]
]
],
'checks' => "'request.folder' : '/sample-folder'"
'checks' => "'request.folder' : '/sample-folder'",
'isPublished' => true
];

$mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj));
Expand Down Expand Up @@ -217,6 +218,7 @@ public function testFileUploadIfSuccessful()
$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();
Expand Down Expand Up @@ -814,6 +816,29 @@ public function testFileUploadWithInvalidChecks()
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 [email protected] ."
];

$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(
Expand Down

0 comments on commit 6313b8e

Please sign in to comment.