Skip to content

Commit 1d63b7f

Browse files
author
Jagveer
committed
documentation and sample app
1 parent 7801514 commit 1d63b7f

File tree

2 files changed

+207
-35
lines changed

2 files changed

+207
-35
lines changed

README.md

Lines changed: 116 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,24 @@ The SDK provides a simple interface for all the [media APIs mentioned here](http
241241
Accepts an object specifying the parameters to be used to list and search files. All parameters specified in the [documentation here](https://docs.imagekit.io/api-reference/media-api/list-and-search-files) can be passed as is with the correct values to get the results.
242242

243243
```
244+
// Using Callback Function
245+
244246
imagekit.listFiles({
245247
skip : 10,
246248
limit : 10
247249
}, function(err, result) {
248-
250+
});
251+
252+
253+
// Using Promises
254+
255+
imagekit.listFiles({
256+
skip : 10,
257+
limit : 10
258+
}).then(response => {
259+
260+
}).catch(err => {
261+
249262
});
250263
```
251264

@@ -254,8 +267,20 @@ imagekit.listFiles({
254267
Accepts the file ID and fetches the details as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/get-file-details).
255268

256269
```
270+
// Using Callback Function
271+
257272
imagekit.getFileDetails("file_id", function(err, result) {
258273
274+
});
275+
276+
277+
// Using Promises
278+
279+
imagekit.getFileDetails("file_id")
280+
.then(response => {
281+
282+
}).catch(err => {
283+
259284
});
260285
```
261286

@@ -264,8 +289,20 @@ imagekit.getFileDetails("file_id", function(err, result) {
264289
Accepts the file ID and fetches the metadata as per the [API documentation here](https://docs.imagekit.io/api-reference/metadata-api/get-image-metadata-for-uploaded-media-files).
265290

266291
```
292+
// Using Callback Function
293+
267294
imagekit.getFileMetadata("file_id", function(err, result) {
268295
296+
});
297+
298+
299+
// Using Promises
300+
301+
imagekit.getFileMetadata("file_id")
302+
.then(response => {
303+
304+
}).catch(err => {
305+
269306
});
270307
```
271308

@@ -274,11 +311,26 @@ imagekit.getFileMetadata("file_id", function(err, result) {
274311
Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/update-file-details). The first argument to the `updateFileDetails` method is the file ID and the second argument is an object with the parameters to be updated.
275312

276313
```
314+
// Using Callback Function
315+
277316
imagekit.updateFileDetails("file_id", {
278317
tags : ['image_tag'],
279318
customCoordinates : "10,10,100,100"
280319
}, function(err, result) {
281320
321+
});
322+
323+
324+
// Using Promises
325+
326+
imagekit.updateFileDetails("file_id", {
327+
tags : ['image_tag'],
328+
customCoordinates : "10,10,100,100"
329+
})
330+
.then(response => {
331+
332+
}).catch(err => {
333+
282334
});
283335
```
284336

@@ -287,28 +339,88 @@ imagekit.updateFileDetails("file_id", {
287339
Delete a file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-file). The method accepts the file ID of the file that has to be deleted.
288340

289341
```
342+
343+
// Using Callback Function
344+
290345
imagekit.deleteFile("file_id", function(err, result) {
291346
292347
});
348+
349+
350+
// Using Promises
351+
352+
imagekit.deleteFile("file_id"})
353+
.then(response => {
354+
355+
}).catch(err => {
356+
357+
});
358+
```
359+
360+
**6. Bulk Delete Files**
361+
362+
Delete multiple file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-files-bulk). The method accepts an array of file IDs of the files that have to be deleted.
363+
293364
```
294365
295-
**6. Purge Cache**
366+
// Using Callback Function
367+
368+
imagekit.bulkDeleteFiles(["fileIds"], function(err, result) {
369+
370+
});
371+
372+
373+
// Using Promises
374+
375+
imagekit.bulkDeleteFiles(["fileIds"])
376+
.then(response => {
377+
378+
}).catch(err => {
379+
380+
});
381+
```
382+
383+
**7. Purge Cache**
296384

297385
Programmatically issue a cache clear request as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache). Accepts the full URL of the file for which the cache has to be cleared.
298386

299387
```
300-
imagekit.purgeCache("full_url", function(err, result) {
388+
// Using Callback Function
389+
390+
imagekit.purgeCache("full_url", function(err, result) {
301391
392+
});
393+
394+
395+
// Using Promises
396+
397+
imagekit.purgeCache("full_url")
398+
.then(response => {
399+
400+
}).catch(err => {
401+
302402
});
303403
```
304404

305-
**7. Purge Cache Status**
405+
**8. Purge Cache Status**
306406

307407
Get the purge cache request status using the request ID returned when a purge cache request gets submitted as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/purge-cache-status)
308408

309409
```
410+
// Using Callback Function
411+
310412
imagekit.getPurgeCacheStatus("cache_request_id", function(err, result) {
311413
414+
});
415+
416+
417+
// Using Promises
418+
419+
imagekit.getPurgeCacheStatus("cache_request_id")
420+
.then(response => {
421+
422+
}).catch(err => {
423+
312424
});
313425
```
314426

demo/index.js

Lines changed: 91 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,100 @@
1-
var ImageKit = require("../index");
1+
const ImageKit = require("../index");
2+
const fs = require('fs');
23

3-
var imagekit = new ImageKit({
4+
const imagekit = new ImageKit({
45
publicKey : "your_public_api_key",
56
privateKey : "your_private_api_key",
67
urlEndpoint : "https://ik.imagekit.io/your_imagekit_id/"
78
})
89

9-
var imageURL = imagekit.url({
10-
path : "/default-image.jpg",
11-
queryParameters : {
12-
v : "1",
13-
q : "something"
14-
},
15-
transformation : [{
16-
"HEIGHT" : "300",
17-
"WIDTH" : "400",
18-
"EFFECT_GRAY" : "-",
19-
"PROGRESSIVE" : "true",
20-
"EFFECT_SHARPEN" : "2",
21-
"EFFECT_CONTRAST" : "-"
22-
}, {
23-
HEIGHT : "500",
24-
"EFFECT_SHARPEN" : "1",
25-
"EFFECT_USM" : "2_2_0.1_0.008"
26-
}],
27-
signed : true,
28-
expireSeconds : 10
10+
//Uploading an image at path /Pictures/test_image_1.jpg
11+
let filePath = "./test_image_1.jpg";
12+
let fileName = "test_image_1a"
13+
let uploadResponse;
14+
let file = fs.createReadStream(filePath);
15+
16+
imagekit.upload({file, fileName})
17+
.then(response => uploadResponse)
18+
.catch(err => console.error("Error recieved:", err));
19+
20+
21+
//URL generation
22+
let {filePath} = uploadResponse;
23+
let imageUrl = imagekit.url({
24+
path: filePath,
25+
transformation: [{
26+
height: 400,
27+
width: 300
28+
}]
29+
});
30+
31+
//List Files
32+
let imagesList;
33+
34+
imagekit.listFiles({limit: 10})
35+
.then(response => imagesList = response)
36+
.catch(err => console.error("Error recieved:", err));
37+
38+
//Get File Details
39+
let {fileId} = imagesList[0];
40+
let fileDetails;
41+
imagekit.getFileDetails(fileId)
42+
.then(response => fileDetails = response)
43+
.catch(err => console.error("Error recieved:", err));
44+
45+
//Get File Metadata
46+
let fileMetadata;
47+
imagekit.getFileMetadata(fileId)
48+
.then(response => fileMetadata = response)
49+
.catch(err => console.error("Error recieved:", err));
50+
51+
//Update File Details
52+
imagekit.updateFileDetails(fileId, {
53+
tags: ['tag1', 'tag2'],
54+
customCoordinates : "10,10,100,1ima00"
55+
}).then(response => fileDetails = response)
56+
.catch(err => console.error("Error recieved:", err));
57+
58+
//purge Cache and purge cache status
59+
let urlToPurge = imagekit.url({
60+
path: filePath
2961
});
62+
let purgeResponse;
63+
imagekit.purgeCache(urlToPurge)
64+
.then(response => purgeResponse = response)
65+
.catch(err => console.error("Error recieved:", err));
66+
imagekit.getPurgeCacheStatus(purgeResponse.requestId).then(console.log).catch(console.log);
67+
68+
//Distance calculation between two pHash values
69+
let fileId_1 = imagesList[0].fileId, fileId_2 = imagesList[1].fileId;
70+
let pHash_1, pHash_2;
71+
72+
imagekit.getFileMetadata(fileId_1)
73+
.then(response => pHash_1 = response.pHash)
74+
.catch(err => console.error("Error recieved:", err));
75+
76+
imagekit.getFileMetadata(fileId_2)
77+
.then(response => pHash_2 = response.pHash)
78+
.catch(err => console.error("Error recieved:", err));
79+
80+
let pHashDistance = imagekit.pHashDistance(pHash_1, pHash_2);
81+
82+
// Delete Files
83+
84+
let deleteResponse, bulkDeleteResponse;
85+
86+
imagekit.deleteFile(fileId_1)
87+
.then(response => deleteResponse = response)
88+
.catch(err => console.error("Error recieved:", err));
89+
90+
91+
let fileIds = imagesList.map(image => image.fileId);
92+
fileIds.shift();
93+
imagekit.bulkDeleteFiles(fileIds)
94+
.then(response => bulkDeleteResponse = response)
95+
.catch(err => console.error("Error recieved:", err));
96+
3097

31-
var authenticationParameters = imagekit.getAuthenticationParameters("your_token");
3298

33-
imagekit.purgeCache("full_url", function(err, result) { /*console.log(arguments);*/ });
34-
imagekit.getPurgeCacheStatus("cache_request_id", function(err, result) { /*console.log(arguments);*/ });
35-
imagekit.deleteFile("file_id", function(err, result) { /*console.log(arguments);*/ });
36-
imagekit.listFiles({}, function(err, result) { /* console.log(arguments) */ });
37-
imagekit.getFileDetails("file_id", function(err, result) { /*console.log(arguments);*/ });
38-
imagekit.getFileMetadata("file_id", function(err, result) { /*console.log(arguments);*/ });
39-
imagekit.updateFileDetails("file_id", { tags : ['image_tag'] }, function(err, result) { /*console.log(arguments);*/ });
40-
imagekit.upload({file : "your_file", fileName : "your_file_name.jpg"}, function(err, result) { /*console.log(arguments);*/ });
99+
//Generating Authentication token
100+
var authenticationParameters = imagekit.getAuthenticationParameters("your_token");

0 commit comments

Comments
 (0)