From 04e8b892aaccee2c64a19374e32f713dd4739dd1 Mon Sep 17 00:00:00 2001 From: GochoMugo Date: Fri, 8 Dec 2017 19:25:56 +0300 Subject: [PATCH] src/telegram: Emit 'info' on stream from TelegramBot#getFileStream() --- doc/api.md | 4 ++++ src/telegram.js | 7 +++++++ test/telegram.js | 14 +++++++++----- test/utils.js | 12 ++++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/doc/api.md b/doc/api.md index a4e6956..28a0fd0 100644 --- a/doc/api.md +++ b/doc/api.md @@ -846,7 +846,11 @@ which returns just path to file on remote server (you will have to manually buil ### telegramBot.getFileStream(fileId, [options]) ⇒ stream.Readable Return a readable stream for file. + `fileStream.path` is the specified file ID i.e. `fileId`. +`fileStream` emits event `info` passing a single argument i.e. +`info` with the interface `{ uri }` where `uri` is the URI of the +file on Telegram servers. This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method, which returns the full URI to the file on remote server. diff --git a/src/telegram.js b/src/telegram.js index 5bd5b46..a1be259 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -1324,7 +1324,11 @@ class TelegramBot extends EventEmitter { /** * Return a readable stream for file. + * * `fileStream.path` is the specified file ID i.e. `fileId`. + * `fileStream` emits event `info` passing a single argument i.e. + * `info` with the interface `{ uri }` where `uri` is the URI of the + * file on Telegram servers. * * This method is a sugar extension of the [getFileLink](#TelegramBot+getFileLink) method, * which returns the full URI to the file on remote server. @@ -1338,6 +1342,9 @@ class TelegramBot extends EventEmitter { fileStream.path = fileId; this.getFileLink(fileId, form) .then((fileURI) => { + fileStream.emit('info', { + uri: fileURI, + }); pump(streamedRequest({ uri: fileURI }), fileStream); }) .catch((error) => { diff --git a/test/telegram.js b/test/telegram.js index cfbf303..a6a9c7c 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -1160,7 +1160,7 @@ describe('TelegramBot', function telegramSuite() { return bot.getFileLink(FILE_ID) .then(fileURI => { assert.ok(is.string(fileURI)); - assert.ok(/https?:\/\/.*\/file\/bot.*\/.*/.test(fileURI)); + assert.ok(utils.isTelegramFileURI(fileURI)); }); }); }); @@ -1174,10 +1174,14 @@ describe('TelegramBot', function telegramSuite() { const fileStream = bot.getFileStream(FILE_ID); assert.ok(fileStream instanceof stream.Readable); assert.equal(fileStream.path, FILE_ID); - fileStream.pipe(concat(function readFile(buffer) { - buffer.equals(fs.readFileSync(FILE_PATH)); // sync :( - return done(); - })); + fileStream.on('info', (info) => { + assert.ok(info); + assert.ok(utils.isTelegramFileURI(info.uri), `${info.uri} is not a file URI`); + fileStream.pipe(concat(function readFile(buffer) { + buffer.equals(fs.readFileSync(FILE_PATH)); // sync :( + return done(); + })); + }); }); }); diff --git a/test/utils.js b/test/utils.js index 23df9d8..f8ebe5e 100644 --- a/test/utils.js +++ b/test/utils.js @@ -30,6 +30,13 @@ exports = module.exports = { * @return {Promise} */ isPollingMockServer, + /** + * Return true if the string is a URI to a file + * on Telegram servers. + * @param {String} uri + * @return {Boolean} + */ + isTelegramFileURI, /** * Send a message to the webhook at the specified port and path. * @param {Number} port @@ -217,3 +224,8 @@ function handleRatelimit(bot, methodName, suite) { }; return bot; } + + +function isTelegramFileURI(uri) { + return /https?:\/\/.*\/file\/bot.*\/.*/.test(uri); +}