Skip to content

Commit

Permalink
added checks parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
ANKUR DWIVEDI authored and ANKUR DWIVEDI committed Jul 26, 2024
1 parent 4f2194f commit 3e84921
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
]);
```

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -657,6 +659,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.
// "customMetadata" => [
// "SKU" => "VS882HJ2JD",
// "price" => 599.99,
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 @@ -49,6 +49,7 @@ 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 $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
3 changes: 3 additions & 0 deletions src/ImageKit/ImageKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ 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));
}
}


Expand Down
28 changes: 28 additions & 0 deletions tests/ImageKit/Upload/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public function testFileUploadIfSuccessful()
]
]
],
'checks' => "'request.folder' : '/sample-folder'"
];

$mockBodyResponse = Utils::streamFor(json_encode($this->uploadSuccessResponseObj));
Expand Down Expand Up @@ -215,6 +216,7 @@ 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']);

// Assert Method
$requestMethod = $container[0]['request']->getMethod();
Expand Down Expand Up @@ -785,6 +787,32 @@ 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));
}

protected function setUp()
{
Expand Down

0 comments on commit 3e84921

Please sign in to comment.