File

src/storage/storage.controller.ts

Prefix

storage

Description

Storage Controller

Index

Methods

Methods

download
download(key: string)
Decorators :
@Get(':key')

Get a file and stream it

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({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,
    NotFoundException,
    Param,
    Post,
    StreamableFile,
    UploadedFile,
    UseInterceptors,
} from "@nestjs/common";
import { FileInterceptor } from "@nestjs/platform-express";
import { ApiBody, ApiConsumes, ApiTags } 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
 */
@ApiTags("Storage")
@Controller("storage")
export class StorageController {
    /**
     * Constructor
     * @param filesService The files service
     */
    constructor(private readonly 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({
        type: FileUploadDto,
    })
    @Post()
    upload(
        @Token() user: TokenPayload,
        @UploadedFile() file: Express.Multer.File,
    ) {
        return this.filesService.saveUserUpload(user.entity!.id, file, true);
    }

    /**
     * Get a file and stream it
     */
    @Get(":key")
    download(@Param("key") key: string) {
        return this.filesService
            .getStream(key)
            .then(
                (stream) =>
                    new StreamableFile(stream.stream, {
                        //TODO: check if it should be attachment or not
                        disposition: "attachment",
                        type: stream.contentType,
                        length: stream.size,
                    }),
            )
            .catch(() => {
                throw new NotFoundException();
            });
    }
}

results matching ""

    No results matching ""