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.

66 lines
1.8 KiB

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