G
G
ganzhybas2020-11-17 12:29:17
Node.js
ganzhybas, 2020-11-17 12:29:17

Node.js(Sails, fs, request, bluebird), one of the two files cannot be downloaded.?

Good day to all!
Faced the following problem, there is a script that downloads two files from a remote server. One is bound to fail. When one of the files is downloaded, the second one immediately stops downloading.

The code:

var request = require('request');
var fs = require('fs');
var path = require('path');
var Promise = require('bluebird');

var uploadPath = './uploads/';
var serverUrl = 'http://somewhere.net/';

    downloadFile: function(name) {
      var self = this;
      return new Promise(function(resolve, reject) {
        var p = path.join(__dirname, uploadPath + name);
         console.log('work with path: ' + p);
        var writer = fs.createWriteStream(p);
        var time_start = Date.now();
        console.log('start downloading', name, 'from: ', serverUrl + name);

        var download = request({
          url: serverUrl + name,
          method: 'get',
          timeout: 1000 * 60 * 10
        }, function(error, response) {
          if (error) {
            console.log('request error', error, 'for', (Date.now() - time_start) / 1000, 'seconds');
            reject({
              reason: 'download failure'
            });
          }
        }).pipe(writer);

          Promise.all([
            self.downloadFile('FIRST_FILE'),
            self.downloadFile('SECOND_FILE')
          ]).then(SOME_CODE);

I'll be honest, the script was not written by me and still worked properly (the files lay down entirely and both), but from some point it stopped, one of the files lays down broken, even if you crack.
I would be extremely grateful for any feedback and suggestions.
Best wishes!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question