You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. var fs = require("fs");
  2. var pathlib = require("path");
  3. var player = require("./player");
  4. var httpStream = require("./http-stream");
  5. var torrentStreamer = require("./torrent-streamer");
  6. var subtitleFinder = require("./subtitle-finder");
  7. exports.httpPath = player.httpPath;
  8. var app;
  9. exports.init = function(_app, conf) {
  10. app = _app;
  11. player.init(app, conf);
  12. httpStream.init(app, conf);
  13. torrentStreamer.init(app, conf);
  14. subtitleFinder.init(app, conf);
  15. }
  16. exports.playFile = function(path, cb) {
  17. // Find file's length
  18. fs.stat(path, (err, stat) => {
  19. // Find subtitles
  20. subtitleFinder.find(stat.size, pathlib.basename(path), subFile => {
  21. // Play!
  22. player.play(path, subFile, cb);
  23. });
  24. });
  25. }
  26. exports.playUrl = function(path, cb) {
  27. // Just play, we won't bother finding subtitles
  28. player.play(path, null, cb);
  29. }
  30. exports.playTorrent = function(magnet, cb) {
  31. // Stream torrent
  32. torrentStreamer.stream(magnet, (err, filesize, filename) => {
  33. if (err)
  34. return cb(err);
  35. // Find subtitles
  36. subtitleFinder.find(filesize, filename, subFile => {
  37. // Play!
  38. player.play(app.getAddress()+httpStream.httpPath, subFile, cb);
  39. });
  40. });
  41. }
  42. exports.isPlaying = player.isPlaying;
  43. player.onstop = function() {
  44. torrentStreamer.stop();
  45. httpStream.stop();
  46. }