2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-28 21:07:39 +00:00

Fix handling fs.readStream.path if it's a buffer

Bug:

  The (private) method TelegramBot#_formatSendData(), used by public
  methods, such as TelegramBot#sendPhoto(), throws an error
  if the stream passed (fs.readStream) has the property 'path',
  being an instance of Buffer.

  For example,

    const stream = fs.createReadStream(Buffer.from('cat.png'));
    bot.sendPhoto(chatId, stream);

  Would throw an error, like

    TypeError: Path must be a string. Received <Buffer 60 62 63 64>

  This is because of this line:

    src/telegram.js:297
      fileName = URL.parse(path.basename(data.path)).pathname;

  path.basename() can not handle buffer (non-string) paths. From the
  docs, "A TypeError is thrown if path is not a string...".

Fix:

  Ensure path.basename() receives a string, by converting the buffer
  to string.

References:

  * fs.ReadStream: https://nodejs.org/docs/latest/api/fs.html#fs_class_fs_readstream
This commit is contained in:
GochoMugo 2016-10-07 19:52:00 +03:00
parent e0e5e9a7b0
commit be49b69219
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
2 changed files with 11 additions and 1 deletions

View File

@ -294,7 +294,7 @@ class TelegramBot extends EventEmitter {
let fileName;
let fileId;
if (data instanceof stream.Stream) {
fileName = URL.parse(path.basename(data.path)).pathname;
fileName = URL.parse(path.basename(data.path.toString())).pathname;
formData = {};
formData[type] = {
value: data,

View File

@ -162,6 +162,16 @@ describe('Telegram', function telegramSuite() {
});
});
describe('#_formatSendData', function _formatSendData() {
it('should handle buffer path from fs.readStream', function test() {
const bot = new Telegram(TOKEN);
const photo = fs.createReadStream(Buffer.from(`${__dirname}/bot.gif`));
return bot.sendPhoto(USERID, photo).then(resp => {
assert.ok(is.object(resp));
});
});
});
describe('#sendPhoto', function sendPhotoSuite() {
let photoId;
it('should send a photo from file', function test() {