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.

63 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. routes.get('/', (request, result) =>
  7. {
  8. if(utils.checkPrivilege(request) >= utils.PRIVILEGE.MEMBER ||
  9. userUtils.isValidAPI(request.query.api))
  10. {
  11. const rootDir = configManager.getRootDirectory();
  12. var videoID = request.query.v;
  13. const path = rootDir + videoID;
  14. const stat = fs.statSync(path);
  15. const fileSize = stat.size;
  16. const range = request.headers.range;
  17. if (range)
  18. {
  19. const parts = range.replace(/bytes=/, "").split("-");
  20. const start = parseInt(parts[0], 10);
  21. const end = parts[1]
  22. ? parseInt(parts[1], 10)
  23. : fileSize-1;
  24. const chunksize = (end-start)+1;
  25. const file = fs.createReadStream(path, {start, end});
  26. const head =
  27. {
  28. 'Content-Range': `bytes ${start}-${end}/${fileSize}`,
  29. 'Accept-Ranges': 'bytes',
  30. 'Content-Length': chunksize,
  31. 'Content-Type': 'video/mp4',
  32. };
  33. result.writeHead(206, head);
  34. file.pipe(result);
  35. }
  36. else
  37. {
  38. const head =
  39. {
  40. 'Content-Length': fileSize,
  41. 'Content-Type': 'video/mp4',
  42. };
  43. result.writeHead(200, head);
  44. fs.createReadStream(path).pipe(result);
  45. }
  46. }
  47. else
  48. {
  49. console.log("invalid attempt to view video");
  50. result.status(401);
  51. result.send('None shall pass');
  52. }
  53. });
  54. module.exports = routes;