mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 04:29:15 +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 {
|
export interface MediaFromURLOptions {
|
||||||
client?: Client
|
client?: Client
|
||||||
|
filename?: string
|
||||||
unsafeMime?: boolean
|
unsafeMime?: boolean
|
||||||
reqOptions?: RequestInit
|
reqOptions?: RequestInit
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const path = require('path');
|
|||||||
const mime = require('mime');
|
const mime = require('mime');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const { URL } = require('url');
|
const { URL } = require('url');
|
||||||
|
const { Client } = require('whatsapp-web.js'); // eslint-disable-line no-unused-vars
|
||||||
/**
|
/**
|
||||||
* Media attached to a message
|
* Media attached to a message
|
||||||
* @param {string} mimetype MIME type of the attachment
|
* @param {string} mimetype MIME type of the attachment
|
||||||
@@ -50,27 +50,25 @@ class MessageMedia {
|
|||||||
* Creates a MessageMedia instance from a URL
|
* Creates a MessageMedia instance from a URL
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {Object} [options]
|
* @param {Object} [options]
|
||||||
* @param {number} [options.unsafeMime=false]
|
* @param {boolean} [options.unsafeMime=false]
|
||||||
* @param {object} [options.client]
|
* @param {string} [options.filename]
|
||||||
|
* @param {Client} [options.client]
|
||||||
* @param {object} [options.reqOptions]
|
* @param {object} [options.reqOptions]
|
||||||
* @param {number} [options.reqOptions.size=0]
|
* @param {number} [options.reqOptions.size=0]
|
||||||
* @returns {Promise<MessageMedia>}
|
* @returns {Promise<MessageMedia>}
|
||||||
*/
|
*/
|
||||||
static async fromUrl(url, options = {}) {
|
static async fromUrl(url, options = {}) {
|
||||||
let mimetype;
|
const pUrl = new URL(url);
|
||||||
|
let mimetype = mime.getType(pUrl.pathname);
|
||||||
|
|
||||||
if (!options.unsafeMime) {
|
if (!mimetype && !options.unsafeMime)
|
||||||
const pUrl = new URL(url);
|
throw new Error('Unable to determine MIME type using URL. Set unsafeMime to true to download it anyway.');
|
||||||
mimetype = mime.getType(pUrl.pathname);
|
|
||||||
|
|
||||||
if (!mimetype)
|
|
||||||
throw new Error('Unable to determine MIME type');
|
|
||||||
}
|
|
||||||
|
|
||||||
async function fetchData (url, options) {
|
async function fetchData (url, options) {
|
||||||
const reqOptions = Object.assign({ headers: { accept: 'image/* video/* text/* audio/*' } }, options);
|
const reqOptions = Object.assign({ headers: { accept: 'image/* video/* text/* audio/*' } }, options);
|
||||||
const response = await fetch(url, reqOptions);
|
const response = await fetch(url, reqOptions);
|
||||||
const mime = response.headers.get('Content-Type');
|
const mime = response.headers.get('Content-Type');
|
||||||
|
const name = response.headers.get('Content-Disposition')?.match(/((?<=filename=")(.*)(?="))/);
|
||||||
let data = '';
|
let data = '';
|
||||||
|
|
||||||
if (response.buffer) {
|
if (response.buffer) {
|
||||||
@@ -83,17 +81,20 @@ class MessageMedia {
|
|||||||
data = btoa(data);
|
data = btoa(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { data, mime };
|
return { data, mime, name };
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = options.client
|
const res = options.client
|
||||||
? (await options.client.pupPage.evaluate(fetchData, url, options.reqOptions))
|
? (await options.client.pupPage.evaluate(fetchData, url, options.reqOptions))
|
||||||
: (await 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)
|
if (!mimetype)
|
||||||
mimetype = res.mime;
|
mimetype = res.mime;
|
||||||
|
|
||||||
return new MessageMedia(mimetype, res.data, null);
|
return new MessageMedia(mimetype, res.data, filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user