Merge branch 'master' into invalid-qr-patch

This commit is contained in:
Adhiraj Singh
2022-04-22 20:05:26 +05:30
2 changed files with 10 additions and 6 deletions

View File

@@ -485,7 +485,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
logger,
userJid,
// multi-device does not have this yet
getUrlInfo: text => getUrlInfo(text, { thumbnailWidth: linkPreviewImageThumbnailWidth }),
getUrlInfo: text => getUrlInfo(
text,
{ thumbnailWidth: linkPreviewImageThumbnailWidth, timeoutMs: 3_000 }
),
upload: waUploadToServer,
mediaCache: config.mediaCache,
...options,

View File

@@ -4,14 +4,15 @@ 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: number) => {
const stream = await getHttpStream(url)
const getCompressedJpegThumbnail = async(url: string, { thumbnailWidth, timeoutMs }: URLGenerationOptions) => {
const stream = await getHttpStream(url, { timeout: timeoutMs })
const result = await extractImageThumb(stream, thumbnailWidth)
return result
}
export type URLGenerationOptions = {
thumbnailWidth: number
timeoutMs: number
}
/**
@@ -22,17 +23,17 @@ export type URLGenerationOptions = {
*/
export const getUrlInfo = async(
text: string,
opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX }
opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX, timeoutMs: 3000 }
): Promise<WAUrlInfo | undefined> => {
try {
const { getLinkPreview } = await import('link-preview-js')
const info = await getLinkPreview(text)
const info = await getLinkPreview(text, { timeout: opts.timeoutMs })
if(info && 'title' in info) {
const [image] = info.images
const jpegThumbnail = image
? await getCompressedJpegThumbnail(image, opts.thumbnailWidth)
? await getCompressedJpegThumbnail(image, opts)
: undefined
return {