diff --git a/src/game/game.controller.ts b/src/game/game.controller.ts index 092f484ffe1576be36a4a6e74d27bb28fbf0e4dc..238f54c56944bf52700eda70f31d00f5820a4b9c 100644 --- a/src/game/game.controller.ts +++ b/src/game/game.controller.ts @@ -10,12 +10,16 @@ import { UseInterceptors, ClassSerializerInterceptor, Delete, + UploadedFile, } from '@nestjs/common'; +import { FileInterceptor } from '@nestjs/platform-express'; +import { diskStorage } from 'multer'; +import { extname } from 'path'; import { GameService } from './game.service'; import { AuthGuard } from '../shared/auth.guard'; import { User } from '../user/user.decorator'; -import { GameDTO, FlagboxEventDTO, GameStateDTO } from './game.dto'; +import { GameDTO, FlagboxEventDTO, GameStateDTO, newGameDTO } from './game.dto'; import { ValidationPipe } from '../shared/validation.pipe'; import { Roles, GameStates } from '../shared/guard.decorator'; import { GameEntity } from './game.entity'; @@ -36,7 +40,7 @@ export class GameController { @Post('new') @UseGuards(new AuthGuard()) @UsePipes(new ValidationPipe()) - async newGame(@User('id') person, @Body() body: GameDTO) { + async newGame(@User('id') person, @Body() body: newGameDTO) { return this.gameservice.createNewGame(person, body); } @@ -101,4 +105,25 @@ export class GameController { async flagboxEvent(@Param('id') id: string, @Body() data: FlagboxEventDTO) { return this.gameservice.flagboxEvent(id, data); } + + @Post('upload') + @UseInterceptors( + FileInterceptor('image', { + storage: diskStorage({ + destination: './images', + filename: (req, file, cb) => { + // Generating a 32 random chars long string + const randomName = Array(32) + .fill(null) + .map(() => Math.round(Math.random() * 16).toString(16)) + .join(''); + //Calling the callback passing the random name generated with the original extension name + cb(null, `${randomName}${extname(file.originalname)}`); + }, + }), + }), + ) + uploadImage(@UploadedFile() image) { + return image; + } }