npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-fastify-multer

v2.0.0

Published

The objective of this module is to provide a package with filters already prepared to work with 'fastify-multer'.

Downloads

3,359

Readme

nest-fastify-multer

Install

npm i nest-fastify-multer

External Dependency Packages

Before installing and using this package you must make sure you have fastify-multer installed.

npm i fastify-multer

You must configure this in the main.js of your Nest application.

import { NestFactory } from '@nestjs/core';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { contentParser } from 'fastify-multer';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter()
  );
  await app.register(contentParser);
  await app.listen(3000);
}

bootstrap()

Usage

import {
  Body,
  Controller,
  Post,
  Req, 
  UploadedFile,
} from '@nestjs/common';

// import the filters to use from the module.
import {
  FastifyFileInterceptor,
  FastifyFilesInterceptor,
  FastifyFileFieldsInterceptor,
} from 'nest-fastify-multer';

// import multer to use your methods
import { diskStorage } from 'multer';

@Controller('image')
export class ImageController {

  @Post('uploadFile')
  // use this interceptor to specify one file
  @FastifyFileInterceptor(
  // fileName
    'avatar',
  // here you can add any multer configuration.
   {
      storage: diskStorage({
        destination: './upload/single', // path where the file will be downloaded
        filename: editFileName, // here you can put your own function to edit multer file name when saving to local disk
      }),
      fileFilter: imageFileFilter, // here you can put your own function to filter the received files
    }
  )
  uploadFile(
    @Req() req: Request,
    // use this param decorator to capture the file. The file type Express.Multer.File is used as this is used.
    @UploadedFiles() file: Express.Multer.File,
  ) {
    return file;
  }

  @Post('uploadFiles')
  // use this interceptor to specify more than one file
  @FastifyFilesInterceptor(
  // fileName
    'avatar',
  // maxCount  
     1,
  // here you can add any multer configuration.
   {
      storage: diskStorage({
        destination: './upload/single', // path where the file will be downloaded
        filename: editFileName, // here you can put your own function to edit multer file name when saving to local disk
      }),
      fileFilter: imageFileFilter, // here you can put your own function to filter the received files
    }
  )
  uploadFiles(
    @Req() req: Request,
    // use this param decorator to capture the files. The file type Express.Multer.File is used as this is used.
    @UploadedFile() files: Express.Multer.File[],
  ) {
    return files;
  }
  
  @Post('uploadFileFields')
  // use this interceptor to specify more than one field containing files
  @FastifyFileFieldsInterceptor(
  // specify here the array of field name and maximum number of files allowed in this field.  
    [{ name: 'avatar', maxCount: 1 }, { name: 'background', maxCount: 1 }],
  // here you can add any multer configuration.
   {
      storage: diskStorage({
        destination: './upload/single', // path where the file will be downloaded
        filename: editFileName, // here you can put your own function to edit multer file name when saving to local disk
      }),
      fileFilter: imageFileFilter, // here you can put your own function to filter the received files
    }
  )
  uploadFileFields(
    @Req() req: Request,
    // use this param decorator to capture the files. The file type Express.Multer.File is used as this is used.
    @UploadedFile() files: { avatar?: Express.Multer.File[], background?: Express.Multer.File[] },
  ) {
    return files;
  }

}

A possible implementation of the editFileName and imageFileFilter methods is provided.

import { extname } from 'path';

export const editFileName = (
  req: Request,
  file: Express.Multer.File,
  callback
) => {
  const name = file.originalname.split('.')[0];
  const fileExtName = extname(file.originalname);
  const randomName = Array(4)
    .fill(null)
    .map(() => Math.round(Math.random() * 16).toString(16))
    .join('');
  callback(null, `${name}-${randomName}${fileExtName}`);
};

export const imageFileFilter = (
  req: Request,
  file: Express.Multer.File,
  callback
) => {
  if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
    return callback(new Error('Only image files are allowed!'), false);
  }
  callback(null, true);
};