Reutan
Reutan

Reputation: 53

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 :)

Upvotes: 2

Views: 4427

Answers (2)

hellowangit
hellowangit

Reputation: 61

As Multer Module is not a global module, so the setting in the app.module.ts not work! It should set at the module which used the file upload.

Upvotes: 0

Kim Kern
Kim Kern

Reputation: 60357

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

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

Upvotes: 3

Related Questions