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.

server.js 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var fs = require("fs");
  2. var pathlib = require("path");
  3. var web = require("webstuff");
  4. var notify = require("./js/notify");
  5. var play = require("./js/play");
  6. var fsutil = require("./js/fsutil");
  7. var conf = JSON.parse(fs.readFileSync("conf.json"));
  8. var app = web();
  9. play.init(app, conf);
  10. app.express.use((req, res, next) => {
  11. if (req.url === "/" && play.isPlaying())
  12. res.redirect(play.httpPath);
  13. else
  14. next();
  15. });
  16. app.static("web");
  17. app.post("/play/url", (req, res) => {
  18. req.parseBody((err, fields) => {
  19. if (!fields.url)
  20. return res.redirect("/");
  21. function cb() {
  22. res.redirect(play.httpPath);
  23. }
  24. if (fields.url.indexOf("magnet:") === 0) {
  25. play.playTorrent(fields.url, cb);
  26. } else if (fields.url.indexOf("/torrent") !== -1) {
  27. play.playTorrentPage(fields.url, cb);
  28. } else {
  29. play.playUrl(fields.url, cb);
  30. }
  31. });
  32. });
  33. app.post("/play/file", (req, res) => {
  34. notify("Receiving file...");
  35. req.parseBody((err, fields, files) => {
  36. var file = files.file;
  37. if (file == null || !file.name || file.size === 0)
  38. return res.redirect("/");
  39. var fname = conf.tmpdir+"/uploaded-file"+pathlib.extname(file.name);
  40. fsutil.move(file.path, fname, err => {
  41. if (err) {
  42. console.trace(err);
  43. return res.redirect("/");
  44. }
  45. play.cleanupFiles.push(fname);
  46. play.playFile(fname, () => {
  47. res.redirect(play.httpPath);
  48. }, file.name);
  49. });
  50. });
  51. });
  52. app.get("/additional-links", (req, res) => {
  53. res.json(conf.additional_links);
  54. });