Wednesday, 26 April 2017

Node FTP doesn't execute `once('close')` callback when downloading file is large

I am using I have a method to download file from FTP server and it works fine on smaller files, but when I use it to download file of ~5GB size of zip type, it downloads it, but after that it doesn't do anything. When it reaches 100% of downloading, then script doesn't continue. Should I wait if it's actually doing something in the background after download is complete? Is there filesize limit?

const FTP = require('ftp')
downloadFile: params => {
    return new Promise((resolve, reject) => {
      let ftpClient = new FTP()
      let total = params.state.fileSize
      let progress = 0
      ftpClient.on('ready', _ => {
        console.log(`Downloading ${params.targetedFile} ...`);
        ftpClient.get(params.targetedFile, (err, stream) => {
          if (err) reject(err)
          stream.on('data', buffer => {
            progress += buffer.length
            process.stdout.write(`Progress: ${(progress/total*100).toFixed(2)}% (${progress}/${total})  \r`)
          })
          stream.once('close', _ => {
            ftpClient.end()
            console.log(`Saved downloaded file to ${params.localDir}`);
            resolve(params.localDir)
          })
          stream.pipe(fs.createWriteStream(params.localDir))
        })
      })
      ftpClient.connect(params.auth)
    })
  }

Basically, the callback for stream.on('close', ...) doesn't get executed when large file is downloaded. And it gets executed for smaller file of same type.



via Kunok

No comments:

Post a Comment