node-ftp icon indicating copy to clipboard operation
node-ftp copied to clipboard

transfer get stuck

Open MetaiR opened this issue 3 years ago • 0 comments

hi, when I use this library to download or upload files on some devices it got stuck at some random point and nothing happen, no error or retry or anything, it just got stuck, what can cause it?

my code:

         download() {
                client.get(path, (e, stream) => {
                      if (e) {
                          console.log('ftp face error for downloading id: ' + someId);
                          this.emitDownloadEvent(this.getDownloadProgressObj(someOtherId, someId, 0, 'ERROR', err));
                          reject();
                          return;
                      }
                      this.emitDownloadCalculatedPercentage(stream, someOtherId, someId, bytes);

                      FileUtil.createDirIfNotExists(videoStorage);

                      const output = fs.createWriteStream(savingPath);
                      stream.pipe(output);

                      output.on('close', () => {
                          console.log('ftp completed for downloading id: ' + someId);
                          this.emitDownloadEvent(this.getDownloadProgressObj(someOtherId, someId, 100, 'COMPLETED'));
                          client.end();
                          resolve();
                      });
                  });
         }
        
        private getDownloadProgressObj(someOtherId: number, someId: string, progress = 0, status: 'CONNECTED' | 'DOWNLOADING' | 'COMPLETED' | 'ERROR' = 'CONNECTED', ex?: Error): FtpProgress {
        return {
            someOtherId: someOtherId,
            type: 'DOWNLOAD',
            someId: someId,
            progress: progress,
            status: status,
            ex: ex
        };
    }

       private emitDownloadCalculatedPercentage(file: NodeJS.ReadableStream, someOtherId: number, someId: string, size: number) {
        let cur = 0;
        file.on('data', (d) => {
            cur += d.length;
            const percent = ((cur / size) * 100).toFixed(1);
            this.emitDownloadEvent(this.getDownloadProgressObj(someOtherId, someId, Number(percent), 'DOWNLOADING'));
        });
    }

    private emitDownloadEvent(progress: FtpProgress) {
        console.log(progress);
        FtpUtil.transferEvent.emit('download', progress);
    }

MetaiR avatar Feb 19 '22 16:02 MetaiR