diff --git a/package.json b/package.json index 647a5d5..0b353dc 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "dependencies": { "@pedroslopez/moduleraid": "^4.1.0", "jsqr": "^1.3.1", + "mime": "^2.4.5", "puppeteer": "^3.0.4" }, "devDependencies": { diff --git a/src/structures/MessageMedia.js b/src/structures/MessageMedia.js index b0c5f0b..0abb4a9 100644 --- a/src/structures/MessageMedia.js +++ b/src/structures/MessageMedia.js @@ -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; \ No newline at end of file