2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-23 10:27:49 +00:00

src/telegram: Implement downloadFile() in terms of getFileStream()

This commit is contained in:
GochoMugo 2017-12-08 19:37:12 +03:00
parent 04e8b892aa
commit f28416fbaf
No known key found for this signature in database
GPG Key ID: 7B6A01CB57AA39E4
2 changed files with 22 additions and 15 deletions

View File

@ -867,7 +867,9 @@ which returns the full URI to the file on remote server.
### telegramBot.downloadFile(fileId, downloadDir, [options]) ⇒ <code>Promise</code> ### telegramBot.downloadFile(fileId, downloadDir, [options]) ⇒ <code>Promise</code>
Downloads file in the specified folder. Downloads file in the specified folder.
This is just a sugar for (getFile)[#getfilefiled] method
This method is a sugar extension of the [getFileStream](#TelegramBot+getFileStream) method,
which returns a readable file stream.
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> **Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
**Returns**: <code>Promise</code> - promise Promise, which will have *filePath* of downloaded file in resolve callback **Returns**: <code>Promise</code> - promise Promise, which will have *filePath* of downloaded file in resolve callback

View File

@ -1355,7 +1355,9 @@ class TelegramBot extends EventEmitter {
/** /**
* Downloads file in the specified folder. * Downloads file in the specified folder.
* This is just a sugar for (getFile)[#getfilefiled] method *
* This method is a sugar extension of the [getFileStream](#TelegramBot+getFileStream) method,
* which returns a readable file stream.
* *
* @param {String} fileId File identifier to get info about * @param {String} fileId File identifier to get info about
* @param {String} downloadDir Absolute path to the folder in which file will be saved * @param {String} downloadDir Absolute path to the folder in which file will be saved
@ -1363,20 +1365,23 @@ class TelegramBot extends EventEmitter {
* @return {Promise} promise Promise, which will have *filePath* of downloaded file in resolve callback * @return {Promise} promise Promise, which will have *filePath* of downloaded file in resolve callback
*/ */
downloadFile(fileId, downloadDir, form = {}) { downloadFile(fileId, downloadDir, form = {}) {
return this let resolve;
.getFileLink(fileId, form) let reject;
.then(fileURI => { const promise = new Promise((a, b) => {
const fileName = fileURI.slice(fileURI.lastIndexOf('/') + 1); resolve = a;
reject = b;
});
const fileStream = this.getFileStream(fileId, form);
fileStream.on('info', (info) => {
const fileName = info.uri.slice(info.uri.lastIndexOf('/') + 1);
// TODO: Ensure fileName doesn't contains slashes // TODO: Ensure fileName doesn't contains slashes
const filePath = path.join(downloadDir, fileName); const filePath = path.join(downloadDir, fileName);
pump(fileStream, fs.createWriteStream(filePath), (error) => {
// properly handles errors and closes all streams if (error) { return reject(error); }
return Promise return resolve(filePath);
.fromCallback(next => {
pump(streamedRequest({ uri: fileURI }), fs.createWriteStream(filePath), next);
})
.return(filePath);
}); });
});
return promise;
} }
/** /**