Lightweight node app to use in place of plex to self host your video and movie collections. Running on just under 50MB of ram this is ideal for people looking to host videos on minimal hardware.
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.

65 lines
1.8 KiB

  1. const routes = require('express').Router();
  2. const utils = require("../utils");
  3. const userUtils = require("../user");
  4. const fs = require('fs');
  5. const videoManager = require("../videoManager");
  6. routes.get('/', (request, result) =>
  7. {
  8. var videoID = request.query.v;
  9. if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER ||
  10. userUtils.isValidAPI(request.query.api) ||
  11. videoManager.isPublicVideo(videoID))
  12. {
  13. const rootDir= (videoManager.isPublicVideo(videoID)) ?
  14. process.env.PUBLIC_DIR :
  15. process.env.PRIVATE_DIR;
  16. const path = rootDir + videoID;
  17. const stat = fs.statSync(path);
  18. const fileSize = stat.size;
  19. const range = request.headers.range;
  20. if (range)
  21. {
  22. const parts = range.replace(/bytes=/, "").split("-");
  23. const start = parseInt(parts[0], 10);
  24. const end = parts[1]
  25. ? parseInt(parts[1], 10)
  26. : fileSize-1;
  27. const chunksize = (end-start)+1;
  28. const file = fs.createReadStream(path, {start, end});
  29. const head =
  30. {
  31. 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
  32. 'Accept-Ranges': 'bytes',
  33. 'Content-Length': chunksize,
  34. 'Content-Type': 'video/mp4',
  35. };
  36. result.writeHead(206, head);
  37. file.pipe(result);
  38. }
  39. else
  40. {
  41. const head =
  42. {
  43. 'Content-Length': fileSize,
  44. 'Content-Type': 'video/mp4',
  45. };
  46. result.writeHead(200, head);
  47. fs.createReadStream(path).pipe(result);
  48. }
  49. }
  50. else
  51. {
  52. utils.printError(result, "You need to be logged in");
  53. }
  54. });
  55. module.exports = routes;