Skip to content

Commit b532041

Browse files
author
RTLcoil
authored
Add eval parameter to upload params (#438)
* Add eval parameter support to upload
1 parent 4085e9f commit b532041

File tree

7 files changed

+35
-4
lines changed

7 files changed

+35
-4
lines changed

lib-es5/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ function build_upload_params(options) {
308308
eager: utils.build_eager(options.eager),
309309
eager_async: utils.as_safe_bool(options.eager_async),
310310
eager_notification_url: options.eager_notification_url,
311+
eval: options.eval,
311312
exif: utils.as_safe_bool(options.exif),
312313
faces: utils.as_safe_bool(options.faces),
313314
folder: options.folder,

lib/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ function build_upload_params(options) {
289289
eager: utils.build_eager(options.eager),
290290
eager_async: utils.as_safe_bool(options.eager_async),
291291
eager_notification_url: options.eager_notification_url,
292+
eval: options.eval,
292293
exif: utils.as_safe_bool(options.exif),
293294
faces: utils.as_safe_bool(options.faces),
294295
folder: options.folder,

test/integration/api/admin/api_spec.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const {
1818
PRESETS,
1919
TRANSFORMATIONS,
2020
PUBLIC_ID_PREFIX,
21-
UNIQUE_TEST_FOLDER
21+
UNIQUE_TEST_FOLDER,
22+
TEST_EVAL_STR
2223
} = testConstants;
2324

2425
const {
@@ -656,7 +657,8 @@ describe("api", function () {
656657
colors: true,
657658
unsigned: true,
658659
disallow_public_id: true,
659-
live: true
660+
live: true,
661+
eval: TEST_EVAL_STR
660662
});
661663
var expectedPath="/.*\/upload_presets/"+API_TEST_UPLOAD_PRESET3+"$";
662664
sinon.assert.calledWith(requestSpy, sinon.match({
@@ -667,6 +669,7 @@ describe("api", function () {
667669
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('unsigned', true, "unsigned=true")));
668670
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('disallow_public_id', true, "disallow_public_id=true")));
669671
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('live', true, "live=true")));
672+
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('eval', TEST_EVAL_STR, `eval=${TEST_EVAL_STR}`)));
670673
});
671674
});
672675
it("should allow creating upload_presets", function () {
@@ -675,7 +678,8 @@ describe("api", function () {
675678
folder: "upload_folder",
676679
unsigned: true,
677680
tags: UPLOAD_TAGS,
678-
live: true
681+
live: true,
682+
eval: TEST_EVAL_STR
679683
}).then((preset) => {
680684
cloudinary.v2.api.delete_upload_preset(preset.name).catch((err) => {
681685
console.log(err);
@@ -685,6 +689,7 @@ describe("api", function () {
685689

686690
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('unsigned', true, "unsigned=true")));
687691
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('live', true, "live=true")));
692+
sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('eval', TEST_EVAL_STR, `eval=${TEST_EVAL_STR}`)));
688693
});
689694
});
690695
});

test/integration/api/uploader/uploader_spec.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const UPLOADER_V2 = cloudinary.v2.uploader;
3030

3131
const {
3232
TIMEOUT,
33-
TAGS
33+
TAGS,
34+
TEST_EVAL_STR,
35+
TEST_IMG_WIDTH
3436
} = testConstants;
3537

3638
const {
@@ -1133,6 +1135,21 @@ describe("uploader", function () {
11331135
});
11341136
});
11351137

1138+
it('should add the eval parameter to an uploaded asset', async () => {
1139+
const testEvalTagsResult = ['a', 'b'];
1140+
const result = await UPLOADER_V2.upload(IMAGE_FILE, {
1141+
tags: [TEST_TAG],
1142+
eval: TEST_EVAL_STR
1143+
});
1144+
1145+
expect(result).not.to.be.empty();
1146+
expect(result.tags).to.be.an("array");
1147+
expect(result.tags).to.eql(testEvalTagsResult);
1148+
expect(result.context).to.be.an("object");
1149+
expect(result.context.custom).to.be.an("object");
1150+
expect(result.context.custom.width).to.eql(TEST_IMG_WIDTH);
1151+
});
1152+
11361153
describe("sign requests", function () {
11371154
var configBck2 = void 0;
11381155
var writeSpy;

test/spechelper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ exports.apiParamMatcher = function (name, value) {
119119
params = {};
120120
params[name] = value;
121121
expected = querystring.stringify(params);
122+
expected = exports.escapeRegexp(expected);
122123
return function (arg) {
123124
return new RegExp(expected).test(arg);
124125
};

test/testUtils/testConstants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ const TEST_TAG_PREFIX = "cloudinary_npm_test"; // identifies resources created b
1313
const TEST_TAG = `${TEST_TAG_PREFIX}_${UNIQUE_JOB_SUFFIX_ID}`;
1414
const UPLOAD_TAGS = [TEST_TAG, TEST_TAG_PREFIX, SDK_TAG];
1515
const UNIQUE_TEST_FOLDER = `${TEST_TAG}_${UNIQUE_JOB_SUFFIX_ID}_folder`;
16+
const TEST_IMG_WIDTH = 241;
1617
const TEST_CLOUD_NAME = process.env.CLOUDINARY_URL.split('@')[1];
1718

19+
const TEST_EVAL_STR = 'if (resource_info["width"] < 450) { upload_options["tags"] = "a,b" }; ' +
20+
'upload_options["context"] = "width=" + resource_info["width"]';
1821
module.exports = {
1922
TEST_TAG_PREFIX,
23+
TEST_IMG_WIDTH,
2024
TEST_CLOUD_NAME,
2125
PUBLIC_ID_PREFIX,
2226
UNIQUE_TEST_FOLDER,
27+
TEST_EVAL_STR,
2328
TIMEOUT: {
2429
SHORT: 5000,
2530
MEDIUM: 20000,

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ declare module 'cloudinary' {
491491
eager?: TransformationOptions;
492492
eager_async?: boolean;
493493
eager_notification_url?: string;
494+
eval?: string;
494495
exif?: boolean;
495496
faces?: boolean;
496497
folder?: string;

0 commit comments

Comments
 (0)