Skip to content

Commit e9cefcb

Browse files
author
Jagveer
committed
Added sample app. Promisification. Bulk Delete API. Sample App.
2 parents 7c9262c + 3b89172 commit e9cefcb

File tree

10 files changed

+347
-56
lines changed

10 files changed

+347
-56
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/*
2+
.vscode

README.md

Lines changed: 149 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,25 @@ The `upload()` method requires at least the `file` and the `fileName` parameter
219219

220220
Sample usage
221221
```
222+
// Using Callback Function
223+
222224
imagekit.upload({
223-
file : <url|base_64|binary>, //required
224-
fileName : "my_file_name.jpg", //required
225+
file : <url|base_64|binary>, //required
226+
fileName : "my_file_name.jpg", //required
225227
}, function(error, result) {
228+
if(error) console.log(error);
229+
else console.log(result);
230+
});
226231
232+
// Using Promises
233+
234+
imagekit.upload({
235+
file : <url|base_64|binary>, //required
236+
fileName : "my_file_name.jpg", //required
237+
}).then(response => {
238+
console.log(response);
239+
}).catch(error => {
240+
console.log(error);
227241
});
228242
```
229243

@@ -241,11 +255,26 @@ The SDK provides a simple interface for all the [media APIs mentioned here](http
241255
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.
242256

243257
```
258+
// Using Callback Function
259+
260+
imagekit.listFiles({
261+
skip : 10,
262+
limit : 10
263+
}, function(error, result) {
264+
if(error) console.log(error);
265+
else console.log(result);
266+
});
267+
268+
269+
// Using Promises
270+
244271
imagekit.listFiles({
245272
skip : 10,
246273
limit : 10
247-
}, function(err, result) {
248-
274+
}).then(response => {
275+
console.log(response);
276+
}).catch(error => {
277+
console.log(error);
249278
});
250279
```
251280

@@ -254,8 +283,21 @@ imagekit.listFiles({
254283
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).
255284

256285
```
257-
imagekit.getFileDetails("file_id", function(err, result) {
286+
// Using Callback Function
287+
288+
imagekit.getFileDetails("file_id", function(error, result) {
289+
if(error) console.log(error);
290+
else console.log(result);
291+
});
292+
258293
294+
// Using Promises
295+
296+
imagekit.getFileDetails("file_id")
297+
}).then(response => {
298+
console.log(response);
299+
}).catch(error => {
300+
console.log(error);
259301
});
260302
```
261303

@@ -264,8 +306,21 @@ imagekit.getFileDetails("file_id", function(err, result) {
264306
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).
265307

266308
```
267-
imagekit.getFileMetadata("file_id", function(err, result) {
309+
// Using Callback Function
310+
311+
imagekit.getFileMetadata("file_id", function(error, result) {
312+
if(error) console.log(error);
313+
else console.log(result);
314+
});
315+
316+
317+
// Using Promises
268318
319+
imagekit.getFileMetadata("file_id")
320+
}).then(response => {
321+
console.log(response);
322+
}).catch(error => {
323+
console.log(error);
269324
});
270325
```
271326

@@ -274,11 +329,26 @@ imagekit.getFileMetadata("file_id", function(err, result) {
274329
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.
275330

276331
```
332+
// Using Callback Function
333+
277334
imagekit.updateFileDetails("file_id", {
278335
tags : ['image_tag'],
279336
customCoordinates : "10,10,100,100"
280-
}, function(err, result) {
337+
}, function(error, result) {
338+
if(error) console.log(error);
339+
else console.log(result);
340+
});
341+
281342
343+
// Using Promises
344+
345+
imagekit.updateFileDetails("file_id", {
346+
tags : ['image_tag'],
347+
customCoordinates : "10,10,100,100"
348+
}).then(response => {
349+
console.log(response);
350+
}).catch(error => {
351+
console.log(error);
282352
});
283353
```
284354

@@ -287,28 +357,92 @@ imagekit.updateFileDetails("file_id", {
287357
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.
288358

289359
```
290-
imagekit.deleteFile("file_id", function(err, result) {
291-
360+
361+
// Using Callback Function
362+
363+
imagekit.deleteFile("file_id", function(error, result) {
364+
if(error) console.log(error);
365+
else console.log(result);
366+
});
367+
368+
369+
// Using Promises
370+
371+
imagekit.deleteFile("file_id"})
372+
}).then(response => {
373+
console.log(response);
374+
}).catch(error => {
375+
console.log(error);
376+
});
377+
```
378+
379+
**6. Bulk Delete Files**
380+
381+
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.
382+
383+
```
384+
385+
// Using Callback Function
386+
387+
imagekit.bulkDeleteFiles(["fileIds"], function(error, result) {
388+
if(error) console.log(error);
389+
else console.log(result);
390+
});
391+
392+
393+
// Using Promises
394+
395+
imagekit.bulkDeleteFiles(["fileIds"])
396+
}).then(response => {
397+
console.log(response);
398+
}).catch(error => {
399+
console.log(error);
292400
});
293401
```
294402

295-
**6. Purge Cache**
403+
**7. Purge Cache**
296404

297405
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.
298406

299407
```
300-
imagekit.purgeCache("full_url", function(err, result) {
301-
408+
// Using Callback Function
409+
410+
imagekit.purgeCache("full_url", function(error, result) {
411+
if(error) console.log(error);
412+
else console.log(result);
413+
});
414+
415+
416+
// Using Promises
417+
418+
imagekit.purgeCache("full_url")
419+
}).then(response => {
420+
console.log(response);
421+
}).catch(error => {
422+
console.log(error);
302423
});
303424
```
304425

305-
**7. Purge Cache Status**
426+
**8. Purge Cache Status**
306427

307428
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)
308429

309430
```
310-
imagekit.getPurgeCacheStatus("cache_request_id", function(err, result) {
311-
431+
// Using Callback Function
432+
433+
imagekit.getPurgeCacheStatus("cache_request_id", function(error, result) {
434+
if(error) console.log(error);
435+
else console.log(result);
436+
});
437+
438+
439+
// Using Promises
440+
441+
imagekit.getPurgeCacheStatus("cache_request_id")
442+
}).then(response => {
443+
console.log(response);
444+
}).catch(error => {
445+
console.log(error);
312446
});
313447
```
314448

constants/errorMessages.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ module.exports = {
1515
"INVALID_PHASH_VALUE": { message: "Invalid pHash value", help: "Both pHash strings must be valid hexadecimal numbers" },
1616
"MISSING_PHASH_VALUE": { message: "Missing pHash value", help: "Please pass two pHash values" },
1717
"UNEQUAL_STRING_LENGTH": { message: "Unequal pHash string length", help: "For distance calucation, the two pHash strings must have equal length" },
18+
//bulk delete errors
19+
"INVALID_FILEIDS_VALUE": {message: "Invalid value for fileIds", help: "fileIds should be an string array of fileId of the files to delete. The array should have atleast one fileId."}
1820
};

demo/index.js

Lines changed: 0 additions & 40 deletions
This file was deleted.

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ var ImageKit = function(opts) {
115115
this.getPurgeCacheStatus = promisify(function(requestId, callback) {
116116
return manage.getPurgeCacheStatus(requestId, this.options, callback);
117117
});
118+
119+
this.bulkDeleteFiles = promisify(function(fileIdArray, callback) {
120+
return manage.bulkDeleteFiles(fileIdArray, this.options, callback);
121+
});
118122

119123
// To generate Signature for upload request
120124
this.getAuthenticationParameters = function(token, timestamp) {

libs/manage/file.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,34 @@ module.exports.listFiles = function(listOptions, defaultOptions, callback) {
157157
});
158158
};
159159

160+
/*
161+
Bulk Delete By FileIds
162+
*/
163+
module.exports.bulkDeleteFiles = function(fileIdArray, defaultOptions, callback) {
164+
165+
if(!Array.isArray(fileIdArray)
166+
|| fileIdArray.length === 0
167+
|| fileIdArray.filter(fileId => typeof(fileId) !== 'string').length > 0) {
168+
respond(true, errorMessages.INVALID_FILEIDS_VALUE, callback);
169+
return;
170+
}
171+
172+
const data = {
173+
fileIds: fileIdArray,
174+
}
175+
176+
const requestOptions = {
177+
url: "https://api.imagekit.io/v1/files/batch/deleteByFileIds",
178+
method: "POST",
179+
json: data
180+
}
160181

182+
request(requestOptions, defaultOptions, function(err, response, body) {
183+
if(err) {
184+
respond(true, err, callback);
185+
return;
186+
}
187+
188+
respond(false, body, callback)
189+
});
190+
};

libs/manage/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ module.exports.getFileDetails = file.getDetails;
66
module.exports.updateFileDetails = file.updateDetails;
77
module.exports.getFileMetadata = file.getMetadata;
88
module.exports.deleteFile = file.deleteFile;
9+
module.exports.bulkDeleteFiles = file.bulkDeleteFiles;
910
module.exports.purgeCache = cache.purgeCache;
1011
module.exports.getPurgeCacheStatus = cache.getPurgeCacheStatus;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit",
3-
"version": "3.0.2",
3+
"version": "3.0.3",
44
"description": "Offical NodeJS SDK for ImageKit.io integration",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)