mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-18 03:29:14 +00:00
update MessageMedia.fromUrl (#811)
* fix MessageMedia.fromUrl docs * use content-type as fallback * set filename based on url * fix empty filename * wrap expression in parentheses * hotfix * JSDoc fix * fix for jsdog * eslint gets retirement home Co-authored-by: Rajeh Taher <rajeh@reforward.dev> Co-authored-by: Aliyss Snow <33941859+Aliyss@users.noreply.github.com>
This commit is contained in:
1
index.d.ts
vendored
1
index.d.ts
vendored
@@ -692,6 +692,7 @@ declare namespace WAWebJS {
|
||||
|
||||
export interface MediaFromURLOptions {
|
||||
client?: Client
|
||||
filename?: string
|
||||
unsafeMime?: boolean
|
||||
reqOptions?: RequestInit
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ const path = require('path');
|
||||
const mime = require('mime');
|
||||
const fetch = require('node-fetch');
|
||||
const { URL } = require('url');
|
||||
|
||||
const { Client } = require('whatsapp-web.js'); // eslint-disable-line no-unused-vars
|
||||
/**
|
||||
* Media attached to a message
|
||||
* @param {string} mimetype MIME type of the attachment
|
||||
@@ -50,27 +50,25 @@ class MessageMedia {
|
||||
* Creates a MessageMedia instance from a URL
|
||||
* @param {string} url
|
||||
* @param {Object} [options]
|
||||
* @param {number} [options.unsafeMime=false]
|
||||
* @param {object} [options.client]
|
||||
* @param {boolean} [options.unsafeMime=false]
|
||||
* @param {string} [options.filename]
|
||||
* @param {Client} [options.client]
|
||||
* @param {object} [options.reqOptions]
|
||||
* @param {number} [options.reqOptions.size=0]
|
||||
* @returns {Promise<MessageMedia>}
|
||||
*/
|
||||
static async fromUrl(url, options = {}) {
|
||||
let mimetype;
|
||||
const pUrl = new URL(url);
|
||||
let mimetype = mime.getType(pUrl.pathname);
|
||||
|
||||
if (!options.unsafeMime) {
|
||||
const pUrl = new URL(url);
|
||||
mimetype = mime.getType(pUrl.pathname);
|
||||
|
||||
if (!mimetype)
|
||||
throw new Error('Unable to determine MIME type');
|
||||
}
|
||||
if (!mimetype && !options.unsafeMime)
|
||||
throw new Error('Unable to determine MIME type using URL. Set unsafeMime to true to download it anyway.');
|
||||
|
||||
async function fetchData (url, options) {
|
||||
const reqOptions = Object.assign({ headers: { accept: 'image/* video/* text/* audio/*' } }, options);
|
||||
const response = await fetch(url, reqOptions);
|
||||
const mime = response.headers.get('Content-Type');
|
||||
const name = response.headers.get('Content-Disposition')?.match(/((?<=filename=")(.*)(?="))/);
|
||||
let data = '';
|
||||
|
||||
if (response.buffer) {
|
||||
@@ -83,18 +81,21 @@ class MessageMedia {
|
||||
data = btoa(data);
|
||||
}
|
||||
|
||||
return { data, mime };
|
||||
return { data, mime, name };
|
||||
}
|
||||
|
||||
const res = options.client
|
||||
? (await options.client.pupPage.evaluate(fetchData, url, options.reqOptions))
|
||||
: (await fetchData(url, options.reqOptions));
|
||||
|
||||
const filename = options.filename ??
|
||||
(res.name ? res.name[0] : (pUrl.pathname.split('/').pop() || 'file'));
|
||||
|
||||
if (!mimetype)
|
||||
mimetype = res.mime;
|
||||
|
||||
return new MessageMedia(mimetype, res.data, null);
|
||||
return new MessageMedia(mimetype, res.data, filename);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageMedia;
|
||||
module.exports = MessageMedia;
|
||||
|
||||
Reference in New Issue
Block a user