Skip to content

Commit 9fe6eb0

Browse files
authored
Merge pull request #98 from imagekit-developer/IK-1499
added checks paramter
2 parents 8166889 + 5475c8c commit 9fe6eb0

File tree

5 files changed

+35
-3
lines changed

5 files changed

+35
-3
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ imagekit.upload({
350350
value: 'w-100'
351351
}
352352
]
353-
}
353+
},
354+
checks={`"file.size" < "1mb"`} // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
354355
}, function(error, result) {
355356
if(error) console.log(error);
356357
else console.log(result);
@@ -376,7 +377,8 @@ imagekit.upload({
376377
value: 'w-100'
377378
}
378379
]
379-
}
380+
},
381+
checks={`"file.size" < "1mb"`}
380382
}).then(response => {
381383
console.log(response);
382384
}).catch(error => {

libs/interfaces/UploadOptions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,8 @@ export interface UploadOptions {
108108
[key: string]: string | number | boolean | Array<string | number | boolean>;
109109
},
110110
transformation?: Transformation
111+
/**
112+
* Optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
113+
*/
114+
checks?: string
111115
}

libs/upload/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export default function (
9292
} else if (key === "transformation" && typeof uploadOptions.transformation === "object" &&
9393
uploadOptions.transformation !== null) {
9494
form.append(key, JSON.stringify(uploadOptions.transformation));
95+
} else if (key === "checks" && uploadOptions.checks) {
96+
form.append(key, uploadOptions.checks);
9597
} else {
9698
form.append(key, String(uploadOptions[key]));
9799
}

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

tests/upload.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,4 +548,28 @@ describe("File upload", function () {
548548
expect(callback.calledOnce).to.be.true;
549549
sinon.assert.calledWith(callback, errRes, null);
550550
});
551+
552+
it("With checks option", function (done) {
553+
const fileOptions = {
554+
fileName: "test_file_name",
555+
file: "test_file_content",
556+
checks: "'request.folder' : '/'",
557+
};
558+
559+
var callback = sinon.spy();
560+
561+
const scope = nock("https://upload.imagekit.io/api")
562+
.post("/v1/files/upload")
563+
.basicAuth({ user: initializationParams.privateKey, pass: "" })
564+
.reply(200, function (uri, requestBody) {
565+
expect(this.req.headers["content-type"]).include("multipart/form-data; boundary=---------------------");
566+
var boundary = this.req.headers["content-type"].replace("multipart/form-data; boundary=", "");
567+
checkFormData({ requestBody, boundary, fieldName: "fileName", fieldValue: fileOptions.fileName });
568+
checkFormData({ requestBody, boundary, fieldName: "file", fieldValue: fileOptions.file });
569+
checkFormData({ requestBody, boundary, fieldName: "checks", fieldValue: fileOptions.checks });
570+
done();
571+
});
572+
573+
imagekit.upload(fileOptions, callback);
574+
});
551575
});

0 commit comments

Comments
 (0)