Path: blob/master/node_modules/@adiwajshing/baileys/lib/Utils/link-preview.js
1129 views
"use strict";1var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {2if (k2 === undefined) k2 = k;3var desc = Object.getOwnPropertyDescriptor(m, k);4if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {5desc = { enumerable: true, get: function() { return m[k]; } };6}7Object.defineProperty(o, k2, desc);8}) : (function(o, m, k, k2) {9if (k2 === undefined) k2 = k;10o[k2] = m[k];11}));12var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {13Object.defineProperty(o, "default", { enumerable: true, value: v });14}) : function(o, v) {15o["default"] = v;16});17var __importStar = (this && this.__importStar) || function (mod) {18if (mod && mod.__esModule) return mod;19var result = {};20if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);21__setModuleDefault(result, mod);22return result;23};24Object.defineProperty(exports, "__esModule", { value: true });25exports.getUrlInfo = void 0;26const messages_media_1 = require("./messages-media");27const THUMBNAIL_WIDTH_PX = 192;28/** Fetches an image and generates a thumbnail for it */29const getCompressedJpegThumbnail = async (url, { thumbnailWidth, timeoutMs }) => {30const stream = await (0, messages_media_1.getHttpStream)(url, { timeout: timeoutMs });31const result = await (0, messages_media_1.extractImageThumb)(stream, thumbnailWidth);32return result;33};34/**35* Given a piece of text, checks for any URL present, generates link preview for the same and returns it36* Return undefined if the fetch failed or no URL was found37* @param text first matched URL in text38* @returns the URL info required to generate link preview39*/40const getUrlInfo = async (text, opts = { thumbnailWidth: THUMBNAIL_WIDTH_PX, timeoutMs: 3000 }) => {41try {42const { getLinkPreview } = await Promise.resolve().then(() => __importStar(require('link-preview-js')));43let previewLink = text;44if (!text.startsWith('https://') && !text.startsWith('http://')) {45previewLink = 'https://' + previewLink;46}47const info = await getLinkPreview(previewLink, { timeout: opts.timeoutMs });48if (info && 'title' in info) {49const [image] = info.images;50let jpegThumbnail = undefined;51try {52jpegThumbnail = image53? await getCompressedJpegThumbnail(image, opts)54: undefined;55}56catch (error) {57}58return {59'canonical-url': info.url,60'matched-text': text,61title: info.title,62description: info.description,63jpegThumbnail64};65}66}67catch (error) {68if (!error.message.includes('receive a valid')) {69throw error;70}71}72};73exports.getUrlInfo = getUrlInfo;747576