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.

116 lines
3.1 KiB

  1. /**
  2. Includes.js
  3. File used for getting the header and footer
  4. */
  5. const utils = require('../utils/utils.js');
  6. const HEADER_FILE = "includes/header.html";
  7. const FOOTER_FILE = "includes/footer.html";
  8. const ADMIN_HEADER = "includes/adminHeader.html";
  9. const crypto = require('crypto');
  10. module.exports =
  11. {
  12. /** Appends the header html section to the result which is
  13. * sent to the user.
  14. *
  15. * @param result
  16. * @return {*} a promise retrieved from the utils.include function
  17. */
  18. printHeader: function()
  19. {
  20. return utils.include(HEADER_FILE);
  21. },
  22. /**
  23. * Appends the footer to the result object
  24. *
  25. * @return {*|Promise}
  26. */
  27. printFooter: function()
  28. {
  29. return utils.include(FOOTER_FILE);
  30. },
  31. /**
  32. * Displays the admin header
  33. *
  34. * @returns {*|Promise}
  35. */
  36. printAdminHeader()
  37. {
  38. return utils.include(ADMIN_HEADER);
  39. },
  40. /**Sends a css file to the user
  41. *
  42. * @param result
  43. * @param path
  44. * @return {*}
  45. */
  46. sendCSS: function(result, path, cache)
  47. {
  48. const css = cache.get(path);
  49. if(css == null)
  50. {
  51. utils.include("./" + path).then(function(content)
  52. {
  53. var eTag = crypto.createHash('md5').update(content).digest('hex');
  54. result.writeHead(200, {'Content-Type': 'text/css', 'Cache-Control':
  55. 'public, max-age=2678400', 'ETag': '"' + eTag + '"',
  56. 'Vary': 'Accept-Encoding'});
  57. result.write(content);
  58. result.end();
  59. cache.put(path, content);
  60. }).catch(function(error)
  61. {
  62. console.log(error);
  63. });
  64. }
  65. else
  66. {
  67. const eTag = crypto.createHash('md5').update(css).digest('hex');
  68. result.writeHead(200, {'Content-Type': 'text/css',
  69. 'Cache-Control': 'public, max-age=2678400',
  70. 'ETag': '"' + eTag + '"',
  71. 'Vary': 'Accept-Encoding'});
  72. result.write(css);
  73. result.end();
  74. }
  75. },
  76. /**Sends the user an image from the specified fileName.
  77. *
  78. * @param result
  79. * @param fileName
  80. */
  81. sendImage: function(result, fileName, cache)
  82. {
  83. const img = cache.get(fileName);
  84. if(img == null)
  85. {
  86. utils.include("." + fileName).then(function(content)
  87. {
  88. const eTag = crypto.createHash('md5').update(content).digest('hex');
  89. console.log(eTag);
  90. result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
  91. result.write(content);
  92. result.end();
  93. cache.put(content);
  94. });
  95. }
  96. else
  97. {
  98. const eTag = crypto.createHash('md5').update(img).digest('hex');
  99. result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'});
  100. result.write(img);
  101. result.end();
  102. }
  103. }
  104. };