From 0eb35e63b71e72857637e1bc86e213aed0ff4175 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 14 Oct 2014 00:13:51 -0700 Subject: Use --index to pick video file to stream (fix #150) --- bin/cmd.js | 15 ++++++++++++++- index.js | 24 ------------------------ lib/server.js | 18 +++++++++--------- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 795f5a3..20c9f96 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -30,6 +30,7 @@ var argv = minimist(process.argv.slice(2), { b: 'blocklist', t: 'subtitles', l: 'list', + i: 'index', n: 'no-quit', r: 'remove', q: 'quiet', @@ -92,7 +93,8 @@ if (argv.help || !torrentId) { -p, --port [number] change the http port [default: 9000] -b, --blocklist [path] use the specified blocklist -t, --subtitles [file] load subtitles file - -l, --list list available files in torrent + -l, --list list available files in torrent (with indexes) + -i, --index stream a particular file from torrnet (by index) -n, --no-quit do not quit webtorrent on vlc exit -r, --remove remove downloaded files on exit -q, --quiet silence stdout @@ -182,10 +184,21 @@ torrent.on('ready', function () { var filename, swarm, wires function onTorrent (torrent) { + client.torrent = torrent + filename = torrent.name swarm = torrent.swarm wires = torrent.swarm.wires + // if no index specified, use largest file + var index = (typeof argv.index === 'number') + ? argv.index + : torrent.files.indexOf(torrent.files.reduce(function (a, b) { + return a.length > b.length ? a : b + })) + client.index = index + torrent.files[index].select() + if (argv.list) { torrent.files.forEach(function (file, i) { clivas.line('{3+bold:' + i + '} : {magenta:' + file.name + '}') diff --git a/index.js b/index.js index e575fca..26e1eef 100644 --- a/index.js +++ b/index.js @@ -152,9 +152,6 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) { opts.client = self opts.storage = opts.storage || self.storage - // TODO: fix this to work with multiple torrents. this should probably be in cmd.js - self.index = opts.index - var torrent = new Torrent(torrentId, extend({ client: self }, opts)) self.torrents.push(torrent) @@ -178,7 +175,6 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) { // Emit 'torrent' when a torrent is ready to be used debug('torrent') self.emit('torrent', torrent) - self._onTorrent(torrent) }) return torrent @@ -280,23 +276,3 @@ WebTorrent.prototype.destroy = function (cb) { parallel(tasks, cb) } - -// TODO: this probably belongs in cmd.js -WebTorrent.prototype._onTorrent = function (torrent) { - var self = this - debug('on torrent') - - // if no index specified, use largest file - if (typeof torrent.index !== 'number') { - var largestFile = torrent.files.reduce(function (a, b) { - return a.length > b.length ? a : b - }) - torrent.index = torrent.files.indexOf(largestFile) - } - - torrent.files[torrent.index].select() - - // TODO: this won't work with multiple torrents - self.index = torrent.index - self.torrent = torrent -} diff --git a/lib/server.js b/lib/server.js index a8c9973..03eef94 100644 --- a/lib/server.js +++ b/lib/server.js @@ -5,7 +5,7 @@ var pump = require('pump') var rangeParser = require('range-parser') var url = require('url') -module.exports = function Server (webtorrent, port) { +module.exports = function Server (client, port) { var server = http.createServer() server.on('connection', function (socket) { @@ -31,21 +31,21 @@ module.exports = function Server (webtorrent, port) { if (req.headers.origin) res.setHeader('Access-Control-Allow-Origin', req.headers.origin) - var u = url.parse(req.url) - if (u.pathname === '/favicon.ico') return res.end() - if (u.pathname === '/') u.pathname = '/' + webtorrent.index - var i = Number(u.pathname.slice(1)) + var pathname = url.parse(req.url).pathname + if (pathname === '/favicon.ico') return res.end() + if (pathname === '/') pathname = '/' + client.index - if (isNaN(i) || i >= webtorrent.torrent.files.length) { + var index = Number(pathname.slice(1)) + if (Number.isNaN(index) || index >= client.torrent.files.length) { res.statusCode = 404 return res.end() } - if (webtorrent.torrent) onTorrent(webtorrent.torrent) - else webtorrent.once('torrent', onTorrent) + if (client.torrent) onTorrent(client.torrent) + else client.once('torrent', onTorrent) function onTorrent (torrent) { - var file = torrent.files[i] + var file = torrent.files[index] res.setHeader('Accept-Ranges', 'bytes') res.setHeader('Content-Type', mime.lookup(file.name)) -- cgit v1.2.3