From a0589bdf19e53ca70144eb71ec960c54e7704a87 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 5 Feb 2014 21:27:06 -0800 Subject: add metadata dl link & now opens in transmission (fix #23) --- chrome/style.css | 4 ++++ chrome/window.html | 1 + lib/Torrent.js | 55 ++++++++++++++++++++++++++++++++++----------------- lib/TorrentManager.js | 15 ++++++++++++-- 4 files changed, 55 insertions(+), 20 deletions(-) diff --git a/chrome/style.css b/chrome/style.css index cb019c5..4ecdb3a 100644 --- a/chrome/style.css +++ b/chrome/style.css @@ -1,4 +1,8 @@ #console { height: 400px; overflow: scroll; +} + +.highlight { + font-weight: bold; } \ No newline at end of file diff --git a/chrome/window.html b/chrome/window.html index 707679f..ef700cf 100644 --- a/chrome/window.html +++ b/chrome/window.html @@ -12,6 +12,7 @@
  • DHT Nodes: 0
  • DHT Peers: 0
  • Connected Peers: 0
  • +
  • Download .torrent
  • Console

    diff --git a/lib/Torrent.js b/lib/Torrent.js index f6a71ef..4cdfcbf 100644 --- a/lib/Torrent.js +++ b/lib/Torrent.js @@ -21,6 +21,7 @@ function Torrent (uri, opts) { this.infoHash = info.infoHash this.displayName = info.displayName + this.metadata = null this.swarm = new Swarm(this.infoHash, opts.peerId, { dht: true }) @@ -128,25 +129,15 @@ function Torrent (uri, opts) { console.log('total_size: ' + dict.total_size) data.copy(wire.metadata, dict.piece * METADATA_BLOCK_SIZE) - var errorHandler = function (err) { - console.error('error' + err.toString()) + console.log('METADATA') + console.log(wire.metadata.toString()) + this.metadata = { + 'announce-list': [], + info: bncode.decode(wire.metadata), + // info_hash: } - - chrome.fileSystem.chooseEntry({ - type: 'saveFile', - suggestedName: this.displayName + '.torrent' - }, function (fileEntry) { - if (!fileEntry) - return - - fileEntry.createWriter(function (writer) { - writer.onerror = errorHandler - writer.onwriteend = function (e) { - console.log('write complete') - } - writer.write(new Blob([wire.metadata]), { type: 'text/plain' }) - }, errorHandler) - }) + console.log(this.metadata) + this.emit('metadata', this.metadata) } } }.bind(this)) @@ -154,6 +145,34 @@ function Torrent (uri, opts) { }.bind(this)) } +Torrent.prototype.downloadMetadata = function () { + if (!this.metadata) + return + + var errorHandler = function (err) { + console.error('error' + err.toString()) + } + + chrome.fileSystem.chooseEntry({ + type: 'saveFile', + suggestedName: this.displayName + '.torrent' + }, function (fileEntry) { + if (!fileEntry) + return + + fileEntry.createWriter(function (writer) { + writer.onerror = errorHandler + writer.onwriteend = function (e) { + console.log('write complete') + } + + var metadata = new Buffer(bncode.encode(this.metadata)) + + writer.write(new Blob([metadata]), { type: 'text/plain' }) + }.bind(this), errorHandler) + }.bind(this)) +} + Object.defineProperty(Torrent.prototype, 'numPeers', { get: function () { return this.swarm.wires.length diff --git a/lib/TorrentManager.js b/lib/TorrentManager.js index 265ef26..8c3e570 100644 --- a/lib/TorrentManager.js +++ b/lib/TorrentManager.js @@ -69,18 +69,28 @@ TorrentManager.prototype.add = function (uri) { // TODO: Add the torrent to the public DHT so peers know to find up }) + torrent.on('metadata', this.updateUI.bind(this)) + this.dht.setInfoHash(torrent.infoHash) this.dht.findPeers(MAX_PEERS) // TODO: should the DHT be concerned with max peers? this.updateUI() + + var t = this.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'] + $('.downloadMetadata').click(function () { + t.downloadMetadata() + }) } // TODO: show multiple torrents TorrentManager.prototype.updateUI = function () { // console.log('Peer ID: ' + this.peerId.toString('utf8')) // console.log('Node ID: ' + this.nodeId.toString('hex')) - $('.infoHash span').text(this.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'].infoHash) - $('.displayName span').text(this.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'].displayName) + + var t = this.torrents['d2474e86c95b19b8bcfdb92bc12c9d44667cfa36'] + + $('.infoHash span').text(t.infoHash) + $('.displayName span').text(t.displayName) $('.dhtNodes span').text(Object.keys(this.dht.nodes).length) $('.dhtPeers span').text(Object.keys(this.dht.peers).length) @@ -91,5 +101,6 @@ TorrentManager.prototype.updateUI = function () { connectedPeers += torrent.numPeers } $('.connectedPeers span').text(connectedPeers) + $('.downloadMetadata').toggleClass('highlight', !!t.metadata) } -- cgit v1.2.3