diff --git a/src/workshop/workshop.controller.ts b/src/workshop/workshop.controller.ts index 8f80b26..e16f149 100644 --- a/src/workshop/workshop.controller.ts +++ b/src/workshop/workshop.controller.ts @@ -13,10 +13,10 @@ import { Body, UseGuards, ValidationPipe, - UsePipes + UsePipes, } from '@nestjs/common'; -import type { Response, Request } from 'express' -import { BandageService } from "./bandage.service"; +import type { Response, Request } from 'express'; +import { BandageService } from './bandage.service'; import { SkipThrottle, Throttle } from '@nestjs/throttler'; import { AuthGuard } from 'src/guards/auth.guard'; import * as sharp from 'sharp'; @@ -25,40 +25,49 @@ import { Auth } from 'src/decorators/auth.decorator'; import { CreateBandageDto } from './dto/createBandage.dto'; import { EditBandageDto } from './dto/editBandage.dto'; import { RequestSession } from 'src/common/bandage_response'; -import { EditQueryDTO, WidthQueryDTO, WorkshopSearchQueryDTO } from 'src/workshop/dto/queries.dto'; +import { + EditQueryDTO, + WidthQueryDTO, + WorkshopSearchQueryDTO, +} from 'src/workshop/dto/queries.dto'; import { SetQueryDTO } from 'src/user/dto/queries.dto'; @Controller() @UseGuards(AuthGuard) export class WorkshopController { constructor(private readonly bandageService: BandageService) { } - @Get("/workshop") + @Get('/workshop') @Auth(AuthEnum.Weak) @UsePipes(new ValidationPipe({ whitelist: true, transform: true })) async bandages( @Req() request: RequestSession, @Res() res: Response, - @Query() query: WorkshopSearchQueryDTO + @Query() query: WorkshopSearchQueryDTO, ): Promise { /* get list of works */ - res.status(200).send(await this.bandageService.getBandages(request.session, - query.take ?? 20, - query.page ?? 0, - query.search, - query.filters, - query.sort - )); + res + .status(200) + .send( + await this.bandageService.getBandages( + request.session, + query.take ?? 20, + query.page ?? 0, + query.search, + query.filters, + query.sort, + ), + ); } @Throttle({ default: { limit: 5, ttl: 60000 } }) - @Post("/workshop") + @Post('/workshop') @Auth(AuthEnum.Strict) @UsePipes(new ValidationPipe({ whitelist: true })) async create_bandage( @Req() request: RequestSession, @Res() res: Response, - @Body() body: CreateBandageDto + @Body() body: CreateBandageDto, ): Promise { /* create work */ @@ -66,12 +75,14 @@ export class WorkshopController { res.status(HttpStatus.BAD_REQUEST).send({ statusCode: 400, message: 'Invalid Body', - message_ru: 'Неправильное тело запроса' + message_ru: 'Неправильное тело запроса', }); return; } - const validate_result = await this.bandageService.validateBandage(body.base64); + const validate_result = await this.bandageService.validateBandage( + body.base64, + ); if (validate_result.statusCode !== 200) { res.status(validate_result.statusCode).send(validate_result); return; @@ -82,12 +93,15 @@ export class WorkshopController { res.status(400).send({ statusCode: 400, message: 'Invalid Body', - message_ru: 'Неправильное тело запроса' + message_ru: 'Неправильное тело запроса', }); return; } - const validate_result_slim = await this.bandageService.validateBandage(body.base64_slim, validate_result.height as number); + const validate_result_slim = await this.bandageService.validateBandage( + body.base64_slim, + validate_result.height as number, + ); if (validate_result.statusCode !== 200) { res.status(validate_result_slim.statusCode).send(validate_result_slim); return; @@ -98,13 +112,13 @@ export class WorkshopController { res.status(data.statusCode).send(data); } - @Get("/workshop/:id") + @Get('/workshop/:id') @SkipThrottle() @Auth(AuthEnum.Weak) async getBandage( @Param('id') id: string, @Req() request: RequestSession, - @Res() res: Response + @Res() res: Response, ): Promise { /* get bandage by external id (internal endpoint) */ @@ -112,7 +126,7 @@ export class WorkshopController { res.status(403).send({ statusCode: 403, message: 'Forbidden', - message_ru: 'У вас нет прав для выполнения этого действия', + message_ru: 'Доступ запрещен', }); return; } @@ -120,12 +134,12 @@ export class WorkshopController { res.status(data.statusCode).send(data); } - @Get("/workshop/:id/info") + @Get('/workshop/:id/info') @SkipThrottle() async getBandageOg( @Param('id') id: string, @Req() request: Request, - @Res() res: Response + @Res() res: Response, ): Promise { /* get bandage info by external id (internal endpoint) */ @@ -133,7 +147,7 @@ export class WorkshopController { res.status(403).send({ statusCode: 403, message: 'Forbidden', - message_ru: 'У вас нет прав для выполнения этого действия', + message_ru: 'Доступ запрещен', }); return; } @@ -141,14 +155,14 @@ export class WorkshopController { res.status(data.statusCode).send(data); } - @Get(["/workshop/:id/as_image", "/workshop/:id/og"]) + @Get(['/workshop/:id/as_image', '/workshop/:id/og']) @Auth(AuthEnum.Weak) @UsePipes(new ValidationPipe({ whitelist: true, transform: true })) async getBandageImage( @Param('id') id: string, @Req() request: RequestSession, @Res({ passthrough: true }) res: Response, - @Query() query: WidthQueryDTO + @Query() query: WidthQueryDTO, ): Promise { /* get bandage image render (for OpenGraph) */ @@ -159,7 +173,7 @@ export class WorkshopController { res.status(data.statusCode).send(data); return; } - const bandage_buff = Buffer.from(data.data.base64, "base64"); + const bandage_buff = Buffer.from(data.data.base64, 'base64'); const metadata = await sharp(bandage_buff).metadata(); const original_width = metadata.width as number; const original_height = metadata.height as number; @@ -173,39 +187,49 @@ export class WorkshopController { width: width, height: height / 2, channels: 4, - background: { r: 0, g: 0, b: 0, alpha: 0 } - } + background: { r: 0, g: 0, b: 0, alpha: 0 }, + }, }).png(); const firstLayer = await sharp(bandage_buff) - .extract({ left: 0, top: (original_height / 2), width: original_width, height: original_height / 2 }) + .extract({ + left: 0, + top: original_height / 2, + width: original_width, + height: original_height / 2, + }) .resize(width, height / 2, { kernel: sharp.kernel.nearest }) .png() .toBuffer(); const secondLayer = await sharp(bandage_buff) - .extract({ left: 0, top: 0, width: original_width, height: original_height / 2 }) + .extract({ + left: 0, + top: 0, + width: original_width, + height: original_height / 2, + }) .resize(width, height / 2, { kernel: sharp.kernel.nearest }) .png() .toBuffer(); bandage.composite([ { input: firstLayer, top: 0, left: 0, blend: 'over' }, - { input: secondLayer, top: 0, left: 0, blend: 'over' } + { input: secondLayer, top: 0, left: 0, blend: 'over' }, ]); res.setHeader('Content-Type', 'image/png'); return new StreamableFile(await bandage.toBuffer()); } - @Put("/workshop/:id") + @Put('/workshop/:id') @Auth(AuthEnum.Strict) @UsePipes(new ValidationPipe({ whitelist: true })) async editBandage( @Param('id') id: string, @Req() request: RequestSession, @Res() res: Response, - @Body() body: EditBandageDto + @Body() body: EditBandageDto, ) { /* edit bandage info */ @@ -213,69 +237,81 @@ export class WorkshopController { res.status(HttpStatus.BAD_REQUEST).send({ statusCode: 400, message: 'Invalid Body', - message_ru: 'Неправильное тело запроса' + message_ru: 'Неправильное тело запроса', }); return; } - const data = await this.bandageService.updateBandage(id, body, request.session); + const data = await this.bandageService.updateBandage( + id, + body, + request.session, + ); res.status(data.statusCode).send(data); - } - @Put("/workshop/:id/archive") + @Put('/workshop/:id/archive') @Auth(AuthEnum.Strict) async archiveBandage( @Param('id') id: string, @Req() request: RequestSession, - @Res() res: Response + @Res() res: Response, ) { /* Archive bandage */ const data = await this.bandageService.archiveBandage(request.session, id); res.status(data.statusCode).send(data); - } - @Delete("/workshop/:id") + @Delete('/workshop/:id') @Auth(AuthEnum.Strict) async deleteBandage( @Param('id') id: string, @Req() request: RequestSession, - @Res() res: Response + @Res() res: Response, ) { /* delete bandage by external id */ const data = await this.bandageService.deleteBandage(request.session, id); res.status(data.statusCode).send(data); - } - @Get("/categories") + @Get('/categories') @Auth(AuthEnum.Weak) @UsePipes(new ValidationPipe({ whitelist: true, transform: true })) async categories( @Req() request: RequestSession, @Res() res: Response, - @Query() query: EditQueryDTO + @Query() query: EditQueryDTO, ): Promise { /* get list of categories */ - res.status(200).send(await this.bandageService.getCategories(query.for_edit === 'true', request.session)); + res + .status(200) + .send( + await this.bandageService.getCategories( + query.for_edit === 'true', + request.session, + ), + ); } - @Put("/star/:id") + @Put('/star/:id') @Auth(AuthEnum.Strict) @UsePipes(new ValidationPipe({ whitelist: true, transform: true })) async setStar( @Param('id') id: string, @Query() query: SetQueryDTO, @Req() request: RequestSession, - @Res() res: Response + @Res() res: Response, ): Promise { /* set star to work by work external id */ - const data = await this.bandageService.setStar(request.session, query.set === "true", id); + const data = await this.bandageService.setStar( + request.session, + query.set === 'true', + id, + ); res.status(data.statusCode).send(data); } @@ -283,10 +319,10 @@ export class WorkshopController { async getCount(@Res() res: Response) { const count = await this.bandageService.getBandagesCount(); res.status(200).send({ - "schemaVersion": 1, - "label": 'Bandages Count', - "message": count.toString(), - "color": 'green' + schemaVersion: 1, + label: 'Bandages Count', + message: count.toString(), + color: 'green', }); } -} \ No newline at end of file +}