Skip to content

Commit b1b9731

Browse files
authored
Merge pull request #42 from msheheryar1/dev
Dev
2 parents 55699f7 + 9ed1d4f commit b1b9731

File tree

12 files changed

+1477
-206
lines changed

12 files changed

+1477
-206
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ImageKit is complete media storage, optimization, and transformation solution th
1919
* [File Upload](#file-upload)
2020
- [Demo Application](#demo-application)
2121
- [URL Generation](#url-generation-1)
22-
- [Signed URL & Image Transformations](#applying-chained-transformations-common-image-manipulations-signed-url--conditional-transformation)
22+
- [Signed URL & Image Transformations](#applying-chained-transformations-common-image-manipulations--signed-url)
2323
- [Server-side File Upload](#server-side-file-upload)
2424
- [File Management](#file-management)
2525
- [Custom Metadata Fields API](#custom-metadata-fields-api)
@@ -787,20 +787,20 @@ $deleteFiles = $imageKit->bulkFileDeleteByIds($fileIds);
787787

788788
This will copy a file from one folder to another.
789789

790-
> If any file at the destination has the same name as the source file, then the source file and its versions (if `includeVersions` is set to true) will be appended to the destination file version history.
790+
> If any file at the destination has the same name as the source file, then the source file and its versions (if `includeFileVersions` is set to true) will be appended to the destination file version history.
791791
792792
Refer to the [copy file API](https://docs.imagekit.io/api-reference/media-api/copy-file) for a better understanding of the **request & response structure**.
793793

794794
#### Basic Usage
795795
```php
796796
$sourceFilePath = '/sample-folder1/sample-file.jpg';
797797
$destinationPath = '/sample-folder2/';
798-
$includeVersions = false;
798+
$includeFileVersions = false;
799799

800800
$copyFile = $imageKit->copy([
801801
'sourceFilePath' => $sourceFilePath,
802802
'destinationPath' => $destinationPath,
803-
'includeVersions' => $includeVersions
803+
'includeFileVersions' => $includeFileVersions
804804
]);
805805
```
806806

@@ -906,11 +906,11 @@ Refer to the [copy folder API](https://docs.imagekit.io/api-reference/media-api/
906906
```php
907907
$sourceFolderPath = '/source-folder/';
908908
$destinationPath = '/destination-folder/';
909-
$includeVersions = false;
909+
$includeFileVersions = false;
910910
$copyFolder = $imageKit->copyFolder([
911911
'sourceFolderPath' => $sourceFolderPath,
912912
'destinationPath' => $destinationPath,
913-
'includeVersions' => $includeVersions
913+
'includeFileVersions' => $includeFileVersions
914914
]);
915915
```
916916

sample/file_management/index.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"fileType" => "all",
2929
"limit" => 10,
3030
"skip" => 0,
31+
"tags" => implode(",",["tag3","tag4"]),
3132
]);
3233

3334
echo "\n\n";
@@ -127,7 +128,7 @@
127128
$copyFile = $imageKit->copy([
128129
'sourceFilePath' => $sourceFilePath,
129130
'destinationPath' => $destinationPath,
130-
'includeVersions' => false
131+
'includeFileVersions' => false
131132
]);
132133

133134
echo "\n\n";
@@ -205,11 +206,11 @@
205206

206207
$sourceFolderPath = $folderName;
207208
$destinationPath = '/sample-folder';
208-
$includeVersions = false;
209+
$includeFileVersions = false;
209210
$copyFolder = $imageKit->copyFolder([
210211
'sourceFolderPath' => $sourceFolderPath,
211212
'destinationPath' => $destinationPath,
212-
'includeVersions' => $includeVersions
213+
'includeFileVersions' => $includeFileVersions
213214
]);
214215

215216
echo "\n\n";

src/ImageKit/Constants/ErrorMessages.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ErrorMessages
7777
'For support kindly contact us at [email protected] .'];
7878
public static $COPY_FILE_PARAMETER_NON_ARRAY = ['message' => 'Copy File API accepts an array of parameters, non array value passed', 'help' => 'For support kindly contact us at [email protected] .'];
7979
public static $COPY_FILE_PARAMETER_EMPTY_ARRAY = ['message' => 'Copy File API accepts an array of parameters, empty array passed', 'help' => 'For support kindly contact us at [email protected] .'];
80-
public static $COPY_FILE_DATA_INVALID = ['message' => 'Missing parameter sourceFilePath and/or destinationPath and/or includeVersions for Copy File API', 'help' =>
80+
public static $COPY_FILE_DATA_INVALID = ['message' => 'Missing parameter sourceFilePath and/or destinationPath for Copy File API', 'help' =>
8181
'For support kindly contact us at [email protected] .'];
8282
public static $MOVE_FILE_PARAMETER_MISSING = ['message' => 'Move File API accepts an array, null passed', 'help' =>
8383
'For support kindly contact us at [email protected] .'];
@@ -109,7 +109,7 @@ class ErrorMessages
109109
'For support kindly contact us at [email protected] .'];
110110
public static $COPY_FOLDER_PARAMETER_NON_ARRAY = ['message' => 'Copy Folder API accepts an array of parameters, non array value passed', 'help' => 'For support kindly contact us at [email protected] .'];
111111
public static $COPY_FOLDER_PARAMETER_EMPTY_ARRAY = ['message' => 'Copy Folder API accepts an array of parameters, empty array passed', 'help' => 'For support kindly contact us at [email protected] .'];
112-
public static $COPY_FOLDER_DATA_INVALID = ['message' => 'Missing parameter sourceFolderPath and/or destinationPath and/or includeVersions for Copy Folder API', 'help' =>
112+
public static $COPY_FOLDER_DATA_INVALID = ['message' => 'Missing parameter sourceFolderPath and/or destinationPath for Copy Folder API', 'help' =>
113113
'For support kindly contact us at [email protected] .'];
114114
public static $MOVE_FOLDER_PARAMETER_MISSING = ['message' => 'Move Folder API accepts an array, null passed', 'help' =>
115115
'For support kindly contact us at [email protected] .'];

src/ImageKit/ImageKit.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ImageKit
5959
* @return void
6060
*/
6161
public function __construct($publicKey, $privateKey, $urlEndpoint, $transformationPosition =
62-
Transformation::DEFAULT_TRANSFORMATION_POSITION)
62+
Transformation::DEFAULT_TRANSFORMATION_POSITION, $handlerStack=null)
6363
{
6464
$this->configuration = new Configuration();
6565
if ($publicKey == null || empty($publicKey)) {
@@ -90,8 +90,7 @@ public function __construct($publicKey, $privateKey, $urlEndpoint, $transformati
9090
}
9191
$this->configuration->transformationPosition = $transformationPosition;
9292

93-
94-
$client = new Client(Authorization::addAuthorization($this->configuration));
93+
$client = new Client(Authorization::addAuthorization($this->configuration,$handlerStack));
9594
$this->httpClient = new GuzzleHttpWrapper($client);
9695
}
9796

@@ -687,7 +686,7 @@ public function bulkDeleteFiles($fileIds=null)
687686
*
688687
* @link https://docs.imagekit.io/api-reference/media-api/copy-file
689688
*
690-
* @param $parameter['sourceFilePath','destinationPath','includeVersions']
689+
* @param $parameter['sourceFilePath','destinationPath','includeFileVersions']
691690
* @return Response
692691
*
693692
*/
@@ -702,12 +701,12 @@ public function copy($parameter=null)
702701
if(sizeof($parameter)==0){
703702
return Response::respond(true, ((object)ErrorMessages::$COPY_FILE_PARAMETER_EMPTY_ARRAY));
704703
}
705-
if (empty($parameter['sourceFilePath']) || empty($parameter['destinationPath']) || !isset($parameter['includeVersions'])) {
704+
if (empty($parameter['sourceFilePath']) || empty($parameter['destinationPath'])) {
706705
return Response::respond(true, ((object)ErrorMessages::$COPY_FILE_DATA_INVALID));
707706
}
708707

709708
$this->httpClient->setUri(Endpoints::getCopyFileEndpoint());
710-
return Manage\File::copy($parameter['sourceFilePath'], $parameter['destinationPath'], $parameter['includeVersions'], $this->httpClient);
709+
return Manage\File::copy($parameter['sourceFilePath'], $parameter['destinationPath'], $parameter['includeFileVersions']??false, $this->httpClient);
711710
}
712711

713712

@@ -719,21 +718,21 @@ public function copy($parameter=null)
719718
*
720719
* @param $sourceFilePath
721720
* @param $destinationPath
722-
* @param $includeVersions
721+
* @param $includeFileVersions
723722
* @return Response
724723
*
725724
* @deprecated since 3.0.0, use <code>copy</code>
726725
*
727726
*/
728-
public function copyFile($sourceFilePath=null, $destinationPath=null, $includeVersions=null)
727+
public function copyFile($sourceFilePath=null, $destinationPath=null, $includeFileVersions=null)
729728
{
730729

731730
if (empty($sourceFilePath) || empty($destinationPath)) {
732731
return Response::respond(true, ((object)ErrorMessages::$COPY_FILE_DATA_INVALID));
733732
}
734733

735734
$this->httpClient->setUri(Endpoints::getCopyFileEndpoint());
736-
return Manage\File::copy($sourceFilePath, $destinationPath, $includeVersions, $this->httpClient);
735+
return Manage\File::copy($sourceFilePath, $destinationPath, $includeFileVersions, $this->httpClient);
737736
}
738737

739738
/**
@@ -928,7 +927,7 @@ public function deleteFolder($folderPath=null)
928927
*
929928
* @link https://docs.imagekit.io/api-reference/media-api/copy-folder
930929
*
931-
* @param $parameter[$sourceFolderPath, $destinationPath, includeVersions]
930+
* @param $parameter[$sourceFolderPath, $destinationPath, includeFileVersions]
932931
*
933932
* @return Response
934933
*/
@@ -943,12 +942,12 @@ public function copyFolder($parameter=null)
943942
if(sizeof($parameter)==0){
944943
return Response::respond(true, ((object)ErrorMessages::$COPY_FOLDER_PARAMETER_EMPTY_ARRAY));
945944
}
946-
if (empty($parameter['sourceFolderPath']) || empty($parameter['destinationPath']) || !isset($parameter['includeVersions'])) {
945+
if (empty($parameter['sourceFolderPath']) || empty($parameter['destinationPath'])) {
947946
return Response::respond(true, ((object)ErrorMessages::$COPY_FOLDER_DATA_INVALID));
948947
}
949948

950949
$this->httpClient->setUri(Endpoints::getCopyFolderEndpoint());
951-
return Manage\Folder::copy($parameter['sourceFolderPath'], $parameter['destinationPath'], $parameter['includeVersions'], $this->httpClient);
950+
return Manage\Folder::copy($parameter['sourceFolderPath'], $parameter['destinationPath'], $parameter['includeFileVersions']??false, $this->httpClient);
952951
}
953952

954953
/**

src/ImageKit/Manage/File.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class File
2323
public static function listFile(GuzzleHttpWrapper $resource,$parameters=null)
2424
{
2525
if($parameters){
26+
if (isset($parameters['tags']) && is_array($parameters['tags'])) {
27+
$parameters['tags'] = implode(',', $parameters['tags']);
28+
}
2629
$resource->setDatas($parameters);
2730
}
2831
try {
@@ -273,9 +276,9 @@ public static function bulkDeleteByFileIds($fileIds, GuzzleHttpWrapper $resource
273276
*
274277
* @return Response
275278
*/
276-
public static function copy($sourceFilePath, $destinationPath, $includeVersions, GuzzleHttpWrapper $resource)
279+
public static function copy($sourceFilePath, $destinationPath, $includeFileVersions, GuzzleHttpWrapper $resource)
277280
{
278-
$resource->setDatas(['sourceFilePath' => $sourceFilePath, 'destinationPath' => $destinationPath, 'includeVersions' => $includeVersions]);
281+
$resource->setDatas(['sourceFilePath' => $sourceFilePath, 'destinationPath' => $destinationPath, 'includeFileVersions' => $includeFileVersions]);
279282
try {
280283
$res = $resource->post();
281284
} catch (\Throwable $th) {
@@ -539,12 +542,12 @@ public static function updateDetails($fileId, $updateData, GuzzleHttpWrapper $re
539542
{
540543
$obj = (object)$updateData;
541544

542-
if (isset($obj->tags) && ($obj->tags !== null) && ($obj->tags !== 'undefined') && !is_array($obj->tags)) {
543-
return Response::respond(true, ((object)ErrorMessages::$UPDATE_DATA_TAGS_INVALID));
545+
if (isset($obj->tags) && is_string($obj->tags)) {
546+
$updateData['tags'] = explode(',', $obj->tags);
544547
}
545548

546-
if (isset($obj->customCoordinates) && ($obj->customCoordinates !== 'undefined') && is_array($obj->customCoordinates)) {
547-
return Response::respond(true, ((object)ErrorMessages::$UPDATE_DATA_COORDS_INVALID));
549+
if (isset($obj->customCoordinates) && is_array($obj->customCoordinates)) {
550+
$updateData['customCoordinates'] = implode(',', $obj->customCoordinates);
548551
}
549552

550553
$resource->setDatas($updateData);

src/ImageKit/Manage/Folder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ public static function delete($folderPath, GuzzleHttpWrapper $httpClient)
9090
*
9191
* @return Response
9292
*/
93-
public static function copy($sourceFolderPath, $destinationPath, $includeVersions, GuzzleHttpWrapper $httpClient)
93+
public static function copy($sourceFolderPath, $destinationPath, $includeFileVersions, GuzzleHttpWrapper $httpClient)
9494
{
95-
$httpClient->setDatas(['sourceFolderPath' => $sourceFolderPath, 'destinationPath' => $destinationPath, 'includeVersions' => $includeVersions]);
95+
$httpClient->setDatas(['sourceFolderPath' => $sourceFolderPath, 'destinationPath' => $destinationPath, 'includeFileVersions' => $includeFileVersions]);
9696
try {
9797
$res = $httpClient->post();
9898
} catch (\Throwable $th) {

src/ImageKit/Utils/Authorization.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ class Authorization
1313
* @param Configuration $configuration
1414
* @return array[]
1515
*/
16-
public static function addAuthorization(Configuration $configuration)
16+
public static function addAuthorization(Configuration $configuration,$handlerStack=null)
1717
{
1818
return [
1919
'auth' => [
2020
$configuration->privateKey, ''
21-
]
21+
],
22+
'handler'=>$handlerStack
2223
];
2324
}
2425
}

tests/ImageKit/Manage/CacheTest.php

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use GuzzleHttp\Psr7\Response;
66
use GuzzleHttp\Psr7\Utils;
77
use ImageKit\ImageKit;
8+
use ImageKit\Utils\Transformation;
89
use ImageKit\Manage\Cache;
910
use ImageKit\Resource\GuzzleHttpWrapper;
11+
use GuzzleHttp\Handler\MockHandler;
1012
use PHPUnit\Framework\TestCase;
13+
use GuzzleHttp\Client;
14+
use GuzzleHttp\HandlerStack;
15+
use GuzzleHttp\Middleware;
1116

1217
class CacheTest extends TestCase
1318
{
@@ -28,10 +33,31 @@ public function testPurgeCache()
2833
];
2934
$mockBodyResponse = Utils::streamFor(json_encode($responseBody));
3035

31-
$this->stubHttpClient('post', new Response(201, ['X-Foo' => 'Bar'], $mockBodyResponse));
36+
$mock = new MockHandler([
37+
new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse)
38+
]);
3239

33-
$response = $this->client->purgeCache($image_url);
40+
$handlerStack = HandlerStack::create($mock);
41+
42+
$container = [];
43+
$history = Middleware::history($container);
44+
45+
$handlerStack->push($history);
46+
47+
$this->createMockClient($handlerStack);
48+
49+
$response = $this->mockClient->purgeCache($image_url);
50+
51+
$request = $container[0]['request'];
52+
$requestPath = $request->getUri()->getPath();
53+
$stream = Utils::streamFor($request->getBody())->getContents();
54+
$stream = json_decode($stream,true);
3455

56+
// Request Check
57+
CacheTest::assertEquals("/v1/files/purge",$requestPath);
58+
CacheTest::assertEquals($stream['url'],$image_url);
59+
60+
// Response Check
3561
CacheTest::assertEquals(json_encode($responseBody), json_encode($response->result));
3662
}
3763

@@ -72,10 +98,32 @@ public function testPurgeCacheStatus()
7298

7399
$mockBodyResponse = Utils::streamFor(json_encode($responseBody));
74100

75-
$this->stubHttpClient('get', new Response(201, ['X-Foo' => 'Bar'], $mockBodyResponse));
101+
$mock = new MockHandler([
102+
new Response(200, ['X-Foo' => 'Bar'], $mockBodyResponse)
103+
]);
76104

77-
$response = $this->client->purgeCacheStatus($cacheRequestId);
105+
$handlerStack = HandlerStack::create($mock);
106+
107+
$container = [];
108+
$history = Middleware::history($container);
109+
110+
$handlerStack->push($history);
111+
112+
$this->createMockClient($handlerStack);
113+
114+
$response = $this->mockClient->purgeCacheStatus($cacheRequestId);
78115

116+
$request = $container[0]['request'];
117+
$requestPath = $request->getUri()->getPath();
118+
$stream = Utils::streamFor($request->getBody())->getContents();
119+
$stream = json_decode($stream,true);
120+
121+
// Request Check
122+
CacheTest::assertEquals("/v1/files/purge/{$cacheRequestId}",$requestPath);
123+
CacheTest::assertEmpty($stream);
124+
125+
126+
// Response Check
79127
CacheTest::assertEquals(json_encode($responseBody), json_encode($response->result));
80128
}
81129

@@ -116,6 +164,20 @@ protected function setUp(): void
116164

117165
}
118166

167+
/**
168+
*
169+
*/
170+
private function createMockClient($handler){
171+
$this->mockClient = new ImageKit(
172+
'testing_public_key',
173+
'testing_private_key',
174+
'https://ik.imagekit.io/demo',
175+
Transformation::DEFAULT_TRANSFORMATION_POSITION,
176+
$handler
177+
);
178+
}
179+
180+
119181
protected function tearDown(): void
120182
{
121183
$this->client = null;

0 commit comments

Comments
 (0)