206 lines
7.7 KiB
JavaScript
206 lines
7.7 KiB
JavaScript
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('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
|
|
f.size, 'full path:',
|
|
f.fullPath, ' bytes, last modified: ',
|
|
f.lastModifiedDate.toLocaleDateString(), '</li>');
|
|
console.log(f);
|
|
}
|
|
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
|
|
}
|
|
|
|
function log_file_start(torrent){
|
|
$('#link_container tr:last').after(
|
|
'<tr>' +
|
|
'<td>' +
|
|
'starting...' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'downloading from: ' + torrent.swarm.wires.length + ' peers' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'<a href="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '"' +
|
|
' target="_blank">' +
|
|
'direct link: ' +
|
|
'</a>' +
|
|
'<input type="text" value="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '" onClick="this.setSelectionRange(0, this.value.length)"/>' +
|
|
' ' +
|
|
'<a class="twitter-share-button" href="https://twitter.com/share" data-via="slingtweet" data-url="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '">Tweet</a>' +
|
|
'</td>' +
|
|
'</tr>');
|
|
}
|
|
|
|
function log_file_add(torrent){
|
|
var progress = (100 * torrent.downloaded / torrent.parsedTorrent.length).toFixed(1)
|
|
$('#link_container tr:last').after(
|
|
'<tr id="' + torrent.infoHash + '">' +
|
|
'<td>' +
|
|
progress + '%' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'speed: ' + torrent.swarm.downloadSpeed() + '/s' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'<a href="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '"' +
|
|
' target="_blank">' +
|
|
'direct link: ' +
|
|
'</a>' +
|
|
'<input type="text" value="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '" onClick="this.setSelectionRange(0, this.value.length)"/>' +
|
|
' ' +
|
|
'<a class="twitter-share-button" href="https://twitter.com/share" data-via="slingtweet" data-url="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '">Tweet</a>' +
|
|
'</td>' +
|
|
'</tr>');
|
|
$.getScript("http://platform.twitter.com/widgets.js");
|
|
}
|
|
|
|
function log_file_upd(torrent){
|
|
if($('#'+torrent.infoHash).length){
|
|
var progress = (100 * torrent.downloaded / torrent.parsedTorrent.length).toFixed(1)
|
|
$('#'+torrent.infoHash).html(
|
|
'<td>' +
|
|
progress + '%' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'speed: ' + torrent.swarm.downloadSpeed() + '/s / ' + torrent.swarm.uploadSpeed() + '/s' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'<a href="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '"' +
|
|
' target="_blank">' +
|
|
'direct link: ' +
|
|
'</a>' +
|
|
'<input type="text" value="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '" onClick="this.setSelectionRange(0, this.value.length)"/>' +
|
|
' ' +
|
|
'<a class="twitter-share-button" href="https://twitter.com/share" data-via="slingtweet" data-url="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '">Tweet</a>' +
|
|
'</td>')
|
|
} else {
|
|
log_file_add(torrent);
|
|
}
|
|
}
|
|
function log_initialize() {
|
|
$('#link_container tr:last').after(
|
|
'<tr>' +
|
|
'<td>' +
|
|
'initializing' +
|
|
'</td>' +
|
|
'<td>' + '</td>' +
|
|
'<td>' +
|
|
'</td>' +
|
|
'</tr>');
|
|
}
|
|
|
|
function log_file_done(torrent,url,file){
|
|
log_file_upd(torrent);
|
|
$('#link_container tr:last').after(
|
|
'<tr>' +
|
|
'<td>' +
|
|
'100%' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'<a href="' + url + '"' +
|
|
' download="' + file.name + '">' +
|
|
'Download ' + file.name +
|
|
'</a>' +
|
|
'</td>' +
|
|
'<td>' +
|
|
'<a href="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '"' +
|
|
' target="_blank">' +
|
|
'direct link: ' +
|
|
'</a>' +
|
|
'<input type="text" value="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '" onClick="this.setSelectionRange(0, this.value.length)"/>' +
|
|
' ' +
|
|
'<a class="twitter-share-button" href="https://twitter.com/share" data-via="slingtweet" data-url="http://www.slingit.org/#!start;hash.' + torrent.infoHash + '">Tweet</a>' +
|
|
'</td>' +
|
|
'</tr>');
|
|
}
|
|
|
|
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);
|
|
} |