Skip to content

Commit a9f97e7

Browse files
authored
Feature/add missing transformations parameters (#403)
* feat(filelink): add missing watermark * feat(filelink): add missing transformations parameters * fix(schema): duplicate values
1 parent be65723 commit a9f97e7

File tree

4 files changed

+193
-6
lines changed

4 files changed

+193
-6
lines changed

src/lib/api/transform.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ export enum ECropfacesType {
124124
fill = 'fill',
125125
}
126126

127+
/**
128+
* SmartCrop mode options enum
129+
*/
130+
export enum ESmartCropMode {
131+
fit = 'fit',
132+
auto = 'auto',
133+
}
134+
135+
export enum EImageWatermarkPosition {
136+
top = 'top',
137+
middle = 'middle',
138+
bottom = 'bottom',
139+
left = 'left',
140+
center = 'center',
141+
right = 'right',
142+
}
143+
127144
/**
128145
* Convert to format
129146
*/
@@ -181,13 +198,30 @@ export enum EVideoAccessMode {
181198
crop = 'crop',
182199
}
183200

201+
184202
/**
185203
* @see https://www.filestack.com/docs/image-transformations
186204
*/
187205
export interface TransformOptions {
188206
flip?: boolean;
189207
compress?: boolean;
190208
flop?: boolean;
209+
no_metadata?: boolean;
210+
pjpg?: {
211+
quality: number;
212+
metadata: boolean;
213+
};
214+
smart_crop?: {
215+
mode?: ESmartCropMode
216+
width: number;
217+
height: number;
218+
};
219+
watermark?: {
220+
files: string;
221+
size?: number;
222+
position?: EImageWatermarkPosition | EImageWatermarkPosition[];
223+
};
224+
imagesize?: boolean;
191225
enchance?: boolean;
192226
redeye?: boolean;
193227
monochrome?: boolean;

src/lib/filelink.spec.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { Filelink, ShapeType, VideoAccessMode, AnimationParams, FitOptions, Align } from './filelink';
18+
import { Filelink, ShapeType, VideoAccessMode, AnimationParams, FitOptions, Align, WatermarkParams, ImageWatermarkPosition, SmartCropParams, SmartCropMode, ProgressiveJpgParams } from './filelink';
1919
import { TransformSchema } from './../schema/transforms.schema';
2020
import * as validator from '../schema';
2121

@@ -252,6 +252,50 @@ describe('Different tasks', () => {
252252
expect(filelink.toString()).toBe('https://cdn.filestackcontent.com/DEFAULT_API_KEY/flop/5aYkEQJSQCmYShsoCnZN');
253253
});
254254

255+
it('should be able to add imagesize', () => {
256+
filelink.imagesize();
257+
expect(filelink.toString()).toBe('https://cdn.filestackcontent.com/DEFAULT_API_KEY/imagesize/5aYkEQJSQCmYShsoCnZN');
258+
});
259+
260+
it('should be able to add no_metadata', () => {
261+
filelink.noMetadata();
262+
expect(filelink.toString()).toBe('https://cdn.filestackcontent.com/DEFAULT_API_KEY/no_metadata/5aYkEQJSQCmYShsoCnZN');
263+
});
264+
265+
it('should be able to add pjpg', () => {
266+
const params: ProgressiveJpgParams = {
267+
quality: 50,
268+
metadata: false,
269+
};
270+
271+
filelink.pjpg(params);
272+
expect(filelink.toString()).toBe('https://cdn.filestackcontent.com/DEFAULT_API_KEY/pjpg=quality:50,metadata:false/5aYkEQJSQCmYShsoCnZN');
273+
});
274+
275+
it('should be able to add smart_crop', () => {
276+
const params: SmartCropParams = {
277+
mode: SmartCropMode.auto,
278+
width: 1000,
279+
height: 1000,
280+
};
281+
282+
filelink.smartCrop(params);
283+
expect(filelink.toString()).toBe('https://cdn.filestackcontent.com/DEFAULT_API_KEY/smart_crop=mode:auto,width:1000,height:1000/5aYkEQJSQCmYShsoCnZN');
284+
});
285+
286+
it('should be able to add watermark', () => {
287+
const params: WatermarkParams = {
288+
file: 'http://welcome-swiss.com/wp-content/uploads/2015/12/Swiss-landscape.jpg',
289+
size: 20,
290+
position: ImageWatermarkPosition.top,
291+
};
292+
293+
filelink.watermark(params);
294+
expect(filelink.toString()).toBe(
295+
'https://cdn.filestackcontent.com/DEFAULT_API_KEY/watermark=file:http://welcome-swiss.com/wp-content/uploads/2015/12/Swiss-landscape.jpg,size:20,position:top/5aYkEQJSQCmYShsoCnZN'
296+
);
297+
});
298+
255299
it('should be able to enhance', () => {
256300
filelink.enhance();
257301
expect(filelink.toString()).toBe('https://cdn.filestackcontent.com/DEFAULT_API_KEY/enhance/5aYkEQJSQCmYShsoCnZN');

src/lib/filelink.ts

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ export enum CropfacesType {
101101
fill = 'fill',
102102
}
103103

104+
/**
105+
* Watermark postion options enum
106+
*/
107+
export enum ImageWatermarkPosition {
108+
top = 'top',
109+
middle = 'middle',
110+
bottom = 'bottom',
111+
left = 'left',
112+
center = 'center',
113+
right = 'right',
114+
}
115+
116+
/**
117+
* SmartCrop options enum
118+
*/
119+
export enum SmartCropMode {
120+
face = 'face',
121+
auto = 'auto',
122+
}
123+
104124
/**
105125
* Convert to format
106126
*/
@@ -285,6 +305,23 @@ export interface CompressParams {
285305
metadata?: boolean;
286306
}
287307

308+
export interface WatermarkParams {
309+
file: string;
310+
size?: number;
311+
position?: ImageWatermarkPosition | ImageWatermarkPosition[];
312+
}
313+
314+
export interface ProgressiveJpgParams {
315+
quality: number;
316+
metadata: boolean;
317+
}
318+
319+
export interface SmartCropParams {
320+
mode: SmartCropMode;
321+
width: number;
322+
height: number;
323+
}
324+
288325
export interface SharpenParams {
289326
amount: number;
290327
}
@@ -628,7 +665,7 @@ export class Filelink {
628665
throw new FilestackError('External sources requires apikey to handle transforms');
629666
}
630667

631-
if (!isExternal && typeof this.source === 'string' && (!handleRegexp.test(this.source) && this.source.indexOf('filestackcontent') === -1)) {
668+
if (!isExternal && typeof this.source === 'string' && !handleRegexp.test(this.source) && this.source.indexOf('filestackcontent') === -1) {
632669
throw new FilestackError('Invalid filestack source provided');
633670
}
634671
}
@@ -786,6 +823,61 @@ export class Filelink {
786823
return this.addTask('flop', true);
787824
}
788825

826+
/**
827+
* Adds imagesize transformation
828+
*
829+
* @see https://www.filestack.com/docs/api/processing/#image-size
830+
* @returns this
831+
* @memberof Filelink
832+
*/
833+
imagesize() {
834+
return this.addTask('imagesize', true);
835+
}
836+
837+
/**
838+
* Adds noMetadata transformation
839+
*
840+
* @see https://www.filestack.com/docs/api/processing/#strip-metadata
841+
* @returns this
842+
* @memberof Filelink
843+
*/
844+
noMetadata() {
845+
return this.addTask('no_metadata', true);
846+
}
847+
848+
/**
849+
* Adds Progressive JPEG transformation
850+
*
851+
* @see https://www.filestack.com/docs/api/processing/#progressive-jpeg
852+
* @returns this
853+
* @memberof Filelink
854+
*/
855+
pjpg(params: ProgressiveJpgParams) {
856+
return this.addTask('pjpg', params);
857+
}
858+
859+
/**
860+
* Adds imagesize transformation
861+
*
862+
* @see https://www.filestack.com/docs/api/processing/#smart-crop
863+
* @returns this
864+
* @memberof Filelink
865+
*/
866+
smartCrop(params: SmartCropParams) {
867+
return this.addTask('smart_crop', params);
868+
}
869+
870+
/**
871+
* Adds watermart transformation
872+
*
873+
* @see https://www.filestack.com/docs/api/processing/#watermark
874+
* @returns this
875+
* @memberof Filelink
876+
*/
877+
watermark(params: WatermarkParams) {
878+
return this.addTask('watermark', params);
879+
}
880+
789881
/**
790882
* Adds enhance transformation
791883
*
@@ -1413,7 +1505,7 @@ export class Filelink {
14131505
private generateTransformString(): string {
14141506
let transforms = [];
14151507

1416-
this.transforms.forEach((el) => {
1508+
this.transforms.forEach(el => {
14171509
transforms.push(this.optionToString(el.name, el.params));
14181510
});
14191511

@@ -1443,7 +1535,7 @@ export class Filelink {
14431535
return key;
14441536
}
14451537

1446-
Object.keys(values).forEach((i) => {
1538+
Object.keys(values).forEach(i => {
14471539
if (Array.isArray(values[i])) {
14481540
optionsString.push(`${i}:${this.arrayToString(values[i])}`);
14491541
return;
@@ -1483,7 +1575,7 @@ export class Filelink {
14831575
* @param arr - any array
14841576
*/
14851577
private arrayToString(arr: any[]): string {
1486-
const toReturn = arr.map((el) => {
1578+
const toReturn = arr.map(el => {
14871579
if (Array.isArray(el)) {
14881580
return this.arrayToString(el);
14891581
}

src/schema/transforms.schema.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,31 @@ export const TransformSchema = {
9090
type: 'boolean',
9191
additionalProperties: false,
9292
},
93+
smart_crop: {
94+
width: {
95+
type: 'integer',
96+
minimum: 0,
97+
maximum: 10000,
98+
},
99+
height: {
100+
type: 'integer',
101+
minimum: 0,
102+
maximum: 10000,
103+
},
104+
mode: {
105+
type: 'string',
106+
default: 'auto',
107+
enum: ['face', 'auto'],
108+
},
109+
},
93110
animate: {
94111
type: 'object',
95112
properties: {
96113
delay: {
97114
type: 'integer',
98115
minimum: 0,
99116
maximum: 10000,
100-
default: 100,
117+
default: 1000,
101118
},
102119
loop: {
103120
type: 'integer',

0 commit comments

Comments
 (0)