@maioradv/nestjs-core 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,4 +8,5 @@ export declare class ImageCompressionPipe implements PipeTransform<Express.Multe
8
8
  constructor(options?: CompressionOptions);
9
9
  transform(file: Express.Multer.File, metadata: ArgumentMetadata): Promise<Express.Multer.File>;
10
10
  private defaultQuality;
11
+ private sanitizeFileName;
11
12
  }
@@ -22,13 +22,19 @@ let ImageCompressionPipe = class ImageCompressionPipe {
22
22
  }
23
23
  async transform(file, metadata) {
24
24
  try {
25
- const meta = await (0, sharp_1.default)(file.buffer).metadata();
25
+ const picture = (0, sharp_1.default)(file.buffer, { failOn: 'error' });
26
+ const meta = await picture.metadata();
27
+ const stats = await picture.stats();
28
+ const dev = stats.channels.map(c => c.stdev);
29
+ const avg = dev.reduce((a, b) => a + b) / dev.length;
30
+ const max = Math.max(...dev);
31
+ const isFlat = avg < 20 && max < 30;
26
32
  const defaultQuality = this.defaultQuality(meta.width, meta.height);
27
33
  const [resolutionCap, quality] = [
28
34
  this.options.resolutionCap ?? 2560,
29
35
  this.options.quality ?? defaultQuality
30
36
  ];
31
- const compressedBuffer = await (0, sharp_1.default)(file.buffer, { failOn: 'error' })
37
+ const compressedBuffer = await picture
32
38
  .resize({
33
39
  width: resolutionCap,
34
40
  height: resolutionCap,
@@ -36,13 +42,13 @@ let ImageCompressionPipe = class ImageCompressionPipe {
36
42
  withoutEnlargement: true
37
43
  })
38
44
  .withMetadata()
39
- .webp({ quality: quality })
45
+ .webp(isFlat ? { lossless: true } : { quality: quality })
40
46
  .rotate()
41
47
  .toBuffer();
42
48
  file.buffer = compressedBuffer;
43
49
  file.mimetype = 'image/webp';
44
50
  file.size = compressedBuffer.length;
45
- file.originalname = file.originalname.replace((0, path_1.extname)(file.originalname), '.webp').replaceAll(' ', '-');
51
+ file.originalname = this.sanitizeFileName(file.originalname).replace((0, path_1.extname)(file.originalname), '.webp');
46
52
  return file;
47
53
  }
48
54
  catch (error) {
@@ -55,6 +61,17 @@ let ImageCompressionPipe = class ImageCompressionPipe {
55
61
  const area = width * height;
56
62
  return area > 2e6 ? 50 : area > 1e6 ? 60 : 70;
57
63
  }
64
+ sanitizeFileName(name) {
65
+ const decoded = decodeURIComponent(name);
66
+ return decoded
67
+ .trim()
68
+ .toLowerCase()
69
+ .normalize("NFD").replace(/[\u0300-\u036f]/g, '') // no accent
70
+ .replace(/\s+/g, '-') // no space
71
+ .replace(/[^a-z0-9\.-]/g, '-') // no special except dot and dash
72
+ .replace(/-+/g, '-') // no multiple dashes
73
+ .replace(/^\-+|\-+$/g, ''); // trim dashes from start and end
74
+ }
58
75
  };
59
76
  exports.ImageCompressionPipe = ImageCompressionPipe;
60
77
  exports.ImageCompressionPipe = ImageCompressionPipe = __decorate([
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@maioradv/nestjs-core",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "NestJS helpers by MaiorADV",
5
5
  "repository": "https://github.com/maioradv/nestjs-core.git",
6
6
  "author": "Maior ADV Srl",