/
sigma
/
Acloud-server
Обзор
Документация
Войти
/
sigma
/
Acloud-server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/files/files.controller.ts
86 строк
2 KB
Александр Пимкин
first_commit
17 апр 2025, 11:44
17 апр 2025, 11:44
293b9b4
Код
Авторство
О чём код?
import { Controller, Delete, Get, MaxFileSizeValidator, NotFoundException, Param, ParseFilePipe, Post, Query, Res, UploadedFile, UseGuards, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { ApiBearerAuth, ApiBody, ApiConsumes, ApiTags } from '@nestjs/swagger'; import { Response } from 'express'; import { JwtAuthGuard } from '../auth/guards/jwt.guard'; import { UserId } from '../decorators/user-id.decorator'; import { FileType } from './entities/file.entity'; import { FilesService } from './files.service'; import { fileStorage } from './storage'; @Controller('files') @ApiTags('files') @UseGuards(JwtAuthGuard) @ApiBearerAuth() export class FilesController { constructor(private readonly filesService: FilesService) {} @Get() findAll(@UserId() userId: number, @Query('type') fileType: FileType) { return this.filesService.findAll(userId, fileType); } @Post() @UseInterceptors( FileInterceptor('file', { storage: fileStorage, }), ) @ApiConsumes('multipart/form-data') @ApiBody({ schema: { type: 'object', properties: { file: { type: 'string', format: 'binary', }, }, }, }) create( @UploadedFile( new ParseFilePipe({ validators: [new MaxFileSizeValidator({ maxSize: 1024 * 1024 * 5 })], }), ) file: Express.Multer.File, @UserId() userId: number, ) { return this.filesService.create(file, userId); } @Delete() remove(@UserId() userId: number, @Query('ids') ids: string) { // files?ids=1,2,7,8 return this.filesService.remove(userId, ids); } @Get(':id') async downloadFile(@Param('id') id: number, @Res() res: Response) { try { return await this.filesService.download(id, res); } catch (error) { if (error instanceof NotFoundException) { throw error; // Re-throw NotFoundException (404) } // Handle other errors (e.g., database errors) // You might want to log them here throw new Error('Failed to download file'); // Or a more specific error } } }