diff --git a/app.js b/app.js index c79b10b..1efab3a 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,17 @@ -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 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; @@ -16,7 +28,7 @@ var prettyBytes = function (num) { } exponent = Math.min(Math.floor(Math.log(num) / Math.log(1000)), units.length - 1); - num = (num / Math.pow(1000, exponent)).toFixed(2) * 1; + num = (num / Math.pow(1000, exponent)).toFixed(1) * 1; unit = units[exponent]; return (neg ? '-' : '') + num + ' ' + unit; @@ -29,58 +41,68 @@ angular.module('bTorrent', [], function ($compileProvider, $locationProvider) { 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() { + var dbg = function (string, torrent) { + if (debug) { + if (torrent) + $log.debug("%c" + torrent.name + " (" + torrent.infoHash + "): %c" + string, 'color: #33C3F0', 'color: #333'); + else + $log.debug("%cClient: %c" + string, 'color: #33C3F0', 'color: #333'); + } + }; + + var updateAll = function () { $scope.$apply(); }; - setInterval($scope.updateAll, 500); + setInterval(updateAll, 500); - $scope.client.done = function() { + $scope.client.done = function () { var done = true; - $scope.client.torrents.forEach(function(torrent) { - if(!torrent.done) { + $scope.client.torrents.forEach(function (torrent) { + if (!torrent.done) { done = false; } }); return done; }; - $scope.client.downloading = function() { + $scope.client.downloading = function () { var downloading = true; - $scope.client.torrents.forEach(function(torrent) { - if(torrent.done) { + $scope.client.torrents.forEach(function (torrent) { + if (torrent.done) { downloading = false; } }); return downloading; }; - $scope.uploadFile = function() { + $scope.uploadFile = function () { document.getElementById("fileUpload").click(); }; - $scope.uploadFile2 = function(elem) { + $scope.uploadFile2 = function (elem) { $scope.client.processing = true; - $scope.client.seed(elem.files, $scope.onTorrent); + dbg("Seeding " + elem.files[0].name); + $scope.client.seed(elem.files, $scope.onSeed); }; - $scope.fromInput = function() { - if(!$scope.torrentInput == "") { + $scope.fromInput = function () { + if (!$scope.torrentInput == "") { $scope.client.processing = true; + dbg("Adding " + $scope.torrentInput); $scope.client.add($scope.torrentInput, $scope.onTorrent); $scope.torrentInput = ""; } }; - $scope.toggleTorrent = function(torrent) { - if(torrent.showFiles) { + $scope.toggleTorrent = function (torrent) { + if (torrent.showFiles) { torrent.showFiles = false; $scope.sTorrent = null; } else { - $scope.client.torrents.forEach(function(t) { + $scope.client.torrents.forEach(function (t) { t.showFiles = false; }); torrent.showFiles = true; @@ -88,14 +110,19 @@ angular.module('bTorrent', [], function ($compileProvider, $locationProvider) { } }; - $scope.onTorrent = function(torrent) { + $scope.destroyedTorrent = function (err) { + if (err) throw err; + dbg("Destroyed torrent"); + }; + + $scope.onTorrent = function (torrent, isSeed) { $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) { + if (angular.isUndefined($scope.sTorrent) || $scope.sTorrent === null) { $scope.sTorrent = torrent; torrent.showFiles = true; } @@ -114,8 +141,6 @@ angular.module('bTorrent', [], function ($compileProvider, $locationProvider) { } }; - if(DEBUG) $log.debug("Downloading..." + torrent.infoHash); - torrent.files.forEach(function (file) { file.pSize = prettyBytes(file.length); file.status = "Downloading"; @@ -123,42 +148,41 @@ angular.module('bTorrent', [], function ($compileProvider, $locationProvider) { file.getBlobURL(function (err, url) { if (err) throw err; file.url = url; - if(DEBUG) $log.debug("Got BLOB " + file.url); + if(!isSeed) dbg("Finished downloading file " + file.name, torrent); file.status = "Ready"; $scope.$apply(); }); - if(DEBUG) $log.debug("FILE"); + if(!isSeed) dbg("Received file " + file.name + " metadata", torrent); }); torrent.on('download', function (chunkSize) { - if(DEBUG) $log.debug("DOWNLOAD"); - torrent.update(); + if(!isSeed) dbg("Downloaded chunk", torrent); }); torrent.on('upload', function (chunkSize) { - if(DEBUG) $log.debug("UPLOAD"); - torrent.update(); + dbg("Uploaded chunk", torrent); }); torrent.on('done', function () { - if(DEBUG) $log.debug("DONE"); + if(!isSeed) dbg("Done", torrent); 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); + dbg("Wire " + addr, torrent); }); setInterval(torrent.update, 500); torrent.update(); }; - if($location.hash() != '') { + $scope.onSeed = function(torrent) { + $scope.onTorrent(torrent, true); + }; + + if ($location.hash() != '') { $scope.client.processing = true; + dbg("Adding " + $location.hash()); client.add($location.hash(), $scope.onTorrent); } }).filter('html', function ($sce) { diff --git a/index.html b/index.html index 82a7b27..56bb22c 100644 --- a/index.html +++ b/index.html @@ -6,21 +6,21 @@ βTorrent: Browser WebTorrent Client - - + + - + + + + - - - -
-

βTorrent v0.3

+
+

βTorrent v0.4

@@ -44,6 +44,7 @@ Uploaded Peers Share + Actions @@ -52,8 +53,8 @@ - - {{torrent.name}}
{{torrent.pSize}} in {{torrent.files.length}} files + +
{{torrent.name}}
{{torrent.pSize}} in {{torrent.files.length}} files {{torrent.pDownloaded}} ({{torrent.pProgress}}%)
@ {{torrent.pDownloadSpeed}}/s {{torrent.tRemaining}} {{torrent.pUploaded}}
@ {{torrent.pUploadSpeed}}/s @@ -62,8 +63,9 @@ βTorrent | Magnet URI | .torrent -
{{torrent.infoHash}} +
{{torrent.infoHash}} + @@ -90,7 +92,7 @@
diff --git a/style.css b/style.css index 848b0de..5e63b47 100644 --- a/style.css +++ b/style.css @@ -3,6 +3,18 @@ body { width: 100%; } +header, footer, .center { + text-align: center; +} + +th, td { + padding-left: 10px; + padding-right: 10px; + max-width: 200px; + overflow: scroll; + white-space: nowrap; +} + .version { color: #ccc; font-size: 0.3em; @@ -18,9 +30,7 @@ body { font-size: 0.7em; } -footer, .center { - text-align: center; -} + .column, .columns { margin-left: 1%;