Initital fiels
This commit is contained in:
parent
6da8813af1
commit
3d8c009e91
3 changed files with 324 additions and 0 deletions
180
app.js
Normal file
180
app.js
Normal file
|
@ -0,0 +1,180 @@
|
|||
var client = new WebTorrent({rtcConfig: {"iceServers":[{"url":"stun:23.21.150.121","urls":"stun:23.21.150.121"},{"url":"turn:global.turn.twilio.com:3478?transport=udp","username":"00bb844e6c2a07d4ed3e22a6edd6da6a715714a7c3eb118dc20246e5e0cc50c1","credential":"Wv1IxOBVhm4CGqoWYQWQ0X4Ia0Va7p2SOENv/S7M9Vg=","urls":"turn:global.turn.twilio.com:3478?transport=udp"}]}});
|
||||
var DEBUG = true;
|
||||
|
||||
var prettyBytes = function (num) {
|
||||
var exponent;
|
||||
var unit;
|
||||
var neg = num < 0;
|
||||
var units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
||||
|
||||
if (neg) {
|
||||
num = -num;
|
||||
}
|
||||
|
||||
if (num < 1) {
|
||||
return (neg ? '-' : '') + num + ' B';
|
||||
}
|
||||
|
||||
exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1);
|
||||
num = (num / Math.pow(1000, exponent)).toFixed(2) * 1;
|
||||
unit = units[exponent];
|
||||
|
||||
return (neg ? '-' : '') + num + ' ' + unit;
|
||||
};
|
||||
|
||||
angular.module('bTorrent', [], function ($compileProvider, $locationProvider) {
|
||||
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|magnet|blob|javascript):/);
|
||||
$locationProvider.html5Mode({
|
||||
enabled: true,
|
||||
requireBase: false
|
||||
}).hashPrefix('#');
|
||||
}).controller('bTorrentCtrl', function ($scope, $http, $log, $location) {
|
||||
if(DEBUG) $log.debug($location.hash());
|
||||
$scope.client = client;
|
||||
$scope.seedIt = true;
|
||||
|
||||
$scope.updateAll = function() {
|
||||
$scope.$apply();
|
||||
};
|
||||
|
||||
setInterval($scope.updateAll, 500);
|
||||
|
||||
$scope.client.done = function() {
|
||||
var done = true;
|
||||
$scope.client.torrents.forEach(function(torrent) {
|
||||
if(!torrent.done) {
|
||||
done = false;
|
||||
}
|
||||
});
|
||||
return done;
|
||||
};
|
||||
|
||||
$scope.client.downloading = function() {
|
||||
var downloading = true;
|
||||
$scope.client.torrents.forEach(function(torrent) {
|
||||
if(torrent.done) {
|
||||
downloading = false;
|
||||
}
|
||||
});
|
||||
return downloading;
|
||||
};
|
||||
|
||||
$scope.uploadFile = function() {
|
||||
document.getElementById("fileUpload").click();
|
||||
};
|
||||
$scope.uploadFile2 = function(elem) {
|
||||
$scope.client.processing = true;
|
||||
$scope.client.seed(elem.files, $scope.onSeed);
|
||||
};
|
||||
|
||||
$scope.fromInput = function() {
|
||||
if(!$scope.torrentInput == "") {
|
||||
$scope.client.processing = true;
|
||||
$scope.client.add($scope.torrentInput, $scope.onTorrent);
|
||||
$scope.torrentInput = "";
|
||||
}
|
||||
};
|
||||
|
||||
$scope.toggleTorrent = function(torrent) {
|
||||
if(torrent.showFiles) {
|
||||
torrent.showFiles = false;
|
||||
$scope.sTorrent = null;
|
||||
} else {
|
||||
$scope.client.torrents.forEach(function(t) {
|
||||
t.showFiles = false;
|
||||
});
|
||||
torrent.showFiles = true;
|
||||
$scope.sTorrent = torrent;
|
||||
}
|
||||
};
|
||||
|
||||
$scope.onSeed = function(torrent) {
|
||||
if($scope.seedIt) {
|
||||
if(DEBUG) $log.debug("Seed it");
|
||||
$http.get("seedIt.php?hash=" + torrent.infoHash).then(function(response) {
|
||||
if(DEBUG) $log.debug("Sent to seeder!");
|
||||
}, function(response) {
|
||||
if(DEBUG) $log.debug("Error sending to seeder!");
|
||||
});
|
||||
}
|
||||
$scope.onTorrent(torrent);
|
||||
};
|
||||
|
||||
$scope.onTorrent = function(torrent) {
|
||||
$scope.client.processing = false;
|
||||
torrent.showFiles = false;
|
||||
torrent.pSize = prettyBytes(torrent.length);
|
||||
torrent.fileName = torrent.name + '.torrent';
|
||||
torrent.oTorrentFileURL = torrent.torrentFileURL;
|
||||
|
||||
if(angular.isUndefined($scope.sTorrent) || $scope.sTorrent === null) {
|
||||
$scope.sTorrent = torrent;
|
||||
torrent.showFiles = true;
|
||||
}
|
||||
|
||||
torrent.update = function () {
|
||||
torrent.pProgress = (100 * torrent.progress).toFixed(1);
|
||||
torrent.pDownloaded = prettyBytes(torrent.downloaded);
|
||||
torrent.pUploaded = prettyBytes(torrent.uploaded);
|
||||
torrent.pUploadSpeed = prettyBytes(torrent.uploadSpeed());
|
||||
torrent.pDownloadSpeed = prettyBytes(torrent.downloadSpeed());
|
||||
if (torrent.done) {
|
||||
torrent.tRemaining = 'Done'
|
||||
} else {
|
||||
var remaining = moment.duration(torrent.timeRemaining / 1000, 'seconds').humanize();
|
||||
torrent.tRemaining = remaining[0].toUpperCase() + remaining.substr(1);
|
||||
}
|
||||
};
|
||||
|
||||
if(DEBUG) $log.debug("Downloading..." + torrent.infoHash);
|
||||
|
||||
torrent.files.forEach(function (file) {
|
||||
file.pSize = prettyBytes(file.length);
|
||||
file.status = "Downloading";
|
||||
file.url = 'javascript: return false;';
|
||||
file.getBlobURL(function (err, url) {
|
||||
if (err) throw err;
|
||||
file.url = url;
|
||||
if(DEBUG) $log.debug("Got BLOB " + file.url);
|
||||
file.status = "Ready";
|
||||
$scope.$apply();
|
||||
});
|
||||
if(DEBUG) $log.debug("FILE");
|
||||
});
|
||||
|
||||
torrent.on('download', function (chunkSize) {
|
||||
if(DEBUG) $log.debug("DOWNLOAD");
|
||||
torrent.update();
|
||||
});
|
||||
|
||||
torrent.on('upload', function (chunkSize) {
|
||||
if(DEBUG) $log.debug("UPLOAD");
|
||||
torrent.update();
|
||||
});
|
||||
|
||||
torrent.on('done', function () {
|
||||
if(DEBUG) $log.debug("DONE");
|
||||
torrent.update();
|
||||
});
|
||||
|
||||
torrent.on('wire', function (wire, addr) {
|
||||
if(DEBUG) $log.debug("WIRE");
|
||||
});
|
||||
|
||||
torrent.on('wire', function (wire, addr) {
|
||||
console.log('connected to peer with address ' + addr);
|
||||
});
|
||||
|
||||
setInterval(torrent.update, 500);
|
||||
torrent.update();
|
||||
};
|
||||
|
||||
if($location.hash() != '') {
|
||||
$scope.client.processing = true;
|
||||
client.add($location.hash(), $scope.onTorrent);
|
||||
}
|
||||
}).filter('html', function ($sce) {
|
||||
return function (input) {
|
||||
return $sce.trustAsHtml(input);
|
||||
}
|
||||
});
|
99
index.html
Normal file
99
index.html
Normal file
|
@ -0,0 +1,99 @@
|
|||
<!DOCTYPE html>
|
||||
<html ng-app="bTorrent" lang="en">
|
||||
<head>
|
||||
<base href="">
|
||||
<meta charset="UTF-8">
|
||||
<title>βTorrent: Browser WebTorrent Client</title>
|
||||
|
||||
<meta name="description" content="βTorrent: Browser WebTorrent Client">
|
||||
<meta name="keywords" content="βTorrent, btorrent, webtorrent client, webtorrent">
|
||||
<meta name="author" content="Diego Rodríguez Baquero">
|
||||
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/g/webtorrent@0.62.3,momentjs@2.10.6,angularjs@1.4.6"></script>
|
||||
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/3.0.3/normalize.min.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css">
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header style="text-align: center">
|
||||
<h1>βTorrent<span class="version"> v0.3</span></h1>
|
||||
</header>
|
||||
<div class="container" ng-controller="bTorrentCtrl" ng-cloak>
|
||||
<div class="row">
|
||||
<div class="twelve columns">
|
||||
<div class="row">
|
||||
<div class="five columns"><input type="text" class="u-full-width" placeholder="magnet link or hash" id="torrentInput" ng-model="torrentInput"></div>
|
||||
<div class="two columns"><button class="button-primary" ng-click="fromInput()"><i class="fa fa-download"></i> Download</button></div>
|
||||
<div class="five columns u-pull-right">
|
||||
<input type="file" style="display: none;" id="fileUpload" onchange="angular.element(this).scope().uploadFile2(this)">
|
||||
<label class="u-pull-right">
|
||||
<input type="checkbox" ng-model="seedIt">
|
||||
<span class="label-body">Help me seed it (10MB Max)</span>
|
||||
<button class="button-primary" ng-click="uploadFile()" ><i class="fa fa-upload"></i> Seed a file</button>
|
||||
</label>
|
||||
</div>
|
||||
</div>sdf
|
||||
<table class="u-full-width">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th ng-hide="client.done()">Downloaded</th>
|
||||
<th ng-hide="client.done()">Remaining</th>
|
||||
<th ng-hide="client.downloading()">Uploaded</th>
|
||||
<th>Peers</th>
|
||||
<th>Share</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody ng-hide="client.torrents.length">
|
||||
<tr>
|
||||
<td colspan="100" class="center">Add a torrent o seed a file!</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tbody ng-repeat="torrent in client.torrents" ng-if="torrent.name">
|
||||
<tr class="torrentRow" ng-class="{selectedTorrent: torrent.showFiles}" ng-click="toggleTorrent(torrent)">
|
||||
<td><i class="fa fa-cloud-download" ng-hide="torrent.done"></i><i class="fa fa-check" ng-show="torrent.done"></i> {{torrent.name}}<br /><span class="subInfo">{{torrent.pSize}} in {{torrent.files.length}} files</span></td>
|
||||
<td ng-hide="client.done()">{{torrent.pDownloaded}} <span class="subInfo">({{torrent.pProgress}}%)</span><br /><span class="subInfo">@ {{torrent.pDownloadSpeed}}/s</span></td>
|
||||
<td ng-hide="client.done()">{{torrent.tRemaining}}</td>
|
||||
<td ng-hide="client.downloading()">{{torrent.pUploaded}}<br /><span class="subInfo">@ {{torrent.pUploadSpeed}}/s</span></td>
|
||||
<td>{{torrent.swarm.wires.length}}</td>
|
||||
<td>
|
||||
<a href="#{{torrent.infoHash}}" target="_blank">βTorrent</a>
|
||||
| <a href="{{torrent.magnetURI}}" target="_blank">Magnet URI</a>
|
||||
| <a href="{{torrent.oTorrentFileURL}}" target="_blank" download="{{torrent.fileName}}">.torrent</a>
|
||||
<br><span class="subInfo">{{torrent.infoHash}}</span>
|
||||
</td>
|
||||
</tr>
|
||||
<tr ng-show="torrent.showFiles">
|
||||
<td class="files" colspan="100">
|
||||
<div class="row">
|
||||
<div class="two columns center"><i class="fa fa-file"></i> <strong>Files:</strong></div>
|
||||
<div class="ten columns fix-height">
|
||||
<ul class="no-margin">
|
||||
<li ng-repeat="file in torrent.files">
|
||||
<span ng-hide="file.done">{{file.status}}: {{file.name}}</span><a href="{{file.url}}" download="{{file.name}}" target="_blank" ng-show="file.done">{{file.name}}</a> <span class="subInfo">{{file.pSize}}</span>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
<tbody ng-show="client.processing">
|
||||
<tr>
|
||||
<td colspan="100" class="center">Please wait a few seconds!</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<footer>
|
||||
<small>Made in Bogotá, Colombia by <a href="http://diegorbaquero.com">DiegoRBaquero</a><br><small><a href="https://webtorrent.io">WebTorrent</a> is powered by JavaScript and WebRTC. Works in Chrome, Firefox, and Opera (desktop and Android).</small></p>
|
||||
</footer>
|
||||
<script src="app.js"></script>
|
||||
</body>
|
||||
</html>
|
45
style.css
Normal file
45
style.css
Normal file
|
@ -0,0 +1,45 @@
|
|||
body {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.version {
|
||||
color: #ccc;
|
||||
font-size: 0.3em;
|
||||
}
|
||||
|
||||
.torrentRow {
|
||||
line-height: 1.2;
|
||||
border-bottom: 0;
|
||||
}
|
||||
|
||||
.subInfo {
|
||||
color: #bbbbbb;
|
||||
font-size: 0.7em;
|
||||
}
|
||||
|
||||
footer, .center {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.column, .columns {
|
||||
margin-left: 1%;
|
||||
}
|
||||
|
||||
.files {
|
||||
padding-top: 1px;
|
||||
padding-bottom: 1px;
|
||||
}
|
||||
|
||||
.fix-height {
|
||||
max-height: 24px;
|
||||
}
|
||||
|
||||
.no-margin {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.selectedTorrent {
|
||||
background: rgb(100,100,100);
|
||||
background: rgba(100,100,100,.06);
|
||||
}
|
Loading…
Reference in a new issue