Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFeross Aboukhadijeh <feross@feross.org>2014-02-06 09:27:06 +0400
committerFeross Aboukhadijeh <feross@feross.org>2014-02-06 09:27:06 +0400
commita0589bdf19e53ca70144eb71ec960c54e7704a87 (patch)
tree5c1cfeab518dcd3f5d99fa2d41efeb1684963bb9
parent6f6715f3594e20434e8b9be36ce0a4a983a4ce43 (diff)
add metadata dl link & now opens in transmission (fix #23)
-rw-r--r--chrome/style.css4
-rw-r--r--chrome/window.html1
-rw-r--r--lib/Torrent.js55
-rw-r--r--lib/TorrentManager.js15
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 @@
<li class="dhtNodes">DHT Nodes: <span>0</span></li>
<li class="dhtPeers">DHT Peers: <span>0</span></li>
<li class="connectedPeers">Connected Peers: <span>0</span></li>
+ <li class="downloadMetadata"><a href="#">Download .torrent</a></li>
</ul>
<h3>Console</h4>
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)
}