Browse Source

Refactored image code

pull/4/head
jrtechs 6 years ago
parent
commit
a74333a0c4
3 changed files with 33 additions and 38 deletions
  1. +0
    -35
      img/image.js
  2. +30
    -0
      includes/includes.js
  3. +3
    -3
      server.js

+ 0
- 35
img/image.js View File

@ -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();
}
}
};

+ 30
- 0
includes/includes.js View File

@ -83,5 +83,35 @@ module.exports =
result.write(css); result.write(css);
result.end(); 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();
}
} }
}; };

+ 3
- 3
server.js View File

@ -48,11 +48,11 @@ app.use(function(request, res)
//handles image requests //handles image requests
if(filename.includes("/img/") || filename.includes(".jpg") || filename.includes(".png") || filename.includes(".ico")) 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")) 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")) else if(filename.includes("/js/") || filename.includes(".js"))
{ {
@ -66,7 +66,7 @@ app.use(function(request, res)
{ {
var file = ""; var file = "";
var html = cache.get(filename);
const html = cache.get(filename);
res.writeHead(200, {'Content-Type': 'text/html'}); res.writeHead(200, {'Content-Type': 'text/html'});
if(html == null) if(html == null)

Loading…
Cancel
Save