Commit a11cd545 authored by Cory Bryant's avatar Cory Bryant

Add last-modified check

parent f17e9a49
Pipeline #5243 passed with stage
in 39 seconds
......@@ -40,6 +40,8 @@ module.exports = function (options, plugins) {
}
return function processImgOptRequest(req, res, next) {
const ifModifiedSince = req.get("If-Modified-Since");
let originalMtime;
let parts = req.path.split("/");
parts.shift();
let mode = parts.shift();
......@@ -77,29 +79,46 @@ module.exports = function (options, plugins) {
}
try {
getRendition(function (renditionData) {
if (renditionData) {
let contentType;
let contentLength;
let body;
if (renditionData instanceof Buffer) {
contentType = 'image/jpeg';
contentLength = renditionData.length;
body = renditionData;
} else {
contentType = (renditionData.ContentType.startsWith('image') ? renditionData.ContentType : 'image/jpeg');
contentLength = renditionData.ContentLength;
body = renditionData.Body;
}
res.setHeader("cache-control", "max-age=31536000");
res.setHeader("content-type", contentType);
res.setHeader("content-length", contentLength);
res.send(body);
} else {
throw new Error("Missing rendition data");
}
});
if (ifModifiedSince) {
checkModifiedTime(function(modified) {
if (modified) {
doGet();
} else {
res.sendStatus(304);
}
});
} else {
doGet();
}
function doGet() {
getRendition(function (renditionData, lastModifiedTime) {
if (renditionData) {
let contentType;
let contentLength;
let body;
if (renditionData instanceof Buffer) {
contentType = 'image/jpeg';
contentLength = renditionData.length;
body = renditionData;
} else {
contentType = (renditionData.ContentType.startsWith('image') ? renditionData.ContentType : 'image/jpeg');
contentLength = renditionData.ContentLength;
body = renditionData.Body;
}
if (lastModifiedTime) {
res.setHeader("last-modified", lastModifiedTime);
}
res.setHeader("cache-control", "max-age=31536000");
res.setHeader("content-type", contentType);
res.setHeader("content-length", contentLength);
res.send(body);
} else {
throw new Error("Missing rendition data");
}
});
}
} catch (err) {
log.error("Problem generating image: ", err);
//res.status(500).send("<b>Problem generating image:</b> " + err);
......@@ -147,6 +166,23 @@ module.exports = function (options, plugins) {
}
function checkModifiedTime(cb) {
/* Get last modified date of original */
sc.get(slingImg + "/jcr:content", 0, function(err, data) {
if (!err && data && data._jcr_lastModified) {
originalMtime = Date.parse(data._jcr_lastModified);
}
if (!originalMtime || Date.parse(ifModifiedSince) < originalMtime) {
// Modified
cb(true);
} else {
// Not modified
cb(false);
}
});
};
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
......@@ -228,12 +264,19 @@ module.exports = function (options, plugins) {
function checkRenditionMtime(renditionMtime, fromRenditionBacking) {
/* Get last modified date of original */
sc.get(slingImg + "/jcr:content", 0, function(err, data) {
let originalMtime;
if (!err && data && data._jcr_lastModified) {
originalMtime = Math.floor(Date.parse(data._jcr_lastModified)/1000);
}
if (originalMtime) {
checkTimes();
} else {
sc.get(slingImg + "/jcr:content", 0, function(err, data) {
if (!err && data && data._jcr_lastModified) {
originalMtime = Date.parse(data._jcr_lastModified);
}
checkTimes();
});
}
function checkTimes() {
if (!originalMtime) {
// Cannot get the original image to compare
if (processor) {
......@@ -251,9 +294,9 @@ module.exports = function (options, plugins) {
generateRendition();
}
} else {
cb(renditionData);
cb(renditionData, renditionMtime);
}
});
}
};
//////////////////////////////////////////////////////////////////////////
......@@ -321,7 +364,7 @@ module.exports = function (options, plugins) {
}
function dealWithRendition(rendition) {
cb(rendition);
cb(rendition, new Date());
if (downloadExistingRendition) {
fs.writeFile(renditionPathOnDisk, rendition, function (error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment