From d6512e1dcda8a5f87e5a06184e217e79fffba038 Mon Sep 17 00:00:00 2001 From: Washington Moreira Date: Fri, 21 Mar 2025 16:46:02 -0300 Subject: [PATCH 1/3] feature(screenshot-client): added screenshot endpoint --- src/controllers/clientController.js | 52 +++++++++++++++++++++++++++++ src/routes.js | 1 + 2 files changed, 53 insertions(+) diff --git a/src/controllers/clientController.js b/src/controllers/clientController.js index 399268e7..a4aec98c 100644 --- a/src/controllers/clientController.js +++ b/src/controllers/clientController.js @@ -772,6 +772,57 @@ const getLabels = async (req, res) => { } } +/** + * update the profile Picture of the session user + * @param {Object} req - The request object. + * @param {Object} res - The response object. + * @throws {Error} If there is an issue setting the profile picture, an error will be thrown. + */ +const getScreenshotImage = async (req, res) => { + // #swagger.summary = 'Get client screenshot image' + // #swagger.description = 'Screenshot of the client with the given session ID.' + try { + const sessionId = req.params.sessionId + const session = sessions.get(sessionId) + if (!session) { + return res.json({ success: false, message: 'session_not_found' }) + } + + const imgBase64Buffer = await session.pupPage.screenshot({ + encoding: 'base64', + type: 'png' + }); + const img = Buffer.from(imgBase64Buffer, 'base64'); + + /* #swagger.responses[200] = { + description: "Screenshot image.", + content: { + "image/png": {} + } + } + */ + + res.writeHead(200, { + 'Content-Type': 'image/png', + 'Content-Length': img.length + }); + + return res.end(img); + } catch (error) { + console.log('getScreenshotImage ERROR', error) + /* #swagger.responses[500] = { + description: "Server Failure.", + content: { + "application/json": { + schema: { "$ref": "#/definitions/ErrorResponse" } + } + } + } + */ + sendErrorResponse(res, 500, error.message) + } +} + /** * Adds or removes labels to/from chats. * @async @@ -1291,6 +1342,7 @@ module.exports = { getInviteInfo, getLabelById, getLabels, + getScreenshotImage, addOrRemoveLabels, isRegisteredUser, getNumberId, diff --git a/src/routes.js b/src/routes.js index 8f0ffdd6..cc8a9ec8 100644 --- a/src/routes.js +++ b/src/routes.js @@ -91,6 +91,7 @@ clientRouter.post('/unarchiveChat/:sessionId', [middleware.sessionNameValidation clientRouter.post('/unmuteChat/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.unmuteChat) clientRouter.post('/unpinChat/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.unpinChat) clientRouter.get('/getWWebVersion/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getWWebVersion) +clientRouter.get('/getScreenshotImage/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getScreenshotImage) /** * ================ From 96698f302f207a2b07f5c7a6112906f0d9990674 Mon Sep 17 00:00:00 2001 From: Washington Moreira Date: Fri, 21 Mar 2025 16:51:47 -0300 Subject: [PATCH 2/3] feature(screenshot-client): swagger updated --- swagger.json | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/swagger.json b/swagger.json index be3420e3..19fbc766 100644 --- a/swagger.json +++ b/swagger.json @@ -3529,6 +3529,80 @@ ] } }, + "/client/getScreenshotImage/{sessionId}": { + "get": { + "tags": [ + "Client" + ], + "summary": "Get client screenshot image", + "description": "Screenshot of the client with the given session ID.", + "parameters": [ + { + "name": "sessionId", + "in": "path", + "required": true, + "schema": { + "type": "string" + }, + "description": "Unique identifier for the session (alphanumeric and - allowed)", + "example": "f8377d8d-a589-4242-9ba6-9486a04ef80c" + } + ], + "responses": { + "200": { + "description": "Screenshot image.", + "content": { + "image/png": {} + } + }, + "403": { + "description": "Forbidden.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ForbiddenResponse" + } + } + } + }, + "404": { + "description": "Not Found.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/NotFoundResponse" + } + } + } + }, + "422": { + "description": "Unprocessable Entity.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponse" + } + } + } + }, + "500": { + "description": "Server Failure.", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/ErrorResponse" + } + } + } + } + }, + "security": [ + { + "apiKeyAuth": [] + } + ] + } + }, "/chat/getClassInfo/{sessionId}": { "post": { "tags": [ From 75c7fdbe69cfaff275dfb4c4a941e9320fd4d061 Mon Sep 17 00:00:00 2001 From: Washington Moreira Date: Fri, 21 Mar 2025 16:56:43 -0300 Subject: [PATCH 3/3] feature(screenshot-client): fixed page --- src/controllers/clientController.js | 4 ++++ src/routes.js | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/controllers/clientController.js b/src/controllers/clientController.js index a4aec98c..0d605636 100644 --- a/src/controllers/clientController.js +++ b/src/controllers/clientController.js @@ -788,6 +788,10 @@ const getScreenshotImage = async (req, res) => { return res.json({ success: false, message: 'session_not_found' }) } + if (!session.pupPage) { + return res.json({ success: false, message: 'page_not_ready' }) + } + const imgBase64Buffer = await session.pupPage.screenshot({ encoding: 'base64', type: 'png' diff --git a/src/routes.js b/src/routes.js index cc8a9ec8..618ff460 100644 --- a/src/routes.js +++ b/src/routes.js @@ -91,7 +91,7 @@ clientRouter.post('/unarchiveChat/:sessionId', [middleware.sessionNameValidation clientRouter.post('/unmuteChat/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.unmuteChat) clientRouter.post('/unpinChat/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.unpinChat) clientRouter.get('/getWWebVersion/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getWWebVersion) -clientRouter.get('/getScreenshotImage/:sessionId', [middleware.sessionNameValidation, middleware.sessionValidation], clientController.getScreenshotImage) +clientRouter.get('/getScreenshotImage/:sessionId', [middleware.sessionNameValidation], clientController.getScreenshotImage) /** * ================