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-04-23 13:56:13 +0400
committerFeross Aboukhadijeh <feross@feross.org>2014-04-23 13:56:13 +0400
commit94e29512fe0a77c0497e293d85b3694cd046a928 (patch)
tree1c02f00531debc7fcef74175bd12a96e81fae275 /index.js
parent1d69988e9fde758af43276eb90f5620ced4bd268 (diff)
support filesystem urls, move support to module
Diffstat (limited to 'index.js')
-rw-r--r--index.js33
1 files changed, 33 insertions, 0 deletions
diff --git a/index.js b/index.js
index 0049c10..04ee4e6 100644
--- a/index.js
+++ b/index.js
@@ -3,6 +3,7 @@
module.exports = WebTorrent
var Client = function () {} // require('bittorrent-client')
+var fs = require('fs')
var http = require('http')
var inherits = require('inherits')
var mime = require('mime')
@@ -25,6 +26,38 @@ function WebTorrent (torrent, opts) {
// completed and handle it by stopping fetching additional data from the network
}
+WebTorrent.prototype.add = function (torrentId, cb) {
+ var self = this
+
+ if (Client.toInfoHash(torrentId)) {
+ // magnet uri, info hash, or torrent file (all can be handled by bittorrent-client)
+ var torrent = Client.prototype.add.call(self, torrentId)
+ process.nextTick(function () {
+ cb(null, torrent)
+ })
+ } else if (/^https?:/.test(torrentId)) {
+ // http or https url to torrent file
+ http.get(torrentId, function (res) {
+ res.pipe(concat(function (torrent) {
+ var torrent = Client.prototype.add.call(self, torrent)
+ cb(null, torrent)
+ }))
+ }).on('error', function (err) {
+ cb(new Error('Error downloading torrent from ' + torrentId + '\n' + err.message))
+ })
+ } else {
+ // assume it's a filesystem path
+ fs.readFile(torrentId, function (err, torrent) {
+ if (err) {
+ return cb(new Error('Cannot add torrent. Require one of: magnet uri, ' +
+ 'info hash, torrent file, http url, or filesystem path'))
+ }
+ var torrent = Client.prototype.add.call(self, torrent)
+ cb(null, torrent)
+ })
+ }
+}
+
WebTorrent.prototype._startServer = function () {
var self = this
self.server = http.createServer()