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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. var fs = require("fs");
  2. var pathlib = require("path");
  3. var web = require("webstuff");
  4. var play = require("./js/play");
  5. var fsutil = require("./js/fsutil");
  6. var conf = JSON.parse(fs.readFileSync("conf.json"));
  7. var app = web();
  8. play.init(app, conf);
  9. app.express.use((req, res, next) => {
  10. if (req.url === "/" && play.isPlaying())
  11. res.redirect(play.httpPath);
  12. else
  13. next();
  14. });
  15. app.static("web");
  16. app.post("/play/url", (req, res) => {
  17. req.parseBody((err, fields) => {
  18. if (!fields.url)
  19. return res.redirect("/");
  20. play.playUrl(fields.url, () => {
  21. res.redirect(play.httpPath);
  22. });
  23. });
  24. });
  25. app.post("/play/magnet", (req, res) => {
  26. req.parseBody((err, fields) => {
  27. if (!fields.magnet)
  28. return res.redirect("/");
  29. play.playTorrent(fields.magnet, () => {
  30. res.redirect(play.httpPath);
  31. });
  32. });
  33. });
  34. app.post("/play/file", (req, res) => {
  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. });