btorrent/www/app.js

1 line
3 KiB
JavaScript
Raw Normal View History

var app,client,debug;client=new WebTorrent;debug=true;app=angular.module("bTorrent",[],["$compileProvider","$locationProvider",function(e,n){e.aHrefSanitizationWhitelist(/^\s*(https?|magnet|blob|javascript):/);n.html5Mode({enabled:true,requireBase:false}).hashPrefix("#")}]);app.controller("bTorrentCtrl",["$scope","$http","$log","$location",function(e,n,t,o){var r,i;e.client=client;e.seedIt=true;r=function(e,n){if(debug){if(n){t.debug("%c"+n.name+" ("+n.infoHash+"): %c"+e,"color: #33C3F0","color: #333")}else{t.debug("%cClient: %c"+e,"color: #33C3F0","color: #333")}}};i=function(){e.$apply()};setInterval(i,500);e.client.done=function(){var n;n=true;e.client.torrents.forEach(function(e){if(!e.done){n=false}});return n};e.client.downloading=function(){var n;n=true;e.client.torrents.forEach(function(e){if(e.done){n=false}});return n};e.uploadFile=function(){document.getElementById("fileUpload").click()};e.uploadFile2=function(n){e.client.processing=true;r("Seeding "+n.files[0].name);e.client.seed(n.files,e.onSeed)};e.fromInput=function(){if(!e.torrentInput===""){e.client.processing=true;r("Adding "+e.torrentInput);e.client.add(e.torrentInput,e.onTorrent);e.torrentInput=""}};e.toggleTorrent=function(n){if(n.showFiles){n.showFiles=false;e.sTorrent=null}else{e.client.torrents.forEach(function(e){e.showFiles=false});n.showFiles=true;e.sTorrent=n}};e.destroyedTorrent=function(e){if(e){throw e}r("Destroyed torrent")};e.onTorrent=function(n,t){e.client.processing=false;n.showFiles=false;n.fileName=n.name+".torrent";n.oTorrentFileURL=n.torrentFileURL;if(angular.isUndefined(e.sTorrent)||e.sTorrent===null){e.sTorrent=n;n.showFiles=true}n.update=function(){var e;n.pProgress=(100*n.progress).toFixed(1);n.pDownloaded=n.downloaded;n.pUploaded=n.uploaded;n.pUploadSpeed=n.uploadSpeed();n.pDownloadSpeed=n.downloadSpeed();if(n.done){n.tRemaining="Done"}else{e=moment.duration(n.timeRemaining/1e3,"seconds").humanize();n.tRemaining=e[0].toUpperCase()+e.substr(1)}};n.files.forEach(function(o){o.pSize=o.length;o.status="Downloading";o.url="javascript: return false;";o.getBlobURL(function(i,l){if(i){throw i}o.url=l;if(!t){r("Finished downloading file "+o.name,n)}o.status="Ready";e.$apply()});if(!t){r("Received file "+o.name+" metadata",n)}});n.on("download",function(e){if(!t){r("Downloaded chunk",n)}});n.on("upload",function(e){r("Uploaded chunk",n)});n.on("done",function(){if(!t){r("Done",n)}n.update()});n.on("wire",function(e,t){r("Wire "+t,n)});setInterval(n.update,500);n.update()};e.onSeed=function(n){e.onTorrent(n,true)};if(o.hash()!==""){e.client.processing=true;r("Adding "+o.hash());client.add(o.hash(),e.onTorrent)}}]);app.filter("html",["$sce",function(e){return function(n){return e.trustAsHtml(n)}}]);app.filter("pbytes",function(){return function(e){var n,t,o,r;n=void 0;o=void 0;t=e<0;r=["B","kB","MB","GB","TB","PB","EB","ZB","YB"];if(t){e=-e}if(e<1){return(t?"-":"")+e+" B"}n=Math.min(Math.floor(Math.log(e)/Math.log(1e3)),r.length-1);e=(e/Math.pow(1e3,n)).toFixed(1)*1;o=r[n];return(t?"-":"")+e+" "+o}});