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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. var fs = require("fs");
  2. var pathlib = require("path");
  3. var fsutil = require("../fsutil");
  4. var player = require("./player");
  5. var httpStream = require("./http-stream");
  6. var torrentStreamer = require("./torrent-streamer");
  7. var subtitleFinder = require("./subtitle-finder");
  8. exports.httpPath = player.httpPath;
  9. exports.cleanupFiles = [];
  10. var app;
  11. var conf
  12. exports.init = function(_app, _conf) {
  13. app = _app;
  14. conf = _conf;
  15. player.init(app, conf);
  16. httpStream.init(app, conf);
  17. torrentStreamer.init(app, conf);
  18. subtitleFinder.init(app, conf);
  19. }
  20. /*
  21. * Filename is optional; in case you want to provide a filename for subtitles
  22. * but want a different path
  23. */
  24. exports.playFile = function(path, cb, filename) {
  25. filename = filename || pathlib.basename(path);
  26. // Find file's length
  27. fs.stat(path, (err, stat) => {
  28. if (err) {
  29. console.trace(err);
  30. return cb();
  31. }
  32. // Find subtitles
  33. subtitleFinder.find(stat.size, filename, subFile => {
  34. exports.cleanupFiles.push(subFile);
  35. // Play!
  36. player.play(path, subFile, cb);
  37. });
  38. });
  39. }
  40. exports.playUrl = function(path, cb) {
  41. // Just play, we won't bother finding subtitles
  42. player.play(path, null, cb);
  43. }
  44. exports.playTorrent = function(magnet, cb) {
  45. // Stream torrent
  46. torrentStreamer.stream(magnet, (err, filesize, filename) => {
  47. if (err)
  48. return cb(err);
  49. // Find subtitles
  50. subtitleFinder.find(filesize, filename, subFile => {
  51. exports.cleanupFiles.push(subFile);
  52. // Play!
  53. player.play(app.getAddress()+httpStream.httpPath, subFile, cb);
  54. });
  55. });
  56. }
  57. exports.isPlaying = player.isPlaying;
  58. player.onstop = function() {
  59. torrentStreamer.stop();
  60. httpStream.stop();
  61. exports.cleanupFiles.forEach(f => {
  62. try {
  63. fs.unlink(f, err => { if (err) console.trace(err) });
  64. } catch (err) {
  65. console.trace(err);
  66. }
  67. });
  68. exports.cleanupFiles = [];
  69. fsutil.rmdir(conf.tmpdir+"/torrent-stream");
  70. }