From 4f29f82f22ca6bdceab3388b68b49dd135d09769 Mon Sep 17 00:00:00 2001 From: allburov Date: Wed, 21 Oct 2020 12:20:58 +0700 Subject: [PATCH] Add GET /screenshot --- ...ngController.ts => chatting.controller.ts} | 0 src/api/screenshot.controller.ts | 28 +++++++++++++++++++ src/app.module.ts | 5 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) rename src/api/{chattingController.ts => chatting.controller.ts} (100%) create mode 100644 src/api/screenshot.controller.ts diff --git a/src/api/chattingController.ts b/src/api/chatting.controller.ts similarity index 100% rename from src/api/chattingController.ts rename to src/api/chatting.controller.ts diff --git a/src/api/screenshot.controller.ts b/src/api/screenshot.controller.ts new file mode 100644 index 00000000..4d562d34 --- /dev/null +++ b/src/api/screenshot.controller.ts @@ -0,0 +1,28 @@ +import {Controller, Get, Inject, Res} from '@nestjs/common'; +import {ApiTags} from "@nestjs/swagger"; +import {Whatsapp} from "venom-bot"; +import {Readable} from "stream"; +import {Response} from 'express'; + + +@Controller('api') +@ApiTags('screenshot') +export class ScreenshotController { + constructor(@Inject('WHATSAPP') private whatsapp: Whatsapp) { + } + + @Get('/screenshot') + async screenshot(@Res() res: Response,) { + const buffer = await this.whatsapp.page.screenshot(); + const stream = new Readable(); + stream.push(buffer); + stream.push(null); + + res.set({ + 'Content-Type': 'image/png', + 'Content-Length': buffer.length, + }); + stream.pipe(res) + } +} + diff --git a/src/app.module.ts b/src/app.module.ts index 37984aad..b054329a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,11 +1,12 @@ import {Module} from '@nestjs/common'; -import {ChattingController} from './api/chattingController'; +import {ChattingController} from './api/chatting.controller'; import {DeviceController} from "./api/device.controller"; import {whatsappProvider, WhatsappService} from "./whatsapp.service"; +import {ScreenshotController} from "./api/screenshot.controller"; @Module({ imports: [], - controllers: [ChattingController, DeviceController], + controllers: [ChattingController, DeviceController, ScreenshotController], providers: [whatsappProvider, WhatsappService], }) export class AppModule {