mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: generate high quality thumbs on link preview
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { Logger } from 'pino'
|
||||
import { WAUrlInfo } from '../Types'
|
||||
import { WAMediaUploadFunction, WAUrlInfo } from '../Types'
|
||||
import { prepareWAMessageMedia } from './messages'
|
||||
import { extractImageThumb, getHttpStream } from './messages-media'
|
||||
|
||||
const THUMBNAIL_WIDTH_PX = 192
|
||||
@@ -14,6 +15,7 @@ const getCompressedJpegThumbnail = async(url: string, { thumbnailWidth, timeoutM
|
||||
export type URLGenerationOptions = {
|
||||
thumbnailWidth: number
|
||||
timeoutMs: number
|
||||
uploadImage?: WAMediaUploadFunction
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -38,25 +40,37 @@ export const getUrlInfo = async(
|
||||
if(info && 'title' in info) {
|
||||
const [image] = info.images
|
||||
|
||||
let jpegThumbnail: Buffer | undefined = undefined
|
||||
try {
|
||||
jpegThumbnail = image
|
||||
? (await getCompressedJpegThumbnail(image, opts)).buffer
|
||||
: undefined
|
||||
} catch(error) {
|
||||
logger?.debug(
|
||||
{ err: error.stack, url: previewLink },
|
||||
'error in generating thumbnail'
|
||||
)
|
||||
}
|
||||
|
||||
return {
|
||||
const urlInfo: WAUrlInfo = {
|
||||
'canonical-url': info.url,
|
||||
'matched-text': text,
|
||||
title: info.title,
|
||||
description: info.description,
|
||||
jpegThumbnail
|
||||
}
|
||||
|
||||
if(opts.uploadImage) {
|
||||
const { imageMessage } = await prepareWAMessageMedia(
|
||||
{ image: { url: image } },
|
||||
{ upload: opts.uploadImage, mediaTypeOverride: 'thumbnail-link' }
|
||||
)
|
||||
urlInfo.jpegThumbnail = imageMessage?.jpegThumbnail
|
||||
? Buffer.from(imageMessage.jpegThumbnail)
|
||||
: undefined
|
||||
urlInfo.highQualityThumbnail = imageMessage || undefined
|
||||
console.log(urlInfo)
|
||||
} else {
|
||||
try {
|
||||
urlInfo.jpegThumbnail = image
|
||||
? (await getCompressedJpegThumbnail(image, opts)).buffer
|
||||
: undefined
|
||||
} catch(error) {
|
||||
logger?.debug(
|
||||
{ err: error.stack, url: previewLink },
|
||||
'error in generating thumbnail'
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return urlInfo
|
||||
}
|
||||
} catch(error) {
|
||||
if(!error.message.includes('receive a valid')) {
|
||||
|
||||
Reference in New Issue
Block a user