feat: Helper function to create MessageMedia from a local file path

This commit is contained in:
Pedro Lopez 2020-05-23 22:35:59 -04:00
commit eb82e80343

View file

@ -1,5 +1,9 @@
'use strict';
const fs = require('fs');
const path = require('path');
const mime = require('mime');
/**
* Media attached to a message
* @param {string} mimetype MIME type of the attachment
@ -26,6 +30,18 @@ class MessageMedia {
*/
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;