简体   繁体   English

在 controller nestjs 中注入一个简单的 class

[英]Injecting a simple class in controller nestjs

So basically I have a controller which is using a decorator 'UseInterceptor' in that I am passing a class which is calling a method but I feel I am not doing the right way it should be.所以基本上我有一个 controller 正在使用装饰器“UseInterceptor”,因为我正在传递一个正在调用方法的 class 但我觉得我没有按照应有的方式做正确的事情。 I need someone who could help me in defining a better way to maintain the singularity of the controller class.Here is the code我需要有人可以帮助我定义一种更好的方法来保持 controller class 的奇异性。这是代码

icons.controller.ts图标.controller.ts

var uploadOptions = new InterceptorClass(); ----> This bugs me and ---doesn't feel right way of doing.
const filesize = 512 * 512;
const driverFilesize = 512 * 512;

@Controller('icons')

export class IconsController {
    constructor(
        
        private iconsService: IconsService) { }
  @Post('/marker/')
    @UseGuards(CustomAccessGuard)
    @UseRoles({
        resource: ModuleNames.ICONS,
        action: "create",
        possession: "own"
    })
    @UseInterceptors(uploadOptions.uploadInterceptor(filesize)) ----> This is where I am using it.
    async uploadMarkerFile(
        @UploadedFile() file,
        @GetUser() user,
        @FileValidator() validator
    ) {

        let result = await this.iconsService.uploadFile(file, IconEntityType.MARKER, user);
        return new SuccessResponse(true, "Successfully Uploaded", result);
    }
}

file.service.ts For Reference.. file.service.ts供参考..

import { FileInterceptor } from "@nestjs/platform-express"
import { memoryStorage } from 'multer';
import { editFileName, imageFileFilter } from "../icons/utils/file-upload.utils";

export class InterceptorClass {

    constructor() { }

    uploadInterceptor(fileSize: number) {
        const UploadInterceptor = FileInterceptor('file', {
            storage: memoryStorage({
                // destination: './src/assets/marker/icons',
                filename: editFileName
            }),
            fileFilter: imageFileFilter,
            limits: {
                files: 1,
                fileSize: fileSize
            }
        });
        return UploadInterceptor;
    }
}

You could simply wrap the FileInterceptor decorator in your own decorator where you initialize it with the given config:您可以简单地将FileInterceptor装饰器包装在您自己的装饰器中,并使用给定的配置对其进行初始化:

// in file custom-file.interceptor.ts

import { editFileName, imageFileFilter } from "../icons/utils/file-upload.utils";

export function CustomFileInterceptor(fileSize) {
    const UploadInterceptor = FileInterceptor('file', {
        storage: memoryStorage({
            // destination: './src/assets/marker/icons',
            filename: editFileName
        }),
        fileFilter: imageFileFilter,
        limits: {
            files: 1,
            fileSize
        }
    });
    return UploadInterceptor;
}

You can then use it like this:然后你可以像这样使用它:

import {CustomFileInterceptor} from "./custom-file.interceptor";
...

@UseInterceptors(CustomFileInterceptor(512))
async uploadMarkerFile(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM