Gathering detailed insights and metrics for o-nest-fastify-multer
Gathering detailed insights and metrics for o-nest-fastify-multer
The objective of this module is to provide a package with filters already prepared to work with 'fastify-multer'.
npm install o-nest-fastify-multer
Typescript
Module System
Node Version
NPM Version
TypeScript (100%)
Total Downloads
269
Last Day
2
Last Week
8
Last Month
16
Last Year
61
5 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.1
Package Id
o-nest-fastify-multer@1.0.1
Unpacked Size
30.24 kB
Size
6.75 kB
File Count
28
NPM Version
8.19.4
Node Version
16.20.1
Publised On
26 Jul 2023
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
100%
8
Compared to previous week
Last month
1,500%
16
Compared to previous month
Last year
-70.7%
61
Compared to previous year
3
1npm i o-nest-fastify-multer
Before installing and using this package you must make sure you have fastify-multer
installed.
Fixed about Buffer() deprecated
issue
1npm i fastify-multer
You must configure this in the main.js
of your Nest application.
1import { NestFactory } from '@nestjs/core'; 2import { 3 FastifyAdapter, 4 NestFastifyApplication, 5} from '@nestjs/platform-fastify'; 6import { AppModule } from './app.module'; 7import { contentParser } from 'fastify-multer'; 8 9async function bootstrap() { 10 const app = await NestFactory.create<NestFastifyApplication>( 11 AppModule, 12 new FastifyAdapter(), 13 ); 14 await app.register(contentParser); 15 await app.listen(3000); 16} 17 18bootstrap()
1import { 2 Body, 3 Controller, 4 Post, 5 Req, 6 UploadedFiles 7} from '@nestjs/common'; 8 9// import the filters to use from the module. 10import { 11 FastifyFileInterceptor, 12 FastifyFilesInterceptor, 13 FastifyFileFieldsInterceptor, 14} from 'o-nest-fastify-multer'; 15 16// import multer to use your methods 17import { diskStorage } from 'multer'; 18 19@Controller('image') 20export class ImageController { 21 22 @Post('uploadFile') 23 // use this interceptor to specify one file 24 @FastifyFileInterceptor( 25 // fileName 26 'avatar' 27 // here you can add any multer configuration. 28 { 29 storage: diskStorage({ 30 destination: './upload/single', // path where the file will be downloaded 31 filename: editFileName, // here you can put your own function to edit multer file name when saving to local disk 32 } 33 fileFilter: imageFileFilter, // here you can put your own function to filter the received files 34 } 35 ), 36 uploadFile( 37 @Req() req: Request, 38 // use this param decorator to capture the file. The file type Express.Multer.File is used as this is used. 39 @UploadedFiles() file: Express.Multer.File, 40 @Body() body: , 41 ) { 42 return file; 43 } 44 45 @Post('uploadFiles') 46 // use this interceptor to specify more than one file 47 @FastifyFilesInterceptor( 48 // fileName 49 'avatar', 50 // maxCount 51 1 52 // here you can add any multer configuration. 53 { 54 storage: diskStorage({ 55 destination: './upload/single', // path where the file will be downloaded 56 filename: editFileName, // here you can put your own function to edit multer file name when saving to local disk 57 } 58 fileFilter: imageFileFilter, // here you can put your own function to filter the received files 59 } 60 ), 61 uploadFiles( 62 @Req() req: Request, 63 // use this param decorator to capture the files. The file type Express.Multer.File is used as this is used. 64 @UploadedFiles() files: Express.Multer.File[], 65 @Body() body: , 66 ) { 67 return files; 68 } 69 70 @Post('uploadFileFields') 71 // use this interceptor to specify more than one field containing files 72 @FastifyFileFieldsInterceptor( 73 // specify here the array of field name and maximum number of files allowed in this field. 74 [{ name: 'avatar', maxCount: 1 }, { name: 'background' maxCount: 1 }], 75 // here you can add any multer configuration. 76 { 77 storage: diskStorage({ 78 destination: './upload/single', // path where the file will be downloaded 79 filename: editFileName, // here you can put your own function to edit multer file name when saving to local disk 80 } 81 fileFilter: imageFileFilter, // here you can put your own function to filter the received files 82 } 83 ), 84 uploadFileFields( 85 @Req() req: Request, 86 // use this param decorator to capture the files. The file type Express.Multer.File is used as this is used. 87 @UploadedFiles() files: { avatar?: Express.Multer.File[], background?: Express.Multer.File[] }, 88 @Body() body: , 89 ) { 90 return files; 91 } 92 93}
A possible implementation of the editFileName
and imageFileFilter
methods is provided.
1import { extname } from 'path'; 2 3export const editFileName = ( 4 req: Request, 5 file: Express.Multer.File, 6 callback 7) => { 8 const name = file.originalname.split('.')[0]; 9 const fileExtName = extname(file.originalname); 10 const randomName = Array(4) 11 .fill(null) 12 .map(() => Math.round(Math.random() * 16).toString(16)) 13 .join(''); 14 callback(null, `${name}-${randomName}${fileExtName}`); 15}; 16 17export const imageFileFilter = ( 18 req: Request, 19 file: Express.Multer.File, 20 callback 21) => { 22 if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) { 23 return callback(new Error('Only image files are allowed!'), false); 24 } 25 callback(null, true); 26};
No vulnerabilities found.
No security vulnerabilities found.