From a74333a0c4aed2a428e95fbdfd607cefcd640fdf Mon Sep 17 00:00:00 2001 From: jrtechs Date: Wed, 15 Aug 2018 20:47:32 -0400 Subject: [PATCH] Refactored image code --- img/image.js | 35 ----------------------------------- includes/includes.js | 30 ++++++++++++++++++++++++++++++ server.js | 6 +++--- 3 files changed, 33 insertions(+), 38 deletions(-) delete mode 100644 img/image.js diff --git a/img/image.js b/img/image.js deleted file mode 100644 index 086aec2..0000000 --- a/img/image.js +++ /dev/null @@ -1,35 +0,0 @@ -const utils = require('../utils/utils.js'); -const crypto = require('crypto'); - -module.exports= - { - /**Sends the user an image from the specified fileName. - * - * @param result - * @param fileName - */ - main: function(result, fileName, cache) - { - //result.contentType = 'image/png'; - - var img = cache.get(fileName); - if(img == null) - { - utils.include("." + fileName).then(function(content) - { - var eTag = crypto.createHash('md5').update(content).digest('hex'); - result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'}); - result.write(content); - result.end(); - cache.put(content); - }); - } - else - { - var eTag = crypto.createHash('md5').update(img).digest('hex'); - result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'}); - result.write(img); - result.end(); - } - } - }; \ No newline at end of file diff --git a/includes/includes.js b/includes/includes.js index 77d2347..eb8eb5e 100644 --- a/includes/includes.js +++ b/includes/includes.js @@ -83,5 +83,35 @@ module.exports = result.write(css); result.end(); } + }, + + + /**Sends the user an image from the specified fileName. + * + * @param result + * @param fileName + */ + sendImage: function(result, fileName, cache) + { + const img = cache.get(fileName); + if(img == null) + { + utils.include("." + fileName).then(function(content) + { + const eTag = crypto.createHash('md5').update(content).digest('hex'); + console.log(eTag); + result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'}); + result.write(content); + result.end(); + cache.put(content); + }); + } + else + { + const eTag = crypto.createHash('md5').update(img).digest('hex'); + result.writeHead(200, {'Content-Type': 'image/png', 'Cache-Control': 'public, max-age=2678400', 'ETag': '"' + eTag + '"'}); + result.write(img); + result.end(); + } } }; \ No newline at end of file diff --git a/server.js b/server.js index 2ec0eeb..f87062d 100644 --- a/server.js +++ b/server.js @@ -48,11 +48,11 @@ app.use(function(request, res) //handles image requests if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png") || filename.includes(".ico")) { - require("./img/image.js").main(res, filename, cache); + includes.sendImage(res, filename, cache); } else if(filename.includes("/css/") || filename.includes(".woff2") || filename.includes(".txt")) { - includes.sendCSS(res, filename, cache) + includes.sendCSS(res, filename, cache); } else if(filename.includes("/js/") || filename.includes(".js")) { @@ -66,7 +66,7 @@ app.use(function(request, res) { var file = ""; - var html = cache.get(filename); + const html = cache.get(filename); res.writeHead(200, {'Content-Type': 'text/html'}); if(html == null)