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.

torrent-streamer.js 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var httpStream = require("./http-stream");
  2. var torrentStream = require("torrent-stream");
  3. var mediarx = /\.(mp4|mkv|mov|avi|ogv)$/;
  4. var engine;
  5. var conf;
  6. exports.init = function(app, _conf) {
  7. conf = _conf;
  8. }
  9. exports.stream = function(magnet, cb) {
  10. if (engine)
  11. return engine.destroy(() =>
  12. { engine = null; exports.stream(magnet, cb) });
  13. try {
  14. engine = torrentStream(magnet, {
  15. tmp: conf.tmpdir
  16. });
  17. } catch (err) {
  18. return cb(err.toString());
  19. }
  20. engine.on("ready", () => {
  21. var file = null;
  22. engine.files.forEach(f => {
  23. if (mediarx.test(f.name) &&
  24. (file == null || f.length > file.length)) {
  25. file = f;
  26. }
  27. });
  28. if (file == null)
  29. return cb("No media file in the torrent");
  30. file.select();
  31. httpStream.readStreamCreator = function(options) {
  32. var rs = file.createReadStream(options);
  33. rs.filesize = file.length;
  34. rs.filename = file.name;
  35. return rs;
  36. }
  37. cb(null, file.length, file.name);
  38. });
  39. }
  40. exports.stop = function() {
  41. if (engine != null)
  42. engine.destroy();
  43. }