NestJS - How/Where 以异步方式配置 FileInterceptor

NestJS - How/Where to configure FileInterceptor with async way

如文档所述:我们可以为文件拦截器进行异步配置。

我想用这个把我的ConfigService用于上传目录(因环境而异)。 但是我不知道在哪里写这个异步配置。

文档为我们提供了设置配置的示例,但我不知道如何将其集成到我的项目中。

我查看了官方文档,尤其是 Techniques/File UploadOverview/Middleware。我已经测试了一些实现,但我的配置似乎从未被使用过。

我用这个方法配置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]
})

你知道如何集成这个配置吗?

感谢您的帮助:)

您必须在 AppModule 中导入 MulterModule 以设置默认配置:

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