From 594290ccd5b65849bc9221e344d4b1908d644e53 Mon Sep 17 00:00:00 2001 From: RTLcoil Date: Wed, 2 Oct 2019 15:01:14 +0300 Subject: [PATCH] Add custom pre function support --- lib-es5/utils/index.js | 21 ++++++++++++++----- lib/utils/index.js | 17 ++++++++++++--- test/cloudinary_spec.js | 46 ++++++++++++++++++++++++++++++++--------- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index 6db22f3b..3ce1cb16 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -221,9 +221,19 @@ function process_custom_function(customFunction) { } if (customFunction.function_type === "remote") { return [customFunction.function_type, base64EncodeURL(customFunction.source)].join(":"); - } else { - return [customFunction.function_type, customFunction.source].join(":"); } + return [customFunction.function_type, customFunction.source].join(":"); +} + +/** + * Parse custom_pre_function options + * @private + * @param {object|*} customPreFunction a custom function object containing function_type and source values + * @return {string|*} custom_pre_function transformation string + */ +function process_custom_pre_function(customPreFunction) { + var result = process_custom_function(customPreFunction); + return utils.isString(result) ? `pre:${result}` : null; } /** @@ -546,8 +556,9 @@ function generate_transformation_string(options) { var overlay = process_layer(utils.option_consume(options, "overlay")); var underlay = process_layer(utils.option_consume(options, "underlay")); var ifValue = process_if(utils.option_consume(options, "if")); - var fps = utils.option_consume(options, 'fps'); var custom_function = process_custom_function(utils.option_consume(options, "custom_function")); + var custom_pre_function = process_custom_pre_function(utils.option_consume(options, "custom_pre_function")); + var fps = utils.option_consume(options, 'fps'); if (isArray(fps)) { fps = fps.join('-'); } @@ -561,7 +572,7 @@ function generate_transformation_string(options) { dpr: normalize_expression(dpr), e: normalize_expression(effect), fl: flags, - fn: custom_function, + fn: custom_function || custom_pre_function, fps: fps, h: normalize_expression(height), ki: normalize_expression(utils.option_consume(options, "keyframe_interval")), @@ -1520,4 +1531,4 @@ exports.build_streaming_profiles_param = build_streaming_profiles_param; exports.hashToParameters = hashToParameters; exports.present = present; exports.only = only; -exports.jsonArrayParam = jsonArrayParam; +exports.jsonArrayParam = jsonArrayParam; \ No newline at end of file diff --git a/lib/utils/index.js b/lib/utils/index.js index a643b407..4ef854a3 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -213,9 +213,19 @@ function process_custom_function(customFunction) { } if (customFunction.function_type === "remote") { return [customFunction.function_type, base64EncodeURL(customFunction.source)].join(":"); - } else { - return [customFunction.function_type, customFunction.source].join(":"); } + return [customFunction.function_type, customFunction.source].join(":"); +} + +/** + * Parse custom_pre_function options + * @private + * @param {object|*} customPreFunction a custom function object containing function_type and source values + * @return {string|*} custom_pre_function transformation string + */ +function process_custom_pre_function(customPreFunction) { + let result = process_custom_function(customPreFunction); + return utils.isString(result) ? `pre:${result}` : null; } /** @@ -543,6 +553,7 @@ function generate_transformation_string(options) { let underlay = process_layer(utils.option_consume(options, "underlay")); let ifValue = process_if(utils.option_consume(options, "if")); let custom_function = process_custom_function(utils.option_consume(options, "custom_function")); + let custom_pre_function = process_custom_pre_function(utils.option_consume(options, "custom_pre_function")); let fps = utils.option_consume(options, 'fps'); if (isArray(fps)) { fps = fps.join('-'); @@ -557,7 +568,7 @@ function generate_transformation_string(options) { dpr: normalize_expression(dpr), e: normalize_expression(effect), fl: flags, - fn: custom_function, + fn: custom_function || custom_pre_function, fps: fps, h: normalize_expression(height), ki: normalize_expression(utils.option_consume(options, "keyframe_interval")), diff --git a/test/cloudinary_spec.js b/test/cloudinary_spec.js index 2d1a7c42..8d6f0937 100644 --- a/test/cloudinary_spec.js +++ b/test/cloudinary_spec.js @@ -424,7 +424,7 @@ describe("cloudinary", function () { it('should support custom function of type wasm with a source', function () { var options, result; options = { - custom_function: {function_type: 'wasm', source: 'blur.wasm'} + custom_function: { function_type: 'wasm', source: 'blur.wasm' }, }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); @@ -433,7 +433,7 @@ describe("cloudinary", function () { it('should support arbitrary custom function types', function () { var options, result; options = { - custom_function: {function_type: 'amazing', source: 'awesome'} + custom_function: { function_type: 'amazing', source: 'awesome' }, }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); @@ -442,7 +442,7 @@ describe("cloudinary", function () { it('should support custom function with no source', function () { var options, result; options = { - custom_function: {function_type: 'wasm'} + custom_function: { function_type: 'wasm' }, }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); @@ -451,7 +451,7 @@ describe("cloudinary", function () { it('should support custom function with no function_type', function () { var options, result; options = { - custom_function: {source: 'blur.wasm'} + custom_function: { source: 'blur.wasm' }, }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); @@ -460,7 +460,7 @@ describe("cloudinary", function () { it('should support custom function that is not an object', function () { var options, result; options = { - custom_function: [] + custom_function: [], }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); @@ -469,30 +469,56 @@ describe("cloudinary", function () { it('should support custom function with no function_type or source', function () { var options, result; options = { - custom_function: {} + custom_function: {}, }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_:/test"); }); - it('should support custom function of type remote', function() { + it('should support custom function of type remote', function () { var options, result; options = { - custom_function: {function_type: 'remote', source: 'https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryFunction'} + custom_function: { + function_type: 'remote', + source: + 'https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryFunction', + }, }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlGdW5jdGlvbg==/test"); }); - it('should should not include custom function with undefined value', function() { + it('should should not include custom function with undefined value', function () { var options, result; options = { - custom_function: undefined + custom_function: undefined, }; result = cloudinary.utils.url("test", options); expect(options).to.eql({}); expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/test"); }); + it('should support custom pre function', function () { + var options, result; + options = { + custom_pre_function: { + function_type: 'remote', + source: + 'https://df34ra4a.execute-api.us-west-2.amazonaws.com/default/cloudinaryFunction', + }, + }; + result = cloudinary.utils.url("test", options); + expect(options).to.eql({}); + expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_pre:remote:aHR0cHM6Ly9kZjM0cmE0YS5leGVjdXRlLWFwaS51cy13ZXN0LTIuYW1hem9uYXdzLmNvbS9kZWZhdWx0L2Nsb3VkaW5hcnlGdW5jdGlvbg==/test"); + }); + it('should support custom pre function with no function_type or source', function () { + var options, result; + options = { + custom_pre_function: {}, + }; + result = cloudinary.utils.url("test", options); + expect(options).to.eql({}); + expect(result).to.eql("http://res.cloudinary.com/test123/image/upload/fn_pre::/test"); + }); it("should support density", function () { var options, result; options = {