diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-03-04 15:24:21 +0400 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-03-04 15:24:21 +0400 |
commit | 037bde2acdf897c8abfc4f0785bd523b3297f1d5 (patch) | |
tree | 757ca7e37d6ae3880631cee2819cdd2e85d5e17d /lib/torrent.js | |
parent | c53e981e8feb6bb98dab1d5727164aaca3089f01 (diff) |
add torrent drag-drop (incomplete)
Diffstat (limited to 'lib/torrent.js')
-rw-r--r-- | lib/torrent.js | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/lib/torrent.js b/lib/torrent.js index f2414b8..4acca3e 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -20,23 +20,35 @@ var EXTENDED_MESSAGES = { inherits(Torrent, EventEmitter) +/** + * Torrent + * ------- + * A torrent file + * + * @param {string|Buffer} uri magnet uri or torrent file + * @param {Object} opts options object + */ function Torrent (uri, opts) { var self = this if (!(self instanceof Torrent)) return new Torrent(uri, opts) EventEmitter.call(self) - var info = parseMagnetUri(uri) - if (!info.infoHash) - throw new Error('invalid torrent uri') - - self.infoHash = info.infoHash - self.name = info.name + if (typeof uri === 'string') { + // magnet uri + var info = parseMagnetUri(uri) + if (!info.infoHash) + throw new Error('invalid torrent uri') + self.infoHash = info.infoHash + self.name = info.name + } else if (Buffer.isBuffer(uri)) { + // torrent file + self._onMetadata(uri) + } self.peerId = opts.peerId self.torrentPort = opts.torrentPort self.dhtPort = opts.dhtPort - self.metadataRaw = null self.metadata = null self.parsedTorrent = null @@ -152,7 +164,7 @@ Torrent.prototype._onWire = function (wire) { // Only send metadata_size if we have complete metadata if (self.metadata) - extendedMessage.metadata_size = self.metadataRaw.length + extendedMessage.metadata_size = self.metadata.length wire.extended(0, extendedMessage) } @@ -291,16 +303,7 @@ Torrent.prototype._onUtMetadata = function (wire, buf) { // example: {'msg_type': 1, 'piece': 0, 'total_size': 3425} case 1: data.copy(wire.metadata, dict.piece * METADATA_BLOCK_LENGTH) - - self.metadataRaw = wire.metadata - self.metadata = bncode.decode(wire.metadata) - self.torrentFile = bncode.encode({ - 'announce-list': [], - infoHash: self.infoHash, - info: self.metadata - }) - - self._onMetadata() + self._onMetadata(wire.metadata) break // ut_metadata reject (peer doesn't have piece we requested) // {'msg_type': 2, 'piece': 0} @@ -310,20 +313,28 @@ Torrent.prototype._onUtMetadata = function (wire, buf) { } } -Torrent.prototype._onMetadata = function () { +Torrent.prototype._onMetadata = function (metadata) { var self = this + self.metadata = metadata + try { + var info = bncode.decode(metadata) + self.torrentFile = bncode.encode({ + 'announce-list': [], + infoHash: self.infoHash, + info: info + }) self.parsedTorrent = parseTorrent(self.torrentFile) } catch (e) { console.error(e) return } - console.log(self.parsedTorrent) + self.name = self.parsedTorrent.name - console.log('before storage instantiation') + self.infoHash = self.parsedTorrent.infoHash + self.storage = new Storage(self.parsedTorrent) - console.log('after storage instantiation') self.storage.on('piece', self._onStoragePiece.bind(self)) self.storage.on('file', function (file) { console.log('FILE', file.name) |