import { Controller, Get, Param, Post, StreamableFile, UploadedFile, UseGuards, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { memoryStorage } from 'multer'; import type { AuthUser } from '../auth/auth.types'; import { CurrentUser } from '../auth/decorators/current-user.decorator'; import { Roles } from '../auth/decorators/roles.decorator'; import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; import { RolesGuard } from '../auth/guards/roles.guard'; import { FilesService } from './files.service'; @Controller('files') @UseGuards(JwtAuthGuard, RolesGuard) @Roles('ADMIN', 'MANAGER', 'OPERATOR') export class FilesController { constructor(private readonly files: FilesService) {} @Get() list(@CurrentUser() user: AuthUser) { return this.files.list(user); } @Post() @UseInterceptors( FileInterceptor('file', { storage: memoryStorage(), limits: { fileSize: 8 * 1024 * 1024 }, }), ) create(@CurrentUser() user: AuthUser, @UploadedFile() file: Express.Multer.File) { return this.files.create(user, file); } @Get(':id/download') async download(@CurrentUser() user: AuthUser, @Param('id') id: string) { const file = await this.files.download(user, id); return new StreamableFile(file.stream, { type: file.mimeType, disposition: `attachment; filename="${file.name.replace(/"/g, '')}"`, }); } }