Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net

168 lines
3.6 KiB

  1. /**
  2. Utilities is a node modules created to make tasks like
  3. including html files easier for me programming.
  4. */
  5. //used for file io
  6. const fs = require('fs');
  7. const whiskers = require('whiskers');
  8. const includes = require("../includes/includes");
  9. module.exports=
  10. {
  11. /**
  12. * Method which return the contents of a file as a string
  13. * @param fileName
  14. * @return {*}
  15. */
  16. getFileContents: function(fileName)
  17. {
  18. try
  19. {
  20. return fs.readFileSync(fileName);
  21. }
  22. catch (e)
  23. {
  24. console.log("Could not find " + fileName);
  25. }
  26. return '';
  27. },
  28. /**
  29. *
  30. * @param fileName
  31. * @returns {any}
  32. */
  33. getFileAsJSON: function(fileName)
  34. {
  35. return JSON.parse(fs.readFileSync(fileName, 'utf8'));
  36. },
  37. /**
  38. * Returns all the contents of a file as a single line
  39. * with no break lines.
  40. *
  41. * @param fileName
  42. * @return {*}
  43. */
  44. getFileLine: function(fileName)
  45. {
  46. try
  47. {
  48. return fs.readFileSync(fileName, "utf8").split('\n').join('');
  49. }
  50. catch (e)
  51. {
  52. console.log("Could not find " + fileName);
  53. }
  54. return '';
  55. },
  56. /**
  57. * Function which is responsible for returning all post data.
  58. *
  59. * @param request sent by user in initial server call
  60. * @return the post data
  61. */
  62. getPostData: function(req)
  63. {
  64. return new Promise(function(resolve, reject)
  65. {
  66. if(req.method == 'POST')
  67. {
  68. var body = '';
  69. req.on('data', function (data)
  70. {
  71. body += data;
  72. //Kills request, don't steal my RAM!!
  73. //You can only download so much ram ;)
  74. if (body.length > 1e6)
  75. {
  76. req.connection.destroy();
  77. reject();
  78. }
  79. });
  80. req.on('end', function ()
  81. {
  82. resolve(body);
  83. });
  84. }
  85. else
  86. {
  87. resolve(0);
  88. }
  89. });
  90. },
  91. /**
  92. * Displays 404 error to user
  93. *
  94. * @param result
  95. * @returns {*}
  96. */
  97. print404: function(result)
  98. {
  99. includes.include("includes/html/404.html").then(function(html)
  100. {
  101. result.write(html);
  102. result.end();
  103. })
  104. },
  105. constructAdminPage: function(request, result, templateFiller)
  106. {
  107. var templateContext = Object();
  108. var promises = [];
  109. promises.push(includes.fetchTemplate("admin/adminMain.html"));
  110. promises.push(includes.printAdminHeader(templateContext));
  111. promises.push(includes.printFooter(templateContext));
  112. if(module.exports.loggedIn(request))
  113. {
  114. promises.push(templateFiller(templateContext));
  115. }
  116. else
  117. {
  118. //login
  119. const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  120. || request.connection.remoteAddress;
  121. promises.push(require("../admin/login").main(request, clientAddress,templateContext));
  122. }
  123. Promise.all(promises).then(function(content)
  124. {
  125. result.write(whiskers.render(content[0], templateContext));
  126. result.end();
  127. }).catch(function(err)
  128. {
  129. console.log(err);
  130. throw err;
  131. });
  132. },
  133. adminPostRoute: function(request, result, templateFiller)
  134. {
  135. },
  136. loggedIn(request)
  137. {
  138. return(request.session && request.session.user);
  139. }
  140. };