torrent.on('done', () => { if (!argv.quiet) { const numActiveWires = torrent.wires .reduce((num, wire) => num + (wire.downloaded > 0), 0) clivas.line('') clivas.line( 'torrent downloaded {green:successfully} from {bold:%s/%s} {green:peers} ' + 'in {bold:%ss}!', numActiveWires, torrent.numPeers, getRuntime() ) } torrentDone() })
/*displayFile(fileindex,locationid){ this.torrent.files[fileindex].renderTo(locationid) }*/ removeTorrent(){ //this.client.remove(torrentId, [function callback (err) {}]) if ((this.torrentId!='')&&(this.torrentId!=undefined)) { this.client.remove(this.torrentId) console.log('remove torrent',this.torrentId) } }
destroyClient(){ //this.client.destroy([function callback (err) {}]) this.client.destroy() }
client.on('error', function (err) { console.error('Error: ' + err.message) loading.style.display = 'none'; alert('An error occured. Please try again.') location.reload() })
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) }
// Listen for for input document.querySelector('form').addEventListener('submit', function (e) { e.preventDefault() // Clear out contents document.getElementById('title').innerHTML = ''; progressBar.value = 0 progressBar.style.display = 'none' document.querySelector('.output').innerHTML = ''; // Get magnet var torrentId = document.querySelector('form input[name=magnet]').value // Show loading spinner loading.style.display = 'block'; ipc.send('downloads-path') ipc.on('downloads-path-reply', function (event, path) { const downloadsPath = path // Start downloading torrent, callback to onTorrent client.add(torrentId, { path: downloadsPath }, onTorrent) }) })
function runSeed (input) { if (path.extname(input).toLowerCase() === '.torrent' || /^magnet:/.test(input)) { // `webtorrent seed` is meant for creating a new torrent based on a file or folder // of content, not a torrent id (.torrent or a magnet uri). If this command is used // incorrectly, let's just do the right thing. runDownload(input) return } const client = new WebTorrent({ blocklist: argv.blocklist }) client.on('error', fatalError) client.seed(input, { announce: argv.announce, torrentPort: argv['torrent-port'], dhtPort: argv['dht-port'] }, torrent => { if (argv.quiet) { console.log(torrent.magnetURI) } drawTorrent(torrent) }) }
torrent.on('infoHash', () => { if ('select' in argv) { torrent.so = argv.select.toString() } if (argv.quiet) return updateMetadata() torrent.on('wire', updateMetadata) function updateMetadata () { clivas.clear() clivas.line( '{green:fetching torrent metadata from} {bold:%s} {green:peers}', torrent.numPeers ) } torrent.on('metadata', () => { clivas.clear() torrent.removeListener('wire', updateMetadata) clivas.clear() clivas.line('{green:verifying existing torrent data...}') }) })
}) torrent.on('infoHash', function () { const torrentFilePath = `${argv.out}/${this.infoHash}.torrent` torrent.on('wire', updateMetadata) clivas.line( '{green:fetching torrent metadata from} {bold:%s} {green:peers}', torrent.numPeers torrent.on('metadata', function () { clivas.clear() torrent.removeListener('wire', updateMetadata)
this.client.add(magnetURI, { path: path.join(__dirname,'../cached/') }, function (torrent) { var file = torrent.files.find(function (file) { return file.path.endsWith(filename) }) file.renderTo('video#contentvideo')//(renderingelement)//('video#contentvideo')//.appendTo('#output') torrent.on('download', function (bytes) { let tmpinfo=[] torrent.files.forEach(function(file){ path:file.path, downloadSpeed:torrent.downloadSpeed, numPeers:torrent.numPeers, progress:file.progress })
// Listen for a recent file or open file click and append that file to the DOM ipc.on('open-file-reply', function (event, filePath) { var fileName = filePath.split('/').slice(-1)[0] document.getElementsByClassName('output')[0].innerHTML = '' if (fileName.endsWith('.mp4')) { log(fileName) log('<video controls autoplay><source src="' + filePath + '" type="video/mp4"></video>') } else if (fileName.endsWith('.torrent')) { var torrentId = filePath // Show loading spinner loading.style.display = 'block'; ipc.send('downloads-path') ipc.on('downloads-path-reply', function (event, path) { const downloadsPath = path // Start downloading torrent, callback to onTorrent client.add(torrentId, { path: downloadsPath }, onTorrent) }) } progressBar.value = 0 progressBar.style.display = 'none' })