mejoras al formulario de registro organizaciones productivas
This commit is contained in:
36
apps/api/src/common/pipes/image-processing.pipe.ts
Normal file
36
apps/api/src/common/pipes/image-processing.pipe.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { Injectable, PipeTransform } from '@nestjs/common';
|
||||
import * as path from 'path';
|
||||
import sharp from 'sharp';
|
||||
|
||||
@Injectable()
|
||||
export class ImageProcessingPipe implements PipeTransform {
|
||||
async transform(
|
||||
files: Express.Multer.File[] | Express.Multer.File,
|
||||
): Promise<Express.Multer.File[] | Express.Multer.File> {
|
||||
if (!files) return files;
|
||||
|
||||
const processItem = async (
|
||||
file: Express.Multer.File,
|
||||
): Promise<Express.Multer.File> => {
|
||||
const processedBuffer = await sharp(file.buffer)
|
||||
.webp({ quality: 80 })
|
||||
.toBuffer();
|
||||
|
||||
const originalName = path.parse(file.originalname).name;
|
||||
|
||||
return {
|
||||
...file,
|
||||
buffer: processedBuffer,
|
||||
originalname: `${originalName}.webp`,
|
||||
mimetype: 'image/webp',
|
||||
size: processedBuffer.length,
|
||||
};
|
||||
};
|
||||
|
||||
if (Array.isArray(files)) {
|
||||
return await Promise.all(files.map((file) => processItem(file)));
|
||||
}
|
||||
|
||||
return await processItem(files);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user