|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- var fs = require("fs");
- var http = require("http");
- var https = require("https");
- var pathlib = require("path");
- var urllib = require("url");
- var fsutil = require("../fsutil");
- var player = require("./player");
- var httpStream = require("./http-stream");
- var torrentStreamer = require("./torrent-streamer");
-
- exports.httpPath = "/playback";
-
- exports.cleanupFiles = [];
-
- var app;
- var conf
-
- exports.init = function(_app, _conf) {
- app = _app;
- conf = _conf;
- player.init(app, conf);
- httpStream.init(app, conf);
- torrentStreamer.init(app, conf);
- }
-
- exports.playTorrent = function(magnet, cb) {
-
- // Stream torrent
- torrentStreamer.stream(magnet, (err, filesize, filename) => {
- if (err)
- return cb(err);
-
- cb();
- });
- }
-
- exports.playTorrentPage = function(url, cb) {
- function findMagnet(str) {
- var rx = /['"](magnet:[^'"]+)['"]/;
- var match = str.match(rx);
- if (!match)
- return null;
-
- return match[1];
- }
-
- var urlobj = urllib.parse(url);
- var o = urlobj.protocol === "https:" ? https : http;
- o.request(urlobj, res => {
- var str = "";
-
- res
- .on("data", d => str += d )
- .on("error", err => {
- console.trace(err);
- cb();
- })
- .on("end", () => {
- var magnet = findMagnet(str);
- if (!magnet) {
- cb("No magnet link on page!");
- return;
- }
-
- exports.playTorrent(magnet, cb);
- });
- }).end();
- }
-
- exports.isPlaying = player.isPlaying;
-
- player.onstop = function() {
- torrentStreamer.stop();
- httpStream.stop();
-
- exports.cleanupFiles.forEach(f => {
- try {
- fs.unlink(f, err => { if (err) console.trace(err) });
- } catch (err) {
- console.log(err.toString());
- }
- });
- exports.cleanupFiles = [];
-
- fsutil.rmdir(conf.tmpdir+"/torrent-stream");
- }
|