mirror of
https://github.com/cheveguerra/whatsapp-web.js.git
synced 2026-04-19 20:19:14 +00:00
feat: Helper function to create MessageMedia from a local file path
This commit is contained in:
@@ -28,6 +28,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pedroslopez/moduleraid": "^4.1.0",
|
"@pedroslopez/moduleraid": "^4.1.0",
|
||||||
"jsqr": "^1.3.1",
|
"jsqr": "^1.3.1",
|
||||||
|
"mime": "^2.4.5",
|
||||||
"puppeteer": "^3.0.4"
|
"puppeteer": "^3.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const mime = require('mime');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
@@ -26,6 +30,18 @@ class MessageMedia {
|
|||||||
*/
|
*/
|
||||||
this.filename = filename;
|
this.filename = filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a MessageMedia instance from a local file path
|
||||||
|
* @param {string} path
|
||||||
|
*/
|
||||||
|
static fromFilePath(filePath) {
|
||||||
|
const b64data = fs.readFileSync(filePath, {encoding: 'base64'});
|
||||||
|
const mimetype = mime.getType(filePath);
|
||||||
|
const filename = path.basename(filePath);
|
||||||
|
|
||||||
|
return new MessageMedia(mimetype, b64data, filename);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = MessageMedia;
|
module.exports = MessageMedia;
|
||||||
Reference in New Issue
Block a user