import { Injectable } from '@nestjs/common' ; import { FileElementResponse } from './dto/file-element.reposonse' ; import { format } from 'date-fns' ; import { path } from 'app-root-path' ; import { ensureDir , writeFile } from 'fs-extra' ; import * as sharp from 'sharp' ; import { MFile } from './mfile.class' ; @ Injectable ( ) export class FilesService { async saveFiles ( files ) { const dateFolder = format ( new Date ( ) , 'yyyy-MM-dd' ) ; const uploadFolder = `${ path }/uploads/${ dateFolder }` ; await ensureDir ( uploadFolder ) ; const res = [ ] ; for ( const file of files ) { await writeFile ( `${ uploadFolder }/${ file . originalname }` , file . buffer ) ; res . push ( { url : `/uploads/${ dateFolder }/${ file . originalname }` , name : file . originalname } ) ; } return res ; } convertToWebP ( file ) { return sharp ( file ) . webp ( ) . toBuffer ( ) ; } }