var client = new WebTorrent({ rtcConfig: { iceServers: [{url: "stun:23.21.150.121"}, {url: "stun:stun.1.google.com:19302"}] } }); function init_start(){ //upload var dropZone = document.body; //Optional. Show the copy icon when dragging over. Seems to only work for chrome. dropZone.addEventListener('dragover', function(e) { e.stopPropagation(); e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; }); // Get file data on drop dropZone.addEventListener('drop', function(e) { e.stopPropagation(); e.preventDefault(); var files = e.dataTransfer.files; // Array of all files slingit_upload(client,files); }); //download $('#btn_load_infohash').click(function(e){ e.preventDefault(); slingit_download(client,$('#input_load_infohash').val());}); //start download if hash provided if($('#input_load_infohash').val() != ''){ slingit_download(client,$('#input_load_infohash').val());} } function handleFileSelect(evt) { var files = evt.target.files; // FileList object // files is a FileList of File objects. List some properties. var output = []; for (var i = 0, f; f = files[i]; i++) { output.push('
  • ', escape(f.name), ' (', f.type || 'n/a', ') - ', f.size, 'full path:', f.fullPath, ' bytes, last modified: ', f.lastModifiedDate.toLocaleDateString(), '
  • '); console.log(f); } document.getElementById('list').innerHTML = ''; } function log_file_start(torrent){ $('#link_container tr:last').after( '' + '' + 'starting...' + '' + '' + 'downloading from: ' + torrent.swarm.wires.length + ' peers' + '' + '' + '' + 'direct link: ' + '' + '' + '' + ''); } function log_file_add(torrent){ var progress = (100 * torrent.downloaded / torrent.parsedTorrent.length).toFixed(1) $('#link_container tr:last').after( '' + '' + progress + '%' + '' + '' + 'speed: ' + torrent.swarm.downloadSpeed() + '/s' + '' + '' + '' + 'direct link: ' + '' + '' + '' + ''); } function log_file_upd(torrent){ if($('#'+torrent.infoHash).length){ var progress = (100 * torrent.downloaded / torrent.parsedTorrent.length).toFixed(1) $('#'+torrent.infoHash).html( '' + progress + '%' + '' + '' + 'speed: ' + torrent.swarm.downloadSpeed() + '/s / ' + torrent.swarm.uploadSpeed() + '/s' + '' + '' + '' + 'direct link: ' + '' + '' + '') } else { log_file_add(torrent); } } function log_initialize() { $('#link_container tr:last').after( '' + '' + 'initializing' + '' + '' + '' + '' + '' + ''); } function log_file_done(torrent,url,file){ log_file_upd(torrent); $('#link_container tr:last').after( '' + '' + '100%' + '' + '' + '' + 'Download ' + file.name + '' + '' + '' + '' + 'direct link: ' + '' + '' + '' + ''); } function slingit_upload(client,files){ log_initialize(); var fileStore = new Array(); console.log(files) for (var i=0, file; file=files[i]; i++) { var reader = new FileReader(); reader.onload = function(e2) { // finished reading file data. } reader.readAsDataURL(file); // start reading the file data. fileStore.push(file); console.log(file); var fname = file.name client.seed(file,onTorrent); } } function onTorrent (torrent) { //log_file_start(torrent) //console.log('Torrent info hash:', torrent.infoHash); torrent.swarm.on('download', function () { log_file_upd(torrent); }) torrent.swarm.on('upload', function () { log_file_upd(torrent); }) torrent.files.forEach(function (file) { /*var extname = path.extname(file.name) if (window.MediaSource && extname === '.mp4' || extname === '.webm') { var video = document.createElement('video') video.controls = true media.appendChild(video) file.createReadStream().pipe(video) } else if (window.MediaSource && extname === '.mp3') { var audio = document.createElement('audio') audio.controls = true media.appendChild(audio) file.createReadStream().pipe(audio) } else {*/ file.getBlobURL(function (err, url) { if (err) throw err log_file_done(torrent,url,file); }) //} }) } function slingit_download(client,hash){ log_initialize(); var magnetUri = 'magnet:?xt=urn:btih:' + hash + '&tr=wss://tracker.webtorrent.io'; console.log(magnetUri); client.add(magnetUri, onTorrent); }