From caa46fb2dff316260f73a295b48f9a14338446ce Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Tue, 26 Nov 2024 09:35:15 -0300 Subject: [PATCH] fix(kb.gbapp): Auto Logo generation fixed. --- package.json | 2 +- packages/kb.gbapp/services/KBService.ts | 30 +++++++++++-------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 5dd323757..5046f5acd 100644 --- a/package.json +++ b/package.json @@ -154,7 +154,7 @@ "instagram-private-api": "1.46.1", "iso-639-1": "3.1.3", "isomorphic-fetch": "3.0.0", - "jimp": "^1.6.0", + "jimp": "1.6.0", "js-md5": "0.8.3", "json-schema-to-zod": "2.4.0", "jsqr": "^1.4.0", diff --git a/packages/kb.gbapp/services/KBService.ts b/packages/kb.gbapp/services/KBService.ts index 8ed7b7410..90fb91d80 100644 --- a/packages/kb.gbapp/services/KBService.ts +++ b/packages/kb.gbapp/services/KBService.ts @@ -53,7 +53,7 @@ import getColors from 'get-image-colors'; import { Document } from 'langchain/document'; import { RecursiveCharacterTextSplitter } from 'langchain/text_splitter'; import puppeteer, { Page } from 'puppeteer'; - +import {Jimp} from 'jimp'; import { GBDialogStep, GBLog, @@ -1065,22 +1065,18 @@ export class KBService implements IGBKBService { const baseUrl = page.url().split('/').slice(0, 3).join('/'); logo = logo.startsWith('https') ? logo : urlJoin(baseUrl, logo); - try { - const logoBinary = await page.goto(logo); - const buffer = await logoBinary.buffer(); - const logoFilename = path.basename(logo); - // TODO: sharp(buffer) - // .resize({ - // width: 48, - // height: 48, - // fit: 'inside', // Resize the image to fit within the specified dimensions - // withoutEnlargement: true // Don't enlarge the image if its dimensions are already smaller - // }) - // .toFile(path.join(logoPath, logoFilename)); - await min.core['setConfig'](min, 'Logo', logoFilename); - } catch (error) { - GBLogEx.debug(min, error); - } + const logoBinary = await page.goto(logo); + const buffer = await logoBinary.buffer(); + const logoFilename = path.basename(logo); + + // Replace sharp with jimp + const image = await Jimp.read(buffer); + await image.scaleToFit({w:48, h:48}); + packagePath = path.join(process.env.PWD, 'work', packagePath); + + const logoPath = path.join(packagePath, 'cache', logoFilename); + await (image as any).write(logoPath); + await min.core['setConfig'](min, 'Logo', logoFilename); } // Extract dominant colors from the screenshot