Skip to content

Commit

Permalink
Add support for remote\local "pre" function invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
YomesInc authored and Nir Maoz committed Aug 18, 2019
1 parent f5056f1 commit e534efa
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/transformation.js
Expand Up @@ -504,6 +504,14 @@ function processVar(varArray) {
}
}

function processCustomFunction(value) {
if (value.function_type === "remote") {
return [value.function_type, btoa(value.source)].join(":")
} else if (value.function_type === "wasm") {
return [value.function_type, value.source].join(":")
}
}

/**
* Transformation Class methods.
* This is a list of the parameters defined in Transformation.
Expand Down Expand Up @@ -605,6 +613,21 @@ class Transformation extends TransformationBase {
return this.param(value, "crop", "c");
}

customFunction(value) {
return this.param(value, "custom_function", "fn", () => {
return processCustomFunction(value);
});
}

customPreFunction(value) {
if (this.get('custom_function')) {
return;
}
return this.rawParam(value, "custom_function", "", () => {
value = processCustomFunction(value);
return value ? `fn_pre:${value}` : value;
});
}

defaultImage(value) {
return this.param(value, "default_image", "d");
Expand Down Expand Up @@ -837,16 +860,6 @@ class Transformation extends TransformationBase {
});
}

customFunction(value) {
return this.param(value, "custom_function", "fn", () => {
if(value.function_type === "remote"){
return [value.function_type, btoa(value.source)].join(":")
}
else if (value.function_type === "wasm")
return [value.function_type, value.source].join(":")
})
}

x(value) {
return this.param(value, "x", "x", Expression.normalize);
}
Expand Down Expand Up @@ -878,6 +891,7 @@ Transformation.methods = [
"colorSpace",
"crop",
"customFunction",
"customPreFunction",
"defaultImage",
"delay",
"density",
Expand Down
19 changes: 19 additions & 0 deletions test/spec/transformation-spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions types/cloudinary-core.d.ts
Expand Up @@ -2,6 +2,11 @@ export as namespace cloudinary;

/****************************** Constants *************************************/
type CropMode = string | "scale" | "fit" | "limit" | "mfit" | "fill" | "lfill" | "pad" | "lpad" | "mpad" | "crop" | "thumb" | "imagga_crop" | "imagga_scale";
type CustomFunctionType = "wasm" | "remote";
interface CustomFunction {
function_type: CustomFunctionType;
source: string;
}
type Gravity = string | "north_west" | "north" | "north_east" | "west" | "center" | "east" | "south_west" | "south" | "south_east" | "xy_center" |
"face" | "face:center" | "face:auto" | "faces" | "faces:center" | "faces:auto" | "body" | "body:face" | "adv_face" | "adv_faces" | "adv_eyes" |
"custom" | "custom:face" | "custom:faces" | "custom:adv_face" | "custom:adv_faces" |
Expand Down Expand Up @@ -180,6 +185,8 @@ export class Transformation {
color(value: string): Transformation; // e.g. "red" or "rgb:20a020"
colorSpace(value: ColorSpace): Transformation;
crop(value: CropMode): Transformation;
customFunction(value: CustomFunction): Transformation;
customPreFunction(value: CustomFunction): Transformation;
defaultImage(value: string): Transformation; // public id of an uploaded image
delay(value: string): Transformation;
density(value: number): Transformation; // Control the density to use while converting a PDF document to images. (range: 50-300, default: 150)
Expand Down Expand Up @@ -290,6 +297,8 @@ export namespace Transformation {
color?: string; // e.g. "red" or "rgb:20a020"
colorSpace?: ColorSpace;
crop?: CropMode,
customFunction?: CustomFunction,
customPreFunction?: CustomFunction,
defaultImage?: string; // public id of an uploaded image
delay?: string;
density?: number | string; // Control the density to use while converting a PDF document to images. (range: 50-300, default: 150)
Expand Down

0 comments on commit e534efa

Please sign in to comment.