client.add(torrentId, torrent => { const files = torrent.files let length = files.length // Stream each file to the disk files.forEach(file => { const source = file.createReadStream() const destination = fs.createWriteStream(file.name) source.on('end', () => { console.log('file:\t\t', file.name) // close after all files are saved length -= 1 if (!length) process.exit() }).pipe(destination) }) })
function onReady () { if (typeof argv.select === 'boolean') { clivas.line('Select a file to download:') torrent.files.forEach((file, i) => clivas.line( '{2+bold+magenta:%s} %s {blue:(%s)}', i, file.name, prettierBytes(file.length) )) clivas.line('\nTo select a specific file, re-run `webtorrent` with "--select [index]"') clivas.line('Example: webtorrent download "magnet:..." --select 0') return gracefulExit() } // if no index specified, use largest file const index = (typeof argv.select === 'number') ? argv.select : torrent.files.indexOf(torrent.files.reduce((a, b) => a.length > b.length ? a : b)) if (!torrent.files[index]) { return errorAndExit(`There's no file that maps to index ${index}`) } onSelection(index) }