mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
feat: add url options to link preview gen
This commit is contained in:
@@ -14,6 +14,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
logger,
|
||||
linkPreviewImageThumbnailWidth,
|
||||
generateHighQualityLinkPreview,
|
||||
options: axiosOptions,
|
||||
patchMessageBeforeSending,
|
||||
} = config
|
||||
const sock = makeGroupsSocket(config)
|
||||
@@ -623,12 +624,15 @@ export const makeMessagesSocket = (config: SocketConfig) => {
|
||||
text,
|
||||
{
|
||||
thumbnailWidth: linkPreviewImageThumbnailWidth,
|
||||
timeoutMs: 3_000,
|
||||
fetchOpts: {
|
||||
timeout: 3_000,
|
||||
...axiosOptions || { }
|
||||
},
|
||||
logger,
|
||||
uploadImage: generateHighQualityLinkPreview
|
||||
? waUploadToServer
|
||||
: undefined
|
||||
},
|
||||
logger
|
||||
),
|
||||
upload: waUploadToServer,
|
||||
mediaCache: config.mediaCache,
|
||||
|
||||
@@ -6,16 +6,25 @@ import { extractImageThumb, getHttpStream } from './messages-media'
|
||||
const THUMBNAIL_WIDTH_PX = 192
|
||||
|
||||
/** Fetches an image and generates a thumbnail for it */
|
||||
const getCompressedJpegThumbnail = async(url: string, { thumbnailWidth, timeoutMs }: URLGenerationOptions) => {
|
||||
const stream = await getHttpStream(url, { timeout: timeoutMs })
|
||||
const getCompressedJpegThumbnail = async(
|
||||
url: string,
|
||||
{ thumbnailWidth, fetchOpts }: URLGenerationOptions
|
||||
) => {
|
||||
const stream = await getHttpStream(url, fetchOpts)
|
||||
const result = await extractImageThumb(stream, thumbnailWidth)
|
||||
return result
|
||||
}
|
||||
|
||||
export type URLGenerationOptions = {
|
||||
thumbnailWidth: number
|
||||
timeoutMs: number
|
||||
fetchOpts: {
|
||||
/** Timeout in ms */
|
||||
timeout: number
|
||||
proxyUrl?: string
|
||||
headers?: { [key: string]: string }
|
||||
}
|
||||
uploadImage?: WAMediaUploadFunction
|
||||
logger?: Logger
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -26,8 +35,10 @@ export type URLGenerationOptions = {
|
||||
*/
|
||||
export const getUrlInfo = async(
|
||||
text: string,
|
||||
opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX, timeoutMs: 3000 },
|
||||
logger?: Logger
|
||||
opts: URLGenerationOptions = {
|
||||
thumbnailWidth: THUMBNAIL_WIDTH_PX,
|
||||
fetchOpts: { timeout: 3000 }
|
||||
},
|
||||
): Promise<WAUrlInfo | undefined> => {
|
||||
try {
|
||||
const { getLinkPreview } = await import('link-preview-js')
|
||||
@@ -36,7 +47,7 @@ export const getUrlInfo = async(
|
||||
previewLink = 'https://' + previewLink
|
||||
}
|
||||
|
||||
const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs })
|
||||
const info = await getLinkPreview(previewLink, opts.fetchOpts)
|
||||
if(info && 'title' in info) {
|
||||
const [image] = info.images
|
||||
|
||||
@@ -63,7 +74,7 @@ export const getUrlInfo = async(
|
||||
? (await getCompressedJpegThumbnail(image, opts)).buffer
|
||||
: undefined
|
||||
} catch(error) {
|
||||
logger?.debug(
|
||||
opts.logger?.debug(
|
||||
{ err: error.stack, url: previewLink },
|
||||
'error in generating thumbnail'
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user