From c74e4658554b0a85aae766931259ed19ab97e180 Mon Sep 17 00:00:00 2001 From: AndrzejSala Date: Tue, 2 Apr 2019 14:14:00 +0200 Subject: [PATCH 1/4] feat(filelink): Add able to switch of validation on task params --- src/lib/filelink.ts | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/lib/filelink.ts b/src/lib/filelink.ts index 331db85b..a5a85d31 100644 --- a/src/lib/filelink.ts +++ b/src/lib/filelink.ts @@ -450,7 +450,7 @@ export class Filelink { * @private * @memberof Filelink */ - private validator = getValidator(TransformSchema); + private static validator = getValidator(TransformSchema); /** * Applied transforms array @@ -486,6 +486,14 @@ export class Filelink { */ private b64: boolean = false; + /** + * should use a validator to check params of every task + * @private + * @type {boolean} + * @memberof Filelink + */ + private useValidator: boolean = true; + /** * Custom CNAME * @@ -538,6 +546,18 @@ export class Filelink { return this; } + /** + * Switch the useValidator flag + * + * @param {boolean} flag + * @returns + * @memberof Filelink + */ + setUseValidator(flag: boolean) { + this.useValidator = flag; + return this; + } + /** * Set cname for transformation link * @@ -634,7 +654,9 @@ export class Filelink { * @memberof Filelink */ addTask(name: string, params?) { - this.validateTask(name, params); + if (this.useValidator) { + Filelink.validateTask(name, params); + } if (name !== 'cache' && typeof params === 'boolean') { if (!params) { @@ -1220,11 +1242,11 @@ export class Filelink { * @returns {void} * @memberof Filelink */ - private validateTask(name, options): void { + private static validateTask(name, options): void { const toValidate = {}; toValidate[name] = options; - const res = this.validator(toValidate); + const res = Filelink.validator(toValidate); if (res.errors.length) { throw new FilestackError(`Task "${name}" validation error, Params: ${JSON.stringify(options)}`, res.errors); } From 24485dbe32f3ab5422490c639d4b5bdbfc6390ea Mon Sep 17 00:00:00 2001 From: AndrzejSala Date: Tue, 2 Apr 2019 14:28:51 +0200 Subject: [PATCH 2/4] chore: --- src/lib/filelink.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/filelink.ts b/src/lib/filelink.ts index a5a85d31..e7866428 100644 --- a/src/lib/filelink.ts +++ b/src/lib/filelink.ts @@ -655,7 +655,7 @@ export class Filelink { */ addTask(name: string, params?) { if (this.useValidator) { - Filelink.validateTask(name, params); + this.validateTask(name, params); } if (name !== 'cache' && typeof params === 'boolean') { @@ -1242,7 +1242,7 @@ export class Filelink { * @returns {void} * @memberof Filelink */ - private static validateTask(name, options): void { + private validateTask(name, options): void { const toValidate = {}; toValidate[name] = options; From f06d4336fb110bda60710e9682fb9791de3d1a88 Mon Sep 17 00:00:00 2001 From: AndrzejSala Date: Tue, 2 Apr 2019 16:26:30 +0200 Subject: [PATCH 3/4] chore: --- src/lib/filelink.ts | 60 ++++++++++++++++++++++++++++++++------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/src/lib/filelink.ts b/src/lib/filelink.ts index e7866428..f5b4b1a0 100644 --- a/src/lib/filelink.ts +++ b/src/lib/filelink.ts @@ -110,18 +110,18 @@ export enum CropfacesType { * Convert to format */ export enum VideoTypes { - h264 = 'h264', - h264_hi = 'h264.hi', - webm = 'webm', - 'webm-hi' = 'webm.hi', - ogg = 'ogg', - 'ogg-hi' = 'ogg.hi', - 'hls-variant' = 'hls.variant', - mp3 = 'mp3', - oga = 'oga', - m4a = 'm4a', - aac = 'aac', - hls = 'hls.variant.audio', + h264 = 'h264', + h264_hi = 'h264.hi', + webm = 'webm', + 'webm-hi' = 'webm.hi', + ogg = 'ogg', + 'ogg-hi' = 'ogg.hi', + 'hls-variant' = 'hls.variant', + mp3 = 'mp3', + oga = 'oga', + m4a = 'm4a', + aac = 'aac', + hls = 'hls.variant.audio', } export enum URLScreenshotAgent { @@ -599,6 +599,9 @@ export class Filelink { * @memberof Filelink */ getTransformations() { + if (this.useValidator) { + this.validateTasks(this.transforms); + } return this.transforms; } @@ -612,6 +615,10 @@ export class Filelink { const returnUrl = []; returnUrl.push(this.getCdnHost()); + if (this.useValidator) { + this.validateTasks(this.transforms); + } + if (this.apikey) { returnUrl.push(this.apikey); } @@ -654,10 +661,6 @@ export class Filelink { * @memberof Filelink */ addTask(name: string, params?) { - if (this.useValidator) { - this.validateTask(name, params); - } - if (name !== 'cache' && typeof params === 'boolean') { if (!params) { return this; @@ -1254,6 +1257,16 @@ export class Filelink { return; } + private validateTasks(transformations): void { + const transformationsObj = this.arrayToObject(transformations, 'name', 'params'); + const res = Filelink.validator(transformationsObj); + if (res.errors.length) { + throw new FilestackError(`Params validation error: ${JSON.stringify(transformations)}`, res.errors); + } + + return; + } + /** * Returns correct cdn url with cname support * @@ -1340,7 +1353,7 @@ export class Filelink { * @returns {string} * @memberof Filelink */ - private escapeValue (value: string): string { + private escapeValue(value: string): string { if (typeof value !== 'string') { return value; } @@ -1371,4 +1384,17 @@ export class Filelink { return `[${toReturn}]`; } + /** + * Converts array of objects to object + * + * @private + * @example [{name: 'resize', params: {height: 125}}] => {resize: {height: 125}} + * @param arr - any array + */ + private arrayToObject = (array: any[], nameKey: string, dataKey: string) => { + return array.reduce((obj, item) => { + obj[item[nameKey]] = item[dataKey]; + return obj; + }, {}); + } } From dcad920fbe604813b388a5a9c8f39dd393d3760c Mon Sep 17 00:00:00 2001 From: AndrzejSala Date: Tue, 2 Apr 2019 17:03:04 +0200 Subject: [PATCH 4/4] chore: --- src/lib/filelink.ts | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/lib/filelink.ts b/src/lib/filelink.ts index f5b4b1a0..6e7579ac 100644 --- a/src/lib/filelink.ts +++ b/src/lib/filelink.ts @@ -1237,33 +1237,19 @@ export class Filelink { } /** - * Validate single task against schema + * Validate every task against schema * * @private - * @param {*} name - * @param {*} options + * @param {object[]} transformations - object which contain all transformations * @returns {void} * @memberof Filelink */ - private validateTask(name, options): void { - const toValidate = {}; - toValidate[name] = options; - - const res = Filelink.validator(toValidate); - if (res.errors.length) { - throw new FilestackError(`Task "${name}" validation error, Params: ${JSON.stringify(options)}`, res.errors); - } - - return; - } - - private validateTasks(transformations): void { + private validateTasks(transformations: object[]): void { const transformationsObj = this.arrayToObject(transformations, 'name', 'params'); const res = Filelink.validator(transformationsObj); if (res.errors.length) { throw new FilestackError(`Params validation error: ${JSON.stringify(transformations)}`, res.errors); } - return; } @@ -1391,7 +1377,7 @@ export class Filelink { * @example [{name: 'resize', params: {height: 125}}] => {resize: {height: 125}} * @param arr - any array */ - private arrayToObject = (array: any[], nameKey: string, dataKey: string) => { + private arrayToObject = (array: object[], nameKey: string, dataKey: string) => { return array.reduce((obj, item) => { obj[item[nameKey]] = item[dataKey]; return obj;