Skip to content

Commit

Permalink
feat: Helper function to create MessageMedia from a local file path
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroslopez committed May 24, 2020
1 parent d6637d6 commit eb82e80
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"dependencies": {
"@pedroslopez/moduleraid": "^4.1.0",
"jsqr": "^1.3.1",
"mime": "^2.4.5",
"puppeteer": "^3.0.4"
},
"devDependencies": {
Expand Down
16 changes: 16 additions & 0 deletions src/structures/MessageMedia.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;

0 comments on commit eb82e80

Please sign in to comment.