node.jsfile-uploadinterceptormulternestjs

NestJS - How/Where to configure FileInterceptor with async way


As the documentation says: we can have an async configuration for the file interceptor.

I would like to use this to use my ConfigService for the upload directory (who is different by environment). But I don't know where to write this async configuration.

The documentation give us an example to set the configuration but I don't know how to integrated this to my project.

I have check the official documentation and especially the Techniques/File Upload and Overview/Middleware. I have tested some implementation but my configuration seems to be never used.

I use this method to configue Multer:

MulterModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    storage: diskStorage({
      destination: configService.downloadFolder,
      filename: (req, file, cb) => {
        const randomName = Array(32)
          .fill(null)
          .map(() => Math.round(Math.random() * 16).toString(16))
          .join('')
        return cb(null, `${randomName}${extname(file.originalname)}`)
      }
    })
  }),
  inject: [ConfigService]
})

Do you have any idea how to integrate this configuration?

Thank you for your help :)


Solution

  • You have to import MulterModule in your AppModule to set the default configuration:

    @Module({
      imports: [
        MulterModule.registerAsync(...)
      ],
    })
    export class AppModule{}