feat: add url options to link preview gen

This commit is contained in:
Adhiraj Singh
2022-11-17 11:08:01 +05:30
parent 00d8cf4270
commit b33c753928
2 changed files with 24 additions and 9 deletions

View File

@@ -14,6 +14,7 @@ export const makeMessagesSocket = (config: SocketConfig) => {
logger, logger,
linkPreviewImageThumbnailWidth, linkPreviewImageThumbnailWidth,
generateHighQualityLinkPreview, generateHighQualityLinkPreview,
options: axiosOptions,
patchMessageBeforeSending, patchMessageBeforeSending,
} = config } = config
const sock = makeGroupsSocket(config) const sock = makeGroupsSocket(config)
@@ -623,12 +624,15 @@ export const makeMessagesSocket = (config: SocketConfig) => {
text, text,
{ {
thumbnailWidth: linkPreviewImageThumbnailWidth, thumbnailWidth: linkPreviewImageThumbnailWidth,
timeoutMs: 3_000, fetchOpts: {
timeout: 3_000,
...axiosOptions || { }
},
logger,
uploadImage: generateHighQualityLinkPreview uploadImage: generateHighQualityLinkPreview
? waUploadToServer ? waUploadToServer
: undefined : undefined
}, },
logger
), ),
upload: waUploadToServer, upload: waUploadToServer,
mediaCache: config.mediaCache, mediaCache: config.mediaCache,

View File

@@ -6,16 +6,25 @@ import { extractImageThumb, getHttpStream } from './messages-media'
const THUMBNAIL_WIDTH_PX = 192 const THUMBNAIL_WIDTH_PX = 192
/** Fetches an image and generates a thumbnail for it */ /** Fetches an image and generates a thumbnail for it */
const getCompressedJpegThumbnail = async(url: string, { thumbnailWidth, timeoutMs }: URLGenerationOptions) => { const getCompressedJpegThumbnail = async(
const stream = await getHttpStream(url, { timeout: timeoutMs }) url: string,
{ thumbnailWidth, fetchOpts }: URLGenerationOptions
) => {
const stream = await getHttpStream(url, fetchOpts)
const result = await extractImageThumb(stream, thumbnailWidth) const result = await extractImageThumb(stream, thumbnailWidth)
return result return result
} }
export type URLGenerationOptions = { export type URLGenerationOptions = {
thumbnailWidth: number thumbnailWidth: number
timeoutMs: number fetchOpts: {
/** Timeout in ms */
timeout: number
proxyUrl?: string
headers?: { [key: string]: string }
}
uploadImage?: WAMediaUploadFunction uploadImage?: WAMediaUploadFunction
logger?: Logger
} }
/** /**
@@ -26,8 +35,10 @@ export type URLGenerationOptions = {
*/ */
export const getUrlInfo = async( export const getUrlInfo = async(
text: string, text: string,
opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX, timeoutMs: 3000 }, opts: URLGenerationOptions = {
logger?: Logger thumbnailWidth: THUMBNAIL_WIDTH_PX,
fetchOpts: { timeout: 3000 }
},
): Promise<WAUrlInfo | undefined> => { ): Promise<WAUrlInfo | undefined> => {
try { try {
const { getLinkPreview } = await import('link-preview-js') const { getLinkPreview } = await import('link-preview-js')
@@ -36,7 +47,7 @@ export const getUrlInfo = async(
previewLink = 'https://' + previewLink previewLink = 'https://' + previewLink
} }
const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs }) const info = await getLinkPreview(previewLink, opts.fetchOpts)
if(info && 'title' in info) { if(info && 'title' in info) {
const [image] = info.images const [image] = info.images
@@ -63,7 +74,7 @@ export const getUrlInfo = async(
? (await getCompressedJpegThumbnail(image, opts)).buffer ? (await getCompressedJpegThumbnail(image, opts)).buffer
: undefined : undefined
} catch(error) { } catch(error) {
logger?.debug( opts.logger?.debug(
{ err: error.stack, url: previewLink }, { err: error.stack, url: previewLink },
'error in generating thumbnail' 'error in generating thumbnail'
) )