mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-20 12:39:20 +00:00
feat: Adding file size by bytes to MessageMedia (#1273)
* Update index.d.ts * Update Message.js * Update Message.js * Update MessageMedia.js * Update MessageMedia.js
This commit is contained in:
5
index.d.ts
vendored
5
index.d.ts
vendored
@@ -809,13 +809,16 @@ declare namespace WAWebJS {
|
|||||||
data: string
|
data: string
|
||||||
/** Document file name. Value can be null */
|
/** Document file name. Value can be null */
|
||||||
filename?: string | null
|
filename?: string | null
|
||||||
|
/** Document file size in bytes. Value can be null. */
|
||||||
|
filesize?: number | null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} mimetype MIME type of the attachment
|
* @param {string} mimetype MIME type of the attachment
|
||||||
* @param {string} data Base64-encoded data of the file
|
* @param {string} data Base64-encoded data of the file
|
||||||
* @param {?string} filename Document file name. Value can be null
|
* @param {?string} filename Document file name. Value can be null
|
||||||
|
* @param {?number} filesize Document file size in bytes. Value can be null.
|
||||||
*/
|
*/
|
||||||
constructor(mimetype: string, data: string, filename?: string | null)
|
constructor(mimetype: string, data: string, filename?: string | null, filesize?: number | null)
|
||||||
|
|
||||||
/** Creates a MessageMedia instance from a local file path */
|
/** Creates a MessageMedia instance from a local file path */
|
||||||
static fromFilePath: (filePath: string) => MessageMedia
|
static fromFilePath: (filePath: string) => MessageMedia
|
||||||
|
|||||||
@@ -413,7 +413,8 @@ class Message extends Base {
|
|||||||
return {
|
return {
|
||||||
data,
|
data,
|
||||||
mimetype: msg.mimetype,
|
mimetype: msg.mimetype,
|
||||||
filename: msg.filename
|
filename: msg.filename,
|
||||||
|
filesize: msg.size
|
||||||
};
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if(e.status && e.status === 404) return undefined;
|
if(e.status && e.status === 404) return undefined;
|
||||||
@@ -422,7 +423,7 @@ class Message extends Base {
|
|||||||
}, this.id._serialized);
|
}, this.id._serialized);
|
||||||
|
|
||||||
if (!result) return undefined;
|
if (!result) return undefined;
|
||||||
return new MessageMedia(result.mimetype, result.data, result.filename);
|
return new MessageMedia(result.mimetype, result.data, result.filename, result.filesize);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -10,10 +10,11 @@ const { URL } = require('url');
|
|||||||
* 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
|
||||||
* @param {string} data Base64-encoded data of the file
|
* @param {string} data Base64-encoded data of the file
|
||||||
* @param {?string} filename Document file name
|
* @param {?string} filename Document file name. Value can be null
|
||||||
|
* @param {?number} filesize Document file size in bytes. Value can be null
|
||||||
*/
|
*/
|
||||||
class MessageMedia {
|
class MessageMedia {
|
||||||
constructor(mimetype, data, filename) {
|
constructor(mimetype, data, filename, filesize) {
|
||||||
/**
|
/**
|
||||||
* MIME type of the attachment
|
* MIME type of the attachment
|
||||||
* @type {string}
|
* @type {string}
|
||||||
@@ -27,10 +28,16 @@ class MessageMedia {
|
|||||||
this.data = data;
|
this.data = data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Name of the file (for documents)
|
* Document file name. Value can be null
|
||||||
* @type {?string}
|
* @type {?string}
|
||||||
*/
|
*/
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Document file size in bytes. Value can be null
|
||||||
|
* @type {?number}
|
||||||
|
*/
|
||||||
|
this.filesize = filesize;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,6 +75,7 @@ class MessageMedia {
|
|||||||
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 size = response.headers.get('Content-Length');
|
||||||
|
|
||||||
const contentDisposition = response.headers.get('Content-Disposition');
|
const contentDisposition = response.headers.get('Content-Disposition');
|
||||||
const name = contentDisposition ? contentDisposition.match(/((?<=filename=")(.*)(?="))/) : null;
|
const name = contentDisposition ? contentDisposition.match(/((?<=filename=")(.*)(?="))/) : null;
|
||||||
@@ -83,7 +91,7 @@ class MessageMedia {
|
|||||||
data = btoa(data);
|
data = btoa(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { data, mime, name };
|
return { data, mime, name, size };
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = options.client
|
const res = options.client
|
||||||
@@ -96,7 +104,7 @@ class MessageMedia {
|
|||||||
if (!mimetype)
|
if (!mimetype)
|
||||||
mimetype = res.mime;
|
mimetype = res.mime;
|
||||||
|
|
||||||
return new MessageMedia(mimetype, res.data, filename);
|
return new MessageMedia(mimetype, res.data, filename, res.size || null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user