diff options
Diffstat (limited to 'lib/Torrent.js')
-rw-r--r-- | lib/Torrent.js | 55 |
1 files changed, 37 insertions, 18 deletions
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 |