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.

81 lines
2.7 KiB

  1. const fileIO = require('./fileIO');
  2. function fetchInTemplate(templateContext, templateKey, filename)
  3. {
  4. templateContext[templateKey] = fileIO.getFile(filename);
  5. }
  6. const PRIVILEGE = {NOBODY: 0, MEMBER: 1, ADMIN: 2};
  7. /** Template engine */
  8. const whiskers = require('whiskers');
  9. module.exports =
  10. {
  11. renderHTML: function(request, result, templateFile, templateDependencyFunction)
  12. {
  13. var templateContext = Object();
  14. var prom = [];
  15. prom.push(fileIO.getFile("./html/mainTemplate.html"));
  16. prom.push(fetchInTemplate(templateContext, "header", "./html/header.html"));
  17. prom.push(fetchInTemplate(templateContext, "footer", "./html/footer.html"));
  18. if(module.exports.checkPrivilege(request) >= PRIVILEGE.MEMBER)
  19. {
  20. templateContext.loggedIn = true;
  21. if(module.exports.checkPrivilege(request) === PRIVILEGE.ADMIN)
  22. templateContext.admin = true;
  23. }
  24. else
  25. {
  26. prom.push(fetchInTemplate(templateContext, "login","./html/login.html"));
  27. }
  28. if(templateDependencyFunction !== null)
  29. prom.push(templateDependencyFunction(templateContext, request));
  30. prom.push(fetchInTemplate(templateContext, "main","./html/" + templateFile));
  31. Promise.all(prom).then(function(content)
  32. {
  33. result.write(whiskers.render(content[0], templateContext));
  34. result.end();
  35. });
  36. },
  37. PRIVILEGE:
  38. {
  39. NOBODY: 0,
  40. MEMBER: 1,
  41. ADMIN: 2
  42. },
  43. checkPrivilege: function(request)
  44. {
  45. if(request.session.login !== true)
  46. return module.exports.PRIVILEGE.NOBODY;
  47. else if(request.session.admin === true)
  48. return module.exports.PRIVILEGE.ADMIN;
  49. return module.exports.RIVILEGE.MEMBER;
  50. },
  51. printError: function(result, errorMessage)
  52. {
  53. var templateContext = Object();
  54. var prom = [];
  55. prom.push(fileIO.getFile("./html/mainTemplate.html"));
  56. prom.push(fetchInTemplate(templateContext, "header", "./html/header.html"));
  57. prom.push(fetchInTemplate(templateContext, "footer", "./html/footer.html"));
  58. prom.push(fetchInTemplate(templateContext, "main", "./html/error.html"));
  59. templateContext.errorMessage = errorMessage;
  60. Promise.all(prom).then(function(content)
  61. {
  62. result.write(whiskers.render(content[0], templateContext));
  63. result.end();
  64. });
  65. }
  66. };