-
Notifications
You must be signed in to change notification settings - Fork 547
/
Copy paththumbor-mapper.ts
477 lines (421 loc) · 15.7 KB
/
thumbor-mapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import Color from "color";
import ColorName from "color-name";
import { ImageEdits, ImageFitTypes, ImageFormatTypes } from "./lib";
export class ThumborMapper {
private static readonly EMPTY_IMAGE_EDITS: ImageEdits = {};
/**
* Initializer function for creating a new Thumbor mapping, used by the image
* handler to perform image modifications based on legacy URL path requests.
* @param path The request path.
* @returns Image edits based on the request path.
*/
public mapPathToEdits(path: string): ImageEdits {
const fileFormat = path.substring(path.lastIndexOf(".") + 1) as ImageFormatTypes;
let edits: ImageEdits = this.mergeEdits(this.mapCrop(path), this.mapResize(path), this.mapFitIn(path));
// parse the image path. we have to sort here to make sure that when we have a file name without extension,
// and `format` and `quality` filters are passed, then the `format` filter will go first to be able
// to apply the `quality` filter to the target image format.
const filters =
path
.match(/filters:[^)]+/g)
?.map((filter) => `${filter})`)
.sort() ?? [];
for (const filter of filters) {
edits = this.mapFilter(filter, fileFormat, edits);
}
return edits;
}
/**
* Enables users to migrate their current image request model to the SIH solution,
* without changing their legacy application code to accommodate new image requests.
* @param path The URL path extracted from the web request.
* @returns The parsed path using the match pattern and the substitution.
*/
public parseCustomPath(path: string): string {
// Perform the substitution and return
const { REWRITE_MATCH_PATTERN, REWRITE_SUBSTITUTION } = process.env;
if (path === undefined) {
throw new Error("ThumborMapping::ParseCustomPath::PathUndefined");
} else if (REWRITE_MATCH_PATTERN === undefined) {
throw new Error("ThumborMapping::ParseCustomPath::RewriteMatchPatternUndefined");
} else if (REWRITE_SUBSTITUTION === undefined) {
throw new Error("ThumborMapping::ParseCustomPath::RewriteSubstitutionUndefined");
} else {
let parsedPath = "";
if (typeof REWRITE_MATCH_PATTERN === "string") {
const patternStrings = REWRITE_MATCH_PATTERN.split("/");
const flags = patternStrings.pop();
const parsedPatternString = REWRITE_MATCH_PATTERN.slice(1, REWRITE_MATCH_PATTERN.length - 1 - flags.length);
const regExp = new RegExp(parsedPatternString, flags);
parsedPath = path.replace(regExp, REWRITE_SUBSTITUTION);
} else {
parsedPath = path.replace(REWRITE_MATCH_PATTERN, REWRITE_SUBSTITUTION);
}
return parsedPath;
}
}
/**
* Maps background color the current edits object
* @param filterValue The specified color value
* @param currentEdits The edits to be performed
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapBGColor(filterValue: string, currentEdits: Record<string, any>): void {
const color = !ColorName[filterValue] ? `#${filterValue}` : filterValue;
currentEdits.flatten = { background: Color(color).object() };
}
/**
* Maps blur to current edits object
* @param filterValue The blur value provided
* @param currentEdits The edits to be performed
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapBlur(filterValue: string, currentEdits: Record<string, any>): void {
const [radius, sigma] = filterValue.split(",").map((x) => (x === "" ? NaN : Number(x)));
currentEdits.blur = !isNaN(sigma) ? sigma : radius / 2;
}
/**
* Maps convolution to current edits object
* @param filterValue the convolution value provided
* @param currentEdits the edits to be performed
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapConvolution(filterValue: string, currentEdits: Record<string, any>): void {
const values = filterValue.split(",");
const matrix = values[0].split(";").map((str) => Number(str));
const matrixWidth = Number(values[1]);
const matrixHeight = Math.ceil(matrix.length / matrixWidth);
currentEdits.convolve = {
width: matrixWidth,
height: matrixHeight,
kernel: matrix,
};
}
/**
* Maps fill to the current edits object
* @param filterValue The fill value provided
* @param currentEdits The edits to be performed
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapFill(filterValue: string, currentEdits: Record<string, any>): void {
if (currentEdits.resize === undefined) {
currentEdits.resize = {};
}
let color = filterValue;
if (!ColorName[color]) {
color = `#${color}`;
}
currentEdits.resize.fit = ImageFitTypes.CONTAIN;
currentEdits.resize.background = Color(color).object();
}
/**
* Maps the output format to the current edits object
* @param filterValue The output format
* @param currentEdits The edits to be provided
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapFormat(filterValue: string, currentEdits: Record<string, any>): void {
const imageFormatType = filterValue.replace(/[^0-9a-z]/gi, "").replace(/jpg/i, "jpeg") as ImageFormatTypes;
const acceptedValues = [
ImageFormatTypes.HEIC,
ImageFormatTypes.HEIF,
ImageFormatTypes.JPEG,
ImageFormatTypes.PNG,
ImageFormatTypes.RAW,
ImageFormatTypes.TIFF,
ImageFormatTypes.WEBP,
ImageFormatTypes.GIF,
];
if (acceptedValues.includes(imageFormatType)) {
currentEdits.toFormat = imageFormatType;
}
}
/**
* Adds withoutEnlargement option to resize in currentEdits object
* @param currentEdits The edits to be perforemd
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapNoUpscale(currentEdits: Record<string, any>): void {
if (currentEdits.resize === undefined) {
currentEdits.resize = {};
}
currentEdits.resize.withoutEnlargement = true;
}
/**
* Maps resize ratios to the current edits object
* @param filterValue The ratio value
* @param currentEdits The edits to be performed
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapResizeRatio(filterValue: string, currentEdits: Record<string, any>): void {
if (currentEdits.resize === undefined) {
currentEdits.resize = {};
}
const ratio = Number(filterValue);
if (currentEdits.resize.width && currentEdits.resize.height) {
currentEdits.resize.width = Number(currentEdits.resize.width * ratio);
currentEdits.resize.height = Number(currentEdits.resize.height * ratio);
} else {
currentEdits.resize.ratio = Number(filterValue);
}
}
/**
* Maps the quality value of the output format to the current edits
* @param filterValue The quality value provided
* @param currentEdits The edits to be performed
* @param fileFormat The image format
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapQuality(filterValue: string, currentEdits: Record<string, any>, fileFormat: ImageFormatTypes): void {
const toSupportedImageFormatType = (format: ImageFormatTypes): ImageFormatTypes => {
if ([ImageFormatTypes.JPG, ImageFormatTypes.JPEG].includes(format)) {
return ImageFormatTypes.JPEG;
} else if (
[
ImageFormatTypes.PNG,
ImageFormatTypes.WEBP,
ImageFormatTypes.TIFF,
ImageFormatTypes.HEIF,
ImageFormatTypes.GIF,
].includes(format)
) {
return format;
}
};
// trying to get a target image type base on `fileFormat` passed to the current method.
// if we cannot get the target format, then trying to get the target format from `format` filter.
const targetImageFileFormat =
toSupportedImageFormatType(fileFormat) ?? toSupportedImageFormatType(currentEdits.toFormat);
if (targetImageFileFormat) {
currentEdits[targetImageFileFormat] = { quality: Number(filterValue) };
}
}
/**
* Maps stretch fit to the current edits
* @param currentEdits The edits to be performed
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapStretch(currentEdits: Record<string, any>): void {
if (currentEdits.resize === undefined) {
currentEdits.resize = {};
}
// If fit-in is not defined, fit parameter would be 'fill'.
if (currentEdits.resize.fit !== ImageFitTypes.INSIDE) {
currentEdits.resize.fit = ImageFitTypes.FILL;
}
}
/**
* Maps upscale fit to the current edits
* @param currentEdits The edits to be performed
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapUpscale(currentEdits: Record<string, any>): void {
if (currentEdits.resize === undefined) {
currentEdits.resize = {};
}
currentEdits.resize.fit = ImageFitTypes.INSIDE;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private mapWatermark(filterValue: string, currentEdits: Record<string, any>): void {
const options = filterValue.replace(/\s+/g, "").split(",");
const [bucket, key, xPos, yPos, alpha, wRatio, hRatio] = options;
currentEdits.overlayWith = {
bucket,
key,
alpha,
wRatio,
hRatio,
options: {},
};
const allowedPosPattern = /^(100|[1-9]?\d|-(100|[1-9]\d?))p$/;
if (allowedPosPattern.test(xPos) || !isNaN(Number(xPos))) {
currentEdits.overlayWith.options.left = xPos;
}
if (allowedPosPattern.test(yPos) || !isNaN(Number(yPos))) {
currentEdits.overlayWith.options.top = yPos;
}
}
/**
* Scanner function for matching supported Thumbor filters and converting their capabilities into sharp.js supported operations.
* @param filterExpression The URL path filter.
* @param fileFormat The file type of the original image.
* @param previousEdits Cumulative edit, to take into account the previous filters, i.g. `stretch` uses `resize.fit` to make a right update.
* @returns Cumulative edits based on the previous edits and the current filter.
*/
public mapFilter(filterExpression: string, fileFormat: ImageFormatTypes, previousEdits: ImageEdits = {}): ImageEdits {
const matched = filterExpression.match(/:(.+)\((.*)\)/);
const [_, filterName, filterValue] = matched;
const currentEdits = { ...previousEdits };
// Find the proper filter
switch (filterName) {
case "autojpg": {
currentEdits.toFormat = ImageFormatTypes.JPEG;
break;
}
case "background_color": {
this.mapBGColor(filterValue, currentEdits);
break;
}
case "blur": {
this.mapBlur(filterValue, currentEdits);
break;
}
case "convolution": {
this.mapConvolution(filterValue, currentEdits);
break;
}
case "equalize": {
currentEdits.normalize = true;
break;
}
case "fill": {
this.mapFill(filterValue, currentEdits);
break;
}
case "format": {
this.mapFormat(filterValue, currentEdits);
break;
}
case "grayscale": {
currentEdits.grayscale = true;
break;
}
case "no_upscale": {
this.mapNoUpscale(currentEdits);
break;
}
case "proportion": {
this.mapResizeRatio(filterValue, currentEdits);
break;
}
case "quality": {
this.mapQuality(filterValue, currentEdits, fileFormat);
break;
}
case "rgb": {
const percentages = filterValue.split(",");
const values = percentages.map((percentage) => 255 * (Number(percentage) / 100));
const [r, g, b] = values;
currentEdits.tint = { r, g, b };
break;
}
case "rotate": {
currentEdits.rotate = Number(filterValue);
break;
}
case "sharpen": {
const values = filterValue.split(",");
currentEdits.sharpen = 1 + Number(values[1]) / 2;
break;
}
case "stretch": {
this.mapStretch(currentEdits);
break;
}
case "strip_exif":
case "strip_icc": {
currentEdits.rotate = null;
break;
}
case "upscale": {
this.mapUpscale(currentEdits);
break;
}
case "watermark": {
this.mapWatermark(filterValue, currentEdits);
break;
}
}
return currentEdits;
}
/**
* Maps the image path to crop image edit.
* @param path an image path.
* @returns image edits associated with crop.
*/
private mapCrop(path: string): ImageEdits {
const pathCropMatchResult = path.match(/\d{1,6}x\d{1,6}:\d{1,6}x\d{1,6}/g);
if (pathCropMatchResult) {
const [leftTopPoint, rightBottomPoint] = pathCropMatchResult[0].split(":");
const [leftTopX, leftTopY] = leftTopPoint.split("x").map((x) => parseInt(x, 10));
const [rightBottomX, rightBottomY] = rightBottomPoint.split("x").map((x) => parseInt(x, 10));
if (!isNaN(leftTopX) && !isNaN(leftTopY) && !isNaN(rightBottomX) && !isNaN(rightBottomY)) {
const cropEdit: ImageEdits = {
crop: {
left: leftTopX,
top: leftTopY,
width: rightBottomX - leftTopX,
height: rightBottomY - leftTopY,
},
};
return cropEdit;
}
}
return ThumborMapper.EMPTY_IMAGE_EDITS;
}
/**
* Maps the image path to resize image edit.
* @param path An image path.
* @returns Image edits associated with resize.
*/
private mapResize(path: string): ImageEdits {
// Process the dimensions
const dimensionsMatchResult = path.match(/\/((\d+x\d+)|(0x\d+))\//g);
if (dimensionsMatchResult) {
// Assign dimensions from the first match only to avoid parsing dimension from image file names
const [width, height] = dimensionsMatchResult[0]
.replace(/\//g, "")
.split("x")
.map((x) => parseInt(x));
// Set only if the dimensions provided are valid
if (!isNaN(width) && !isNaN(height)) {
const resizeEdit: ImageEdits = { resize: {} };
// If width or height is 0, fit would be inside.
if (width === 0 || height === 0) {
resizeEdit.resize.fit = ImageFitTypes.INSIDE;
}
resizeEdit.resize.width = width === 0 ? null : width;
resizeEdit.resize.height = height === 0 ? null : height;
return resizeEdit;
}
}
return ThumborMapper.EMPTY_IMAGE_EDITS;
}
/**
* Maps the image path to fit image edit.
* @param path An image path.
* @returns Image edits associated with fit-in filter.
*/
private mapFitIn(path: string): ImageEdits {
return path.includes("fit-in") ? { resize: { fit: ImageFitTypes.INSIDE } } : ThumborMapper.EMPTY_IMAGE_EDITS;
}
/**
* A helper method to merge edits.
* @param edits Edits to merge.
* @returns Merged edits.
*/
private mergeEdits(...edits: ImageEdits[]) {
return edits.reduce((result, current) => {
Object.keys(current).forEach((key) => {
if (Array.isArray(result[key]) && Array.isArray(current[key])) {
result[key] = Array.from(new Set(result[key].concat(current[key])));
} else if (this.isObject(result[key]) && this.isObject(current[key])) {
result[key] = this.mergeEdits(result[key], current[key]);
} else {
result[key] = current[key];
}
});
return result;
}, {});
}
/**
* A helper method to check whether a passed argument is object or not.
* @param obj Object to check.
* @returns Whether or not a passed argument is object.
*/
private isObject(obj: unknown): boolean {
return obj && typeof obj === "object" && !Array.isArray(obj);
}
}