Personal blog written from scratch using Node.js, Bootstrap, and MySQL. https://jrtechs.net
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.

217 lines
5.3 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. templateContext.loggedIn = true;
  115. promises.push(templateFiller(templateContext));
  116. }
  117. else
  118. {
  119. //login
  120. const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  121. || request.connection.remoteAddress;
  122. promises.push(require("../admin/login").main(request, clientAddress,templateContext));
  123. }
  124. Promise.all(promises).then(function(content)
  125. {
  126. result.write(whiskers.render(content[0], templateContext));
  127. result.end();
  128. }).catch(function(err)
  129. {
  130. console.log(err);
  131. throw err;
  132. });
  133. },
  134. adminPostPageWithOutput: function(request, result, templateFiller)
  135. {
  136. module.exports.getPostData(request).then(function(postData)
  137. {
  138. var templateContext = Object();
  139. var promises = [];
  140. promises.push(includes.fetchTemplate("admin/adminMain.html"));
  141. promises.push(includes.printAdminHeader(templateContext));
  142. promises.push(includes.printFooter(templateContext));
  143. if (module.exports.loggedIn(request))
  144. {
  145. templateContext.loggedIn = true;
  146. promises.push(templateFiller(templateContext, postData));
  147. }
  148. else
  149. {
  150. //login
  151. const clientAddress = (request.headers['x-forwarded-for'] || '').split(',')[0]
  152. || request.connection.remoteAddress;
  153. promises.push(require("../admin/login").main(request, clientAddress, templateContext));
  154. }
  155. Promise.all(promises).then(function (content)
  156. {
  157. result.write(whiskers.render(content[0], templateContext));
  158. result.end();
  159. }).catch(function (err) {
  160. console.log(err);
  161. throw err;
  162. });
  163. });
  164. },
  165. adminPostRoute: function(request, templateFiller)
  166. {
  167. return new Promise(function(resolve, reject)
  168. {
  169. module.exports.getPostData(request).then(function(postData)
  170. {
  171. templateFiller(postData).then(function()
  172. {
  173. resolve();
  174. })
  175. }).catch(function(e)
  176. {
  177. reject(e);
  178. })
  179. })
  180. },
  181. loggedIn(request)
  182. {
  183. return(request.session && request.session.user);
  184. }
  185. };