write(identifier, writableStream, options) {
        return new Promise((resolve, reject) => {
            options = options || {};
            options.end = (typeof options['end'] === 'undefined' ? true : options['end']);

            writableStream.on('error', error => {
                reject(error);
            });

            // Iterate over each chunk
            var pipeChunk = number => {
                var chunkFilename = this._getChunkFilename(number, identifier);
                fs.exists(chunkFilename).then(exists => {
                    if (exists) {
                        // If the chunk with the current number exists,
                        // then create a ReadStream from the file
                        // and pipe it to the specified writableStream.
                        var sourceStream = createReadStream(chunkFilename);
                        sourceStream.pipe(writableStream, {
                            end: false
                        });
                        sourceStream.on('end', function() {
                            // When the chunk is fully streamed,
                            // jump to the next one
                            pipeChunk(number + 1);
                        });
                    } else {
                        if (options.end) {
                            writableStream.end();
                        }
                        resolve();
                    }
                }).catch(err => {
                    reject(err);
                });
            };
            pipeChunk(1);
        });
    }
// Combine downloaded chunks and pipe them to a writable stream
Combine downloaded chunks and pipe them to a writable stream
Combine downloaded chunks and pipe them to a writable stream
write(identifier, writableStream, options) {
        return new Promise((resolve, reject) => {
            options = options || {};
            options.end = (typeof options['end'] === 'undefined' ? true : options['end']);
            writableStream.on('error', error => {
                reject(error);
            });
            var pipeChunk = number => {
                var chunkFilename = this._getChunkFilename(number, identifier);
                fs.exists(chunkFilename).then(exists => {
                    if (exists) {
                        var sourceStream = createReadStream(chunkFilename);
                        sourceStream.pipe(writableStream, {
                            end: false
                        });
                        sourceStream.on('end', function() {
                            pipeChunk(number + 1);
                        });
                    } else {
                        if (options.end) {
                            writableStream.end();
                        }
                        resolve();
                    }
                }).catch(err => {
                    reject(err);
                });
            };
            pipeChunk(1);
        });
    }
write ( identifier , writableStream , options ) { return new Promise ( ( resolve , reject ) => { options = options || { } ; options . end = ( typeof options [ 'end' ] === 'undefined' ? true : options [ 'end' ] ) ; writableStream . on ( 'error' , error => { reject ( error ) ; } ) ; var pipeChunk = number => { var chunkFilename = this . _getChunkFilename ( number , identifier ) ; fs . exists ( chunkFilename ) . then ( exists => { if ( exists ) { var sourceStream = createReadStream ( chunkFilename ) ; sourceStream . pipe ( writableStream , { end : false } ) ; sourceStream . on ( 'end' , function ( ) { pipeChunk ( number + 1 ) ; } ) ; } else { if ( options . end ) { writableStream . end ( ) ; } resolve ( ) ; } } ) . catch ( err => { reject ( err ) ; } ) ; } ; pipeChunk ( 1 ) ; } ) ; }
Combine downloaded chunks and pipe them to a writable stream
Combine downloaded chunks and pipe them to a writable stream
// Iterate over each chunk // If the chunk with the current number exists, // then create a ReadStream from the file // and pipe it to the specified writableStream. // When the chunk is fully streamed, // jump to the next one