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);
});
//start download if hash provided
if($('#dropZone').attr('hash') !== ''){
slingit_download(client,$('#dropZone').attr('hash'));}
}
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){
$('#init_loader').hide();
$('#link_container tr:last').after(
'' +
'' +
'starting...' +
' ' +
'' +
'downloading from: ' + torrent.swarm.wires.length + ' peers' +
' ' +
'' +
'' +
' Share: ' +
' ' +
' ' +
' ' +
'' +
' ' +
' ');
}
function log_file_add(torrent){
$('#init_loader').hide();
var progress = (100 * torrent.downloaded / torrent.parsedTorrent.length).toFixed(1);
$('#link_container tr:last').after(
'' +
'' +
progress + '%' +
' ' +
'' +
'speed: ' + Math.round(torrent.swarm.downloadSpeed() * 100) / 100 + ' kb/s' +
' ' +
'' +
'' +
' Share: ' +
' ' +
' ' +
' ' +
'' +
' ' +
' ');
$.getScript("http://platform.twitter.com/widgets.js");
}
function log_file_upd(torrent){
$('#init_loader').hide();
if($('#'+torrent.infoHash).length){
var progress = (100 * torrent.downloaded / torrent.parsedTorrent.length).toFixed(1);
$('#'+torrent.infoHash).html(
'' +
progress + '%' +
' ' +
'' +
'speed: ' + Math.round(torrent.swarm.downloadSpeed() * 100) / 100 + ' / ' + Math.round(torrent.swarm.uploadSpeed() * 100) / 100 + ' kb/s' +
' ' +
'' +
'' +
' Share: ' +
' ' +
' ' +
' ');
} else {
log_file_add(torrent);
}
}
function log_initialize() {
$('#link_container tr:last').after(
'' +
'' +
' '+
'initializing' +
' ' +
'' + ' ' +
'' +
' ' +
' ');
}
function log_file_done(torrent,url,file){
$('#init_loader').hide();
log_file_upd(torrent);
$('#link_container tr:last').after(
'' +
'' +
'100%' +
' ' +
'' +
'' +
' Save: ' + file.name +
' ' +
' ' +
'' +
'' +
' Share: ' +
' ' +
' ' +
' ' +
'' +
' ' +
' ');
$.getScript("http://platform.twitter.com/widgets.js");
}
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);
}