Chain promise on getFileLink and return a new one. Call reject on error

experimental
Yago 10 years ago
parent 04412cc7bf
commit 2247d248c8
  1. 31
      src/telegram.js

@ -449,23 +449,20 @@ TelegramBot.prototype.getFileLink = function(fileId) {
*/ */
TelegramBot.prototype.downloadFile = function(fileId, downloadDir) { TelegramBot.prototype.downloadFile = function(fileId, downloadDir) {
var bot = this; return this.getFileLink(fileId).then(function (fileURI) {
var fileName = fileURI.slice(fileURI.lastIndexOf('/') + 1);
return new Promise(function(resolve, reject) { // TODO: Ensure fileName doesn't contains slashes
var filePath = downloadDir + '/' + fileName;
bot.getFileLink(fileId).then(function(fileURI) { return new Promise(function (resolve, reject) {
// Apparently, this is not the safest method to get the file name, request({uri: fileURI})
// but I am pretty sure that Telegram will not include slashes when generating names .pipe(fs.createWriteStream(filePath))
var fileName = fileURI.slice(fileURI.lastIndexOf('/') + 1); .on('error', reject)
var filePath = downloadDir + '/' + fileName; .on('close', function() {
resolve(filePath);
request({uri: fileURI}) });
.pipe(fs.createWriteStream(filePath)) });
.on('close', function() { });
resolve(filePath);
});
});
});
} }
module.exports = TelegramBot; module.exports = TelegramBot;

Loading…
Cancel
Save