微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自静态方法的NestJs FilesInterceptor fileFilter配置

如何解决来自静态方法的NestJs FilesInterceptor fileFilter配置

我有一种使用@UseInterceptors和@UploadedFile装饰器上传文件方法,在FileInterceptor中,我需要使用来自config服务的数据来设置fileFilter,因为我需要在env vars中外部化config。我想使用静态方法,但不知道是否可以访问实例AppConfigService。

我的控制器

    @Post('/upload')
    @UseInterceptors(FileInterceptor('file',{
        fileFilter: FileHelper.isExcelFormat
    }))
    async upload(@UploadedFile() file) {

        if (file?.size) {
            this.businesService.loadFile(file);
        }
    } 

带有静态方法的FileHelper类

static isExcelFormat(req,file,callback) {
    const configUpload = JSON.parse(this.appConfigService.get('configAttachmentFile')); // How can I do something like this to inject instance
    const ext = path.extname(file.originalname);

    if (![...configUpload.mimetypes].includes(file.mimetype)) {
        return callback(new Error(`[${file.originalname}] File type not supported,valid types: [${configUpload.extensions}].`),false);
    }

    return callback(null,true);
}

是否可以通过静态方法从我的AppConfigService中获取实例(如构造函数中的依赖项注入),或者可以在fileFilter中直接使用服务实例?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。