Skip to content

Commit

Permalink
Added extensions parameter and related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aman051197 committed Nov 3, 2021
1 parent 82d747f commit c2cd08f
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 10 deletions.
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,13 @@ Sample usage
imagekit.upload({
file : <url|base_64|binary>, //required
fileName : "my_file_name.jpg", //required
extensions: [
{
name: "google-auto-tagging",
maxTags: 5,
minConfidence: 95
}
]
}, function(error, result) {
if(error) console.log(error);
else console.log(result);
Expand All @@ -258,6 +265,13 @@ imagekit.upload({
imagekit.upload({
file : <url|base_64|binary>, //required
fileName : "my_file_name.jpg", //required
extensions: [
{
name: "google-auto-tagging",
maxTags: 5,
minConfidence: 95
}
]
}).then(response => {
console.log(response);
}).catch(error => {
Expand Down Expand Up @@ -374,7 +388,14 @@ Update parameters associated with the file as per the [API documentation here](h

imagekit.updateFileDetails("file_id", {
tags : ['image_tag'],
customCoordinates : "10,10,100,100"
customCoordinates : "10,10,100,100",
extensions: [
{
name: "google-auto-tagging",
maxTags: 5,
minConfidence: 95
}
]
}, function(error, result) {
if(error) console.log(error);
else console.log(result);
Expand All @@ -385,7 +406,14 @@ imagekit.updateFileDetails("file_id", {

imagekit.updateFileDetails("file_id", {
tags : ['image_tag'],
customCoordinates : "10,10,100,100"
customCoordinates : "10,10,100,100",
extensions: [
{
name: "google-auto-tagging",
maxTags: 5,
minConfidence: 95
}
]
}).then(response => {
console.log(response);
}).catch(error => {
Expand Down
16 changes: 16 additions & 0 deletions libs/interfaces/FileDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export interface FileDetailsOptions {
* Example - 50,50,500,500
*/
customCoordinates?: string;
/*
* Object with array of extensions to be processed on the image.
*/
extensions?: object[];
/*
* Final status of pending extensions will be sent to this URL.
*/
webhookUrl?: string
}

/**
Expand Down Expand Up @@ -68,4 +76,12 @@ export interface FileDetailsResponse {
* The type of file, it could be either image or non-image.
*/
fileType: FileType;
/*
* AITags field is populated only because the google-auto-tagging extension was executed synchronously and it received a successresponse.
*/
AITags?: object[];
/*
* Field object which will contain the status of each extension at the time of completion of the update/upload request.
*/
extensionStatus?: { [key: string]: string }
}
8 changes: 8 additions & 0 deletions libs/interfaces/UploadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ export interface UploadOptions {
* 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.
*/
responseFields?: string;
/*
* Object with array of extensions to be processed on the image.
*/
extensions?: object[];
/*
* Final status of pending extensions will be sent to this URL.
*/
webhookUrl?: string
}
8 changes: 8 additions & 0 deletions libs/interfaces/UploadResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ export interface UploadResponse {
* The metadata of the upload file. Use responseFields property in request to get the metadata returned in response of upload API.
*/
metadata?: string;
/*
* AITags field is populated only because the google-auto-tagging extension was executed synchronously and it received a successresponse.
*/
AITags?: object[];
/*
* Field object which will contain the status of each extension at the time of completion of the update/upload request.
*/
extensionStatus?: { [key: string]: string }
}
2 changes: 2 additions & 0 deletions libs/manage/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ const updateDetails = function (
var data = {
tags: updateData.tags,
customCoordinates: updateData.customCoordinates,
extensions: updateData.extensions,
webhookUrl: updateData.webhookUrl
};

var requestOptions = {
Expand Down
8 changes: 7 additions & 1 deletion libs/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type FormDataOptions = Modify<
file: string | Buffer | object;
useUniqueFileName: string;
isPrivateFile: string;
extensions?: string;
webhookUrl?: string;
}
>;

Expand Down Expand Up @@ -50,7 +52,11 @@ export default function (
};
} else if (key == "tags" && Array.isArray(uploadOptions.tags)) {
formData.tags = uploadOptions.tags.join(",");
} else {
}
else if(key == "extensions" && Array.isArray(uploadOptions.extensions)){
formData.extensions = JSON.stringify(uploadOptions.extensions);
}
else {
formData[key] = String(uploadOptions[key]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagekit",
"version": "3.2.2",
"version": "3.2.3",
"description": "Offical NodeJS SDK for ImageKit.io integration",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 22 additions & 3 deletions sample/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ const sampleApp = async () => {
filesList[0].fileId,
["buildings", "day"],
"10,10,100,100",
//Uncomment to send extensions parameter
// [
// {
// name: "google-auto-tagging",
// maxTags: 5,
// minConfidence: 95
// }
// ]
);
console.log("File Update Response: ", JSON.stringify(fileUpdateResponse, undefined, 2), "\n");

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

const uploadFileBase64 = async (imagekitInstance, filePath, fileName) => {
const file_base64 = fs.readFileSync(filePath, "base64");
const response = await imagekitInstance.upload({ file: file_base64, fileName });
//Uncomment to send extensions parameter
// var extensions = [
// {
// name: "google-auto-tagging",
// maxTags: 5,
// minConfidence: 95
// }
// ];
const response = await imagekitInstance.upload({ file: file_base64, fileName/*, extensions*/});
return response;
};

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

const updateFileDetails = async (imagekitInstance, fileId, tags = [], customCoordinates = "") => {
const updateFileDetails = async (imagekitInstance, fileId, tags = [], customCoordinates = "", extensions = [], webhookUrl = "") => {
let options = {};
if (Array.isArray(tags) && tags.length > 0) Object.assign(options, { tags });
if (typeof customCoordinates === "string" && customCoordinates.length > 0)
Object.assign(options, { customCoordinates });

if (Array.isArray(extensions) && extensions.length > 0)
Object.assign(options,{ extensions });
if (typeof webhookUrl === "string" && webhookUrl.length > 0)
Object.assign(options,{ webhookUrl })
const response = await imagekitInstance.updateFileDetails(fileId, options);
return response;
};
Expand Down
10 changes: 9 additions & 1 deletion tests/mediaLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,15 @@ describe("Media library APIs", function () {

var updateData = {
tags: ["tag1", "tag2"],
customCoordinates: "10,10,100,100"
customCoordinates: "10,10,100,100",
extensions: [
{
name: "google-auto-tagging",
maxTags: 5,
minConfidence: 95
}
],
webhookUrl: "https://some-domain/some-api-id"
}

const scope = nock('https://api.imagekit.io')
Expand Down
17 changes: 15 additions & 2 deletions tests/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ const uploadSuccessResponseObj = {
"tags": ["t-shirt", "round-neck", "sale2019"],
"isPrivateFile": false,
"customCoordinates": null,
"fileType": "image"
"fileType": "image",
"AITags":[{"name":"Face","confidence":99.95,"source":"aws-auto-tagging"}],
"extensionStatus":{"aws-auto-tagging":"success"}
};

describe("File upload custom endpoint", function () {
Expand Down Expand Up @@ -97,10 +99,19 @@ describe("File upload", function () {
tags: ["tag1","tag2"], // array handling
isPrivateFile: true, // Boolean handling
useUniqueFileName: "false", // As string
responseFields : ["tags,metadata"]
responseFields : ["tags,metadata"],
extensions: [
{
name: "aws-auto-tagging",
minConfidence: 80,
maxTags: 10
}
],
webhookUrl: "https://your-domain/?appId=some-id"
};

var callback = sinon.spy();
var jsonStringifiedExtensions = JSON.stringify(fileOptions.extensions);

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

Expand Down

0 comments on commit c2cd08f

Please sign in to comment.