1.1.5 • Published 2 years ago

nestjs-file-mimetype-filter v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

npm version npm download by month

Description

Nestjs file mimetype filter framework where you can filter the filemimetype of the file you are going to upload.

Install

$ npm install nestjs-file-mimetype-filter

Quick Start

# For import
$ import { ApiPdfFile,ApiImageFile,CustomFile } from 'nestjs-file-mimetype-filter';

Example Usage

String of 'file' is the parameter name for uploadedFile from body

@ApiPdfFile({fileName:'file'})
  @Post()
  async create(@UploadedFile() file: Express.Multer.File, @Body() body: CreateConsentFormDto) {
    if (!file) {
      throw new UnprocessableEntityException('File is required!');
  }

Access Multer Options

You can access the multer options with localOptions parameter(Example) More about Multer Options

@ApiPdfFile({fileName:'file', localOptions:{limits:{fileSize:1000}}})
  @Post()
  async create(@UploadedFile() file: Express.Multer.File, @Body() body: CreateConsentFormDto) {
    if (!file) {
      throw new UnprocessableEntityException('File is required!');
  }

You can combine with Swagger if you want

@ApiPdfFile({fileName:'file', apiBodyType:ConsentFormBody })
@Post()
async create(@UploadedFile() file: Express.Multer.File, @Body() body:CreateConsentFormDto) {
  if (!file) {
    throw new UnprocessableEntityException('File is required!');
  }

ConsentFormBody Example

  export function ConsentFormBody() {
    return applyDecorators(
      ApiBody({
        schema: {
          type: 'object',
          properties: {
            name: { type: 'string' },
            file: {
              type: 'string',
              format: 'binary',
            },
            fileId: { type: 'string', nullable: true },
          },
        },
      })
    );
  }

Other Usage

You can use for filtering images

@ApiImageFile({fileName:'file'})

or Custom fileMimeType,You can select multiple types for you want

 @CustomFile(
    {
      fileName:'file',
      type:{allowedFiles:['image','pdf']},
    })
#or you can block files type you want
@CustomFile(
    {
      fileName:'file',
      type:{deniedFiles:['image','pdf']},
    })
#Don't use allowedFiles and deniedFiles together!

Stay in touch

  • Author - Mustafa Kendigüzel

License

Nestjs-file-mimetype-filter MIT licensed.