@@ -110,18 +110,18 @@ export enum CropfacesType {
110
110
* Convert to format
111
111
*/
112
112
export enum VideoTypes {
113
- h264 = 'h264' ,
114
- h264_hi = 'h264.hi' ,
115
- webm = 'webm' ,
116
- 'webm-hi' = 'webm.hi' ,
117
- ogg = 'ogg' ,
118
- 'ogg-hi' = 'ogg.hi' ,
119
- 'hls-variant' = 'hls.variant' ,
120
- mp3 = 'mp3' ,
121
- oga = 'oga' ,
122
- m4a = 'm4a' ,
123
- aac = 'aac' ,
124
- hls = 'hls.variant.audio' ,
113
+ h264 = 'h264' ,
114
+ h264_hi = 'h264.hi' ,
115
+ webm = 'webm' ,
116
+ 'webm-hi' = 'webm.hi' ,
117
+ ogg = 'ogg' ,
118
+ 'ogg-hi' = 'ogg.hi' ,
119
+ 'hls-variant' = 'hls.variant' ,
120
+ mp3 = 'mp3' ,
121
+ oga = 'oga' ,
122
+ m4a = 'm4a' ,
123
+ aac = 'aac' ,
124
+ hls = 'hls.variant.audio' ,
125
125
}
126
126
127
127
export enum URLScreenshotAgent {
@@ -450,7 +450,7 @@ export class Filelink {
450
450
* @private
451
451
* @memberof Filelink
452
452
*/
453
- private validator = getValidator ( TransformSchema ) ;
453
+ private static validator = getValidator ( TransformSchema ) ;
454
454
455
455
/**
456
456
* Applied transforms array
@@ -486,6 +486,14 @@ export class Filelink {
486
486
*/
487
487
private b64 : boolean = false ;
488
488
489
+ /**
490
+ * should use a validator to check params of every task
491
+ * @private
492
+ * @type {boolean }
493
+ * @memberof Filelink
494
+ */
495
+ private useValidator : boolean = true ;
496
+
489
497
/**
490
498
* Custom CNAME
491
499
*
@@ -538,6 +546,18 @@ export class Filelink {
538
546
return this ;
539
547
}
540
548
549
+ /**
550
+ * Switch the useValidator flag
551
+ *
552
+ * @param {boolean } flag
553
+ * @returns
554
+ * @memberof Filelink
555
+ */
556
+ setUseValidator ( flag : boolean ) {
557
+ this . useValidator = flag ;
558
+ return this ;
559
+ }
560
+
541
561
/**
542
562
* Set cname for transformation link
543
563
*
@@ -579,6 +599,9 @@ export class Filelink {
579
599
* @memberof Filelink
580
600
*/
581
601
getTransformations ( ) {
602
+ if ( this . useValidator ) {
603
+ this . validateTasks ( this . transforms ) ;
604
+ }
582
605
return this . transforms ;
583
606
}
584
607
@@ -592,6 +615,10 @@ export class Filelink {
592
615
const returnUrl = [ ] ;
593
616
returnUrl . push ( this . getCdnHost ( ) ) ;
594
617
618
+ if ( this . useValidator ) {
619
+ this . validateTasks ( this . transforms ) ;
620
+ }
621
+
595
622
if ( this . apikey ) {
596
623
returnUrl . push ( this . apikey ) ;
597
624
}
@@ -634,8 +661,6 @@ export class Filelink {
634
661
* @memberof Filelink
635
662
*/
636
663
addTask ( name : string , params ?) {
637
- this . validateTask ( name , params ) ;
638
-
639
664
if ( name !== 'cache' && typeof params === 'boolean' ) {
640
665
if ( ! params ) {
641
666
return this ;
@@ -1212,23 +1237,19 @@ export class Filelink {
1212
1237
}
1213
1238
1214
1239
/**
1215
- * Validate single task against schema
1240
+ * Validate every task against schema
1216
1241
*
1217
1242
* @private
1218
- * @param {* } name
1219
- * @param {* } options
1243
+ * @param {object[] } transformations - object which contain all transformations
1220
1244
* @returns {void }
1221
1245
* @memberof Filelink
1222
1246
*/
1223
- private validateTask ( name , options ) : void {
1224
- const toValidate = { } ;
1225
- toValidate [ name ] = options ;
1226
-
1227
- const res = this . validator ( toValidate ) ;
1247
+ private validateTasks ( transformations : object [ ] ) : void {
1248
+ const transformationsObj = this . arrayToObject ( transformations , 'name' , 'params' ) ;
1249
+ const res = Filelink . validator ( transformationsObj ) ;
1228
1250
if ( res . errors . length ) {
1229
- throw new FilestackError ( `Task " ${ name } " validation error, Params : ${ JSON . stringify ( options ) } ` , res . errors ) ;
1251
+ throw new FilestackError ( `Params validation error: ${ JSON . stringify ( transformations ) } ` , res . errors ) ;
1230
1252
}
1231
-
1232
1253
return ;
1233
1254
}
1234
1255
@@ -1318,7 +1339,7 @@ export class Filelink {
1318
1339
* @returns {string }
1319
1340
* @memberof Filelink
1320
1341
*/
1321
- private escapeValue ( value : string ) : string {
1342
+ private escapeValue ( value : string ) : string {
1322
1343
if ( typeof value !== 'string' ) {
1323
1344
return value ;
1324
1345
}
@@ -1349,4 +1370,17 @@ export class Filelink {
1349
1370
return `[${ toReturn } ]` ;
1350
1371
}
1351
1372
1373
+ /**
1374
+ * Converts array of objects to object
1375
+ *
1376
+ * @private
1377
+ * @example [{name: 'resize', params: {height: 125}}] => {resize: {height: 125}}
1378
+ * @param arr - any array
1379
+ */
1380
+ private arrayToObject = ( array : object [ ] , nameKey : string , dataKey : string ) => {
1381
+ return array . reduce ( ( obj , item ) => {
1382
+ obj [ item [ nameKey ] ] = item [ dataKey ] ;
1383
+ return obj ;
1384
+ } , { } ) ;
1385
+ }
1352
1386
}
0 commit comments