Skip to content

Commit c2cd08f

Browse files
committed
Added extensions parameter and related tests
1 parent 82d747f commit c2cd08f

File tree

10 files changed

+118
-10
lines changed

10 files changed

+118
-10
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ Sample usage
248248
imagekit.upload({
249249
file : <url|base_64|binary>, //required
250250
fileName : "my_file_name.jpg", //required
251+
extensions: [
252+
{
253+
name: "google-auto-tagging",
254+
maxTags: 5,
255+
minConfidence: 95
256+
}
257+
]
251258
}, function(error, result) {
252259
if(error) console.log(error);
253260
else console.log(result);
@@ -258,6 +265,13 @@ imagekit.upload({
258265
imagekit.upload({
259266
file : <url|base_64|binary>, //required
260267
fileName : "my_file_name.jpg", //required
268+
extensions: [
269+
{
270+
name: "google-auto-tagging",
271+
maxTags: 5,
272+
minConfidence: 95
273+
}
274+
]
261275
}).then(response => {
262276
console.log(response);
263277
}).catch(error => {
@@ -374,7 +388,14 @@ Update parameters associated with the file as per the [API documentation here](h
374388

375389
imagekit.updateFileDetails("file_id", {
376390
tags : ['image_tag'],
377-
customCoordinates : "10,10,100,100"
391+
customCoordinates : "10,10,100,100",
392+
extensions: [
393+
{
394+
name: "google-auto-tagging",
395+
maxTags: 5,
396+
minConfidence: 95
397+
}
398+
]
378399
}, function(error, result) {
379400
if(error) console.log(error);
380401
else console.log(result);
@@ -385,7 +406,14 @@ imagekit.updateFileDetails("file_id", {
385406

386407
imagekit.updateFileDetails("file_id", {
387408
tags : ['image_tag'],
388-
customCoordinates : "10,10,100,100"
409+
customCoordinates : "10,10,100,100",
410+
extensions: [
411+
{
412+
name: "google-auto-tagging",
413+
maxTags: 5,
414+
minConfidence: 95
415+
}
416+
]
389417
}).then(response => {
390418
console.log(response);
391419
}).catch(error => {

libs/interfaces/FileDetails.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ export interface FileDetailsOptions {
1616
* Example - 50,50,500,500
1717
*/
1818
customCoordinates?: string;
19+
/*
20+
* Object with array of extensions to be processed on the image.
21+
*/
22+
extensions?: object[];
23+
/*
24+
* Final status of pending extensions will be sent to this URL.
25+
*/
26+
webhookUrl?: string
1927
}
2028

2129
/**
@@ -68,4 +76,12 @@ export interface FileDetailsResponse {
6876
* The type of file, it could be either image or non-image.
6977
*/
7078
fileType: FileType;
79+
/*
80+
* AITags field is populated only because the google-auto-tagging extension was executed synchronously and it received a successresponse.
81+
*/
82+
AITags?: object[];
83+
/*
84+
* Field object which will contain the status of each extension at the time of completion of the update/upload request.
85+
*/
86+
extensionStatus?: { [key: string]: string }
7187
}

libs/interfaces/UploadOptions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ export interface UploadOptions {
6969
* For example, set the value of this field to tags,customCoordinates,isPrivateFile,metadata to get value of tags, customCoordinates, isPrivateFile , and metadata in the response.
7070
*/
7171
responseFields?: string;
72+
/*
73+
* Object with array of extensions to be processed on the image.
74+
*/
75+
extensions?: object[];
76+
/*
77+
* Final status of pending extensions will be sent to this URL.
78+
*/
79+
webhookUrl?: string
7280
}

libs/interfaces/UploadResponse.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,12 @@ export interface UploadResponse {
5858
* The metadata of the upload file. Use responseFields property in request to get the metadata returned in response of upload API.
5959
*/
6060
metadata?: string;
61+
/*
62+
* AITags field is populated only because the google-auto-tagging extension was executed synchronously and it received a successresponse.
63+
*/
64+
AITags?: object[];
65+
/*
66+
* Field object which will contain the status of each extension at the time of completion of the update/upload request.
67+
*/
68+
extensionStatus?: { [key: string]: string }
6169
}

libs/manage/file.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ const updateDetails = function (
118118
var data = {
119119
tags: updateData.tags,
120120
customCoordinates: updateData.customCoordinates,
121+
extensions: updateData.extensions,
122+
webhookUrl: updateData.webhookUrl
121123
};
122124

123125
var requestOptions = {

libs/upload/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type FormDataOptions = Modify<
1212
file: string | Buffer | object;
1313
useUniqueFileName: string;
1414
isPrivateFile: string;
15+
extensions?: string;
16+
webhookUrl?: string;
1517
}
1618
>;
1719

@@ -50,7 +52,11 @@ export default function (
5052
};
5153
} else if (key == "tags" && Array.isArray(uploadOptions.tags)) {
5254
formData.tags = uploadOptions.tags.join(",");
53-
} else {
55+
}
56+
else if(key == "extensions" && Array.isArray(uploadOptions.extensions)){
57+
formData.extensions = JSON.stringify(uploadOptions.extensions);
58+
}
59+
else {
5460
formData[key] = String(uploadOptions[key]);
5561
}
5662
}

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.2.2",
3+
"version": "3.2.3",
44
"description": "Offical NodeJS SDK for ImageKit.io integration",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

sample/index.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ const sampleApp = async () => {
8080
filesList[0].fileId,
8181
["buildings", "day"],
8282
"10,10,100,100",
83+
//Uncomment to send extensions parameter
84+
// [
85+
// {
86+
// name: "google-auto-tagging",
87+
// maxTags: 5,
88+
// minConfidence: 95
89+
// }
90+
// ]
8391
);
8492
console.log("File Update Response: ", JSON.stringify(fileUpdateResponse, undefined, 2), "\n");
8593

@@ -168,7 +176,15 @@ const uploadFileBuffer = async (imagekitInstance, filePath, fileName) => {
168176

169177
const uploadFileBase64 = async (imagekitInstance, filePath, fileName) => {
170178
const file_base64 = fs.readFileSync(filePath, "base64");
171-
const response = await imagekitInstance.upload({ file: file_base64, fileName });
179+
//Uncomment to send extensions parameter
180+
// var extensions = [
181+
// {
182+
// name: "google-auto-tagging",
183+
// maxTags: 5,
184+
// minConfidence: 95
185+
// }
186+
// ];
187+
const response = await imagekitInstance.upload({ file: file_base64, fileName/*, extensions*/});
172188
return response;
173189
};
174190

@@ -195,12 +211,15 @@ const getFileMetadata = async (imagekitInstance, fileId) => {
195211
return response;
196212
};
197213

198-
const updateFileDetails = async (imagekitInstance, fileId, tags = [], customCoordinates = "") => {
214+
const updateFileDetails = async (imagekitInstance, fileId, tags = [], customCoordinates = "", extensions = [], webhookUrl = "") => {
199215
let options = {};
200216
if (Array.isArray(tags) && tags.length > 0) Object.assign(options, { tags });
201217
if (typeof customCoordinates === "string" && customCoordinates.length > 0)
202218
Object.assign(options, { customCoordinates });
203-
219+
if (Array.isArray(extensions) && extensions.length > 0)
220+
Object.assign(options,{ extensions });
221+
if (typeof webhookUrl === "string" && webhookUrl.length > 0)
222+
Object.assign(options,{ webhookUrl })
204223
const response = await imagekitInstance.updateFileDetails(fileId, options);
205224
return response;
206225
};

tests/mediaLibrary.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,15 @@ describe("Media library APIs", function () {
263263

264264
var updateData = {
265265
tags: ["tag1", "tag2"],
266-
customCoordinates: "10,10,100,100"
266+
customCoordinates: "10,10,100,100",
267+
extensions: [
268+
{
269+
name: "google-auto-tagging",
270+
maxTags: 5,
271+
minConfidence: 95
272+
}
273+
],
274+
webhookUrl: "https://some-domain/some-api-id"
267275
}
268276

269277
const scope = nock('https://api.imagekit.io')

tests/upload.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ const uploadSuccessResponseObj = {
2323
"tags": ["t-shirt", "round-neck", "sale2019"],
2424
"isPrivateFile": false,
2525
"customCoordinates": null,
26-
"fileType": "image"
26+
"fileType": "image",
27+
"AITags":[{"name":"Face","confidence":99.95,"source":"aws-auto-tagging"}],
28+
"extensionStatus":{"aws-auto-tagging":"success"}
2729
};
2830

2931
describe("File upload custom endpoint", function () {
@@ -97,10 +99,19 @@ describe("File upload", function () {
9799
tags: ["tag1","tag2"], // array handling
98100
isPrivateFile: true, // Boolean handling
99101
useUniqueFileName: "false", // As string
100-
responseFields : ["tags,metadata"]
102+
responseFields : ["tags,metadata"],
103+
extensions: [
104+
{
105+
name: "aws-auto-tagging",
106+
minConfidence: 80,
107+
maxTags: 10
108+
}
109+
],
110+
webhookUrl: "https://your-domain/?appId=some-id"
101111
};
102112

103113
var callback = sinon.spy();
114+
var jsonStringifiedExtensions = JSON.stringify(fileOptions.extensions);
104115

105116
const scope = nock('https://upload.imagekit.io/api')
106117
.post('/v1/files/upload')
@@ -114,6 +125,8 @@ describe("File upload", function () {
114125
checkFormData({requestBody,boundary,fieldName:"isPrivateFile",fieldValue:"true"});
115126
checkFormData({requestBody,boundary,fieldName:"useUniqueFileName",fieldValue:"false"});
116127
checkFormData({requestBody,boundary,fieldName:"responseFields",fieldValue:"tags,metadata"});
128+
checkFormData({requestBody,boundary,fieldName:"extensions",fieldValue:jsonStringifiedExtensions});
129+
checkFormData({requestBody,boundary,fieldName:"webhookUrl",fieldValue:"https://your-domain/?appId=some-id"});
117130
done()
118131
})
119132

0 commit comments

Comments
 (0)