Skip to content

Commit 6e43ae4

Browse files
committed
file metadata using remote URL
1 parent e410046 commit 6e43ae4

File tree

4 files changed

+85
-13
lines changed

4 files changed

+85
-13
lines changed

README.md

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,13 @@ Accepts the file ID and fetches the metadata as per the [API documentation here]
329329

330330
```js
331331
// Using Callback Function
332-
333332
imagekit.getFileMetadata("file_id", function(error, result) {
334333
if(error) console.log(error);
335334
else console.log(result);
336335
});
337336

338337

339338
// Using Promises
340-
341339
imagekit.getFileMetadata("file_id")
342340
}).then(response => {
343341
console.log(response);
@@ -346,6 +344,25 @@ imagekit.getFileMetadata("file_id")
346344
});
347345
```
348346

347+
You can also pass the remote URL of the image to get metadata.
348+
349+
```js
350+
// Using Callback Function
351+
imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg", function(error, result) {
352+
if(error) console.log(error);
353+
else console.log(result);
354+
});
355+
356+
357+
// Using Promises
358+
imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg")
359+
}).then(response => {
360+
console.log(response);
361+
}).catch(error => {
362+
console.log(error);
363+
});
364+
```
365+
349366
**4. Update File Details**
350367

351368
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 a second argument is an object with the parameters to be updated.
@@ -506,7 +523,7 @@ imagekit.bulkRemoveTags(["fileIds"], ["tags"]).then(response => {
506523

507524
**11. Copy File**
508525

509-
This will copy a file from one folder to another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-file). This method accepts source file's path and destination folder path.
526+
This will copy a file from one location to another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-file). This method accepts the source file's path and destination folder path.
510527

511528
```js
512529
// Using Callback Function
@@ -527,7 +544,7 @@ imagekit.copyFile("sourceFilePath", "destinationPath").then(response => {
527544

528545
**12. Move File**
529546

530-
This will move a file from one folder to another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-file). This method accepts source file's path and destination folder path.
547+
This will move a file from one location to another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-file). This method accepts the source file's path and destination folder path.
531548

532549
```js
533550
// Using Callback Function
@@ -548,7 +565,7 @@ imagekit.moveFile("sourceFilePath", "destinationPath").then(response => {
548565

549566
**13. Copy Folder**
550567

551-
This will copy one folder into another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-folder). This method accepts source folder's path and destination folder path.
568+
This will copy one folder into another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-folder). This method accepts the source folder's path and destination folder path.
552569

553570
```js
554571
// Using Callback Function
@@ -569,7 +586,7 @@ imagekit.copyFolder("sourceFolderPath", "destinationPath").then(response => {
569586

570587
**14. Move Folder**
571588

572-
This will move one folder into another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-folder). This method accepts source folder's path and destination folder path.
589+
This will move one folder into another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-folder). This method accepts the source folder's path and destination folder path.
573590

574591
```js
575592
// Using Callback Function
@@ -590,7 +607,7 @@ imagekit.moveFolder("sourceFolderPath", "destinationPath").then(response => {
590607

591608
**15. Get bulk job status**
592609

593-
This allows us to get the status of a bulk operation e.g. copy or move folder as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-move-folder-status). This method accepts jobID that is returned by copy and move folder operations.
610+
This allows us to get a bulk operation status e.g. copy or move folder as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-move-folder-status). This method accepts `jobId` that is returned by copy and move folder operations.
594611

595612
```js
596613
// Using Callback Function
@@ -632,7 +649,7 @@ imagekit.createFolder("folderName", "parentFolderPath").then(response => {
632649

633650
**17. Delete Folder**
634651

635-
This will delete the specified folder and all nested files & folders as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-folder). This method accepts full path of folder that is to be deleted.
652+
This will delete the specified folder and all nested files & folders as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-folder). This method accepts the full path of the folder that is to be deleted.
636653

637654
```js
638655
// Using Callback Function

constants/errorMessages.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module.exports = {
77
"CACHE_PURGE_URL_MISSING" : { message : "Missing URL parameter for this request", help : "" },
88
"CACHE_PURGE_STATUS_ID_MISSING" : { message : "Missing Request ID parameter for this request", help : "" },
99
"FILE_ID_MISSING" : { message : "Missing File ID parameter for this request", help : "" },
10+
"FILE_ID_OR_URL_MISSING" : { message : "Pass either File ID or remote URL of the image as first parameter", help : "" },
1011
"UPDATE_DATA_MISSING" : { message : "Missing file update data for this request", help : "" },
1112
"UPDATE_DATA_TAGS_INVALID" : { message : "Invalid tags parameter for this request", help : "tags should be passed as null or an array like ['tag1', 'tag2']" },
1213
"UPDATE_DATA_COORDS_INVALID" : { message : "Invalid customCoordinates parameter for this request", help : "customCoordinates should be passed as null or a string like 'x,y,width,height'" },

libs/manage/file.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,27 @@ module.exports.deleteFile = function(fileId, defaultOptions, callback) {
2828
/*
2929
Get Metadata of a file
3030
*/
31-
module.exports.getMetadata = function(fileId, defaultOptions, callback) {
32-
if(!fileId) {
33-
respond(true, errorMessages.FILE_ID_MISSING, callback);
31+
module.exports.getMetadata = function(fileIdOrURL, defaultOptions, callback) {
32+
if(!fileIdOrURL || fileIdOrURL.trim () == "") {
33+
respond(true, errorMessages.FILE_ID_OR_URL_MISSING, callback);
3434
return;
3535
}
36+
3637
var requestOptions = {
37-
url : "https://api.imagekit.io/v1/files/" + fileId + "/metadata",
38+
url : "https://api.imagekit.io/v1/files/" + fileIdOrURL + "/metadata",
3839
method : "GET",
3940
json : true
4041
};
4142

43+
// In case of URL change the endopint
44+
if(fileIdOrURL.indexOf("http") === 0) {
45+
requestOptions = {
46+
url : `https://api.imagekit.io/v1/metadata?url=${fileIdOrURL}`,
47+
method : "GET",
48+
json : true
49+
};
50+
}
51+
4252
request(requestOptions, defaultOptions, callback);
4353
};
4454

tests/mediaLibrary.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ describe("Media library APIs", function () {
205205
imagekit.getFileMetadata(null, function (err, response) {
206206
expect(err).to.deep.equal({
207207
help: "",
208-
message: "Missing File ID parameter for this request"
208+
message: "Pass either File ID or remote URL of the image as first parameter"
209209
})
210210
done();
211211
});
@@ -422,6 +422,28 @@ describe("Media library APIs", function () {
422422
}, 50);
423423
});
424424

425+
it('Get file metadata using remote URL', function (done) {
426+
var url = "https://ik.imagekit.io/demo/image.jpg";
427+
428+
const scope = nock('https://api.imagekit.io')
429+
.get(`/v1/metadata`)
430+
.basicAuth({ user: initializationParams.privateKey, pass: '' })
431+
.query({
432+
url: url
433+
})
434+
.reply(200, dummyAPISuccessResponse)
435+
436+
var callback = sinon.spy();
437+
438+
imagekit.getFileMetadata(url, callback);
439+
440+
setTimeout(function () {
441+
expect(callback.calledOnce).to.be.true;
442+
sinon.assert.calledWith(callback, null, dummyAPISuccessResponse);
443+
done();
444+
}, 50);
445+
});
446+
425447
it('Get file details', function (done) {
426448
var fileId = "23902390239203923";
427449

@@ -725,6 +747,28 @@ describe("Media library APIs", function () {
725747
}, 50);
726748
});
727749

750+
it('Get file metadata using remote URL', function (done) {
751+
var url = "https://ik.imagekit.io/demo/image.jpg";
752+
753+
const scope = nock('https://api.imagekit.io')
754+
.get(`/v1/metadata`)
755+
.query({
756+
url
757+
})
758+
.basicAuth({ user: initializationParams.privateKey, pass: '' })
759+
.reply(500, dummyAPIErrorResponse)
760+
761+
var callback = sinon.spy();
762+
763+
imagekit.getFileMetadata(url, callback);
764+
765+
setTimeout(function () {
766+
expect(callback.calledOnce).to.be.true;
767+
sinon.assert.calledWith(callback, dummyAPIErrorResponse, null);
768+
done();
769+
}, 50);
770+
});
771+
728772
it('Get file details', function (done) {
729773
var fileId = "23902390239203923";
730774

0 commit comments

Comments
 (0)