Skip to content

Commit

Permalink
Merge pull request #67 from imagekit-developer/IK-1499
Browse files Browse the repository at this point in the history
Added checks and isPublished parameter for uplaod
ahnv authored Sep 5, 2024
2 parents 4f2194f + 1244cc6 commit b3ae1b6
Showing 5 changed files with 149 additions and 2 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 2 additions & 0 deletions src/ImageKit/Constants/ErrorMessages.php
Original file line number Diff line number Diff line change
@@ -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 [email protected] ."];
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] .'];
8 changes: 6 additions & 2 deletions src/ImageKit/ImageKit.php
Original file line number Diff line number Diff line change
@@ -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);
}
63 changes: 63 additions & 0 deletions tests/ImageKit/Manage/FileTest.php
Original file line number Diff line number Diff line change
@@ -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';
53 changes: 53 additions & 0 deletions tests/ImageKit/Upload/UploadTest.php
Original file line number Diff line number Diff line change
@@ -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 [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));
}

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(

0 comments on commit b3ae1b6

Please sign in to comment.