File

src/storage/storage.controller.ts

Prefix

storage

Description

Storage Controller

Index

Methods

Methods

download
download(key: string)
Decorators :
@Get(':key')
Parameters :
Name Type Optional
key string No
Returns : any
upload
upload(user: TokenPayload, file: Express.Multer.File)
Decorators :
@UseInterceptors(undefined)
@Secured(['undefined'])
@ApiConsumes('multipart/form-data')
@ApiBody({description: 'List of cats', type: FileUploadDto})
@Post()

Upload files that belong to a tenant like images

Parameters :
Name Type Optional
user TokenPayload No
file Express.Multer.File No
Returns : any
import {
    Controller,
    Get,
    Param,
    Post,
    StreamableFile,
    UploadedFile,
    UseInterceptors,
} from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";
import { ApiBody, ApiConsumes } from "@nestjs/swagger";
import { Role } from "../auth/roles/role.enum";
import { Secured } from "../auth/secure.decorator";
import { Token, TokenPayload } from "../auth/token.decorator";
import { FileUploadDto } from "./dto/file-upload.dto";
import { FilesService } from "./files.service";

/**
 * Storage Controller
 */
@Controller("storage")
export class StorageController {
    /**
     * Constructor
     * @param filesService The files service
     */
    constructor(private filesService: FilesService) {}

    /**
     * Upload files that belong to a tenant like images
     * @param user
     * @param file
     * @returns
     */
    @UseInterceptors(FileInterceptor("file"))
    @Secured([Role.Issuances])
    @ApiConsumes("multipart/form-data")
    @ApiBody({
        description: "List of cats",
        type: FileUploadDto,
    })
    @Post()
    upload(
        @Token() user: TokenPayload,
        @UploadedFile() file: Express.Multer.File,
    ) {
        return this.filesService.saveUserUpload(user.entity!.id, file, true);
    }

    @Get(":key")
    download(@Param("key") key: string) {
        return this.filesService.getStream(key).then(
            (stream) =>
                new StreamableFile(stream.stream, {
                    disposition: "attachment",
                    type: stream.contentType,
                    length: stream.size,
                }),
        );
    }
}

results matching ""

    No results matching ""