...
 
Commits (15)
build:
stage: build
script:
- yarn install
artifacts:
name: "$CI_PROJECT_NAME-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA"
paths:
- node_modules/
tags:
- node
include:
- project: 'entropy/gitlab-scripts'
file: 'node.gitlab-ci.yml'
test:
stage: test
script:
- yarn test
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
tags:
- node
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
examples
tests/
.gitlab-ci.yml
.nvmrc
.gitignore
docs
coverage/
......@@ -111,19 +111,27 @@ module.exports=function(options, plugins){
var renditionBackingConfig={
renditionPath: renditionPathWithoutRoot,
renditionPathOnDisk: renditionPath,
downloadRendition: options.downloadExistingRendition,
emailError
}
try{
ensureOriginalIsOnDisk(function(err){
if(err){allDone(err);return;}
getRendition(function(err){
if(err){allDone(err);return;}
res.setHeader("cache-control", "max-age=31536000");
staticHandler(req,res,next);
});
});
getRendition(function(err, renditionData){
if (err) {
allDone(err);
return;
}
res.setHeader("cache-control", "max-age=31536000");
if (renditionData) {
let contentType = (renditionData.ContentType.startsWith('image') ? renditionData.ContentType : 'image/jpeg');
res.setHeader("content-type", contentType);
res.setHeader("content-length", renditionData.ContentLength);
res.send(renditionData.Body);
} else {
staticHandler(req,res,next);
}
});
}catch(err){
allDone(err);
}
......@@ -224,44 +232,69 @@ module.exports=function(options, plugins){
////////////////////////////////////////
fs.stat(originalPath, (err,origStats)=>{
if(err){cb(err); return;} // Original doesn't exist, something's terribly wrong. Dump out.
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);
}
var originalMtime = Math.floor(origStats.mtime.getTime()/1000);
var renditionMtime;
fs.stat(renditionPath, (err,stats)=>{
if(err){
if(renditionsBacking){
renditionsBacking.checkForRendition(renditionBackingConfig, (err, lastModifiedTime)=>{
if(err){
console.log('Error checking for s3 rendition', err);
genRend();
}else{
renditionMtime = lastModifiedTime;
checkRenditionMtime();
}
});
var renditionData;
let process = () => {
fs.stat(renditionPath, (err,stats)=>{
if(err){
if(renditionsBacking){
renditionsBacking.checkForRendition(renditionBackingConfig, (err, lastModifiedTime, data)=>{
if(err){
genRend();
}else{
renditionMtime = lastModifiedTime;
renditionData = data;
checkRenditionMtime();
}
});
}else{
genRend();
}
}else{
genRend();
renditionMtime = stats?Math.floor(stats.mtime.getTime()/1000):null;
checkRenditionMtime();
}
});
};
if (!originalMtime) {
ensureOriginalIsOnDisk(function(err){
if(err){
cb(err);
return;
}
}else{
renditionMtime = stats?Math.floor(stats.mtime.getTime()/1000):null;
checkRenditionMtime();
}
});
originalMtime = Math.floor(fs.statSync(originalPath).mtime.getTime()/1000);
process();
});
} else {
process();
}
let checkRenditionMtime=()=>{
if(!renditionMtime || renditionMtime<originalMtime){
genRend();
}else{
cb();
cb(null, renditionData);
}
};
let genRend=()=>{
generateRendition(originalPath, originalMtime);
ensureOriginalIsOnDisk(function(err){
if(err){
cb(err);
return;
}
generateRendition(originalPath, originalMtime);
});
}
});
......@@ -410,7 +443,7 @@ module.exports=function(options, plugins){
if(err){log.error("Can't stat: " , lastModifiedPath, err);}
fs.readFile(lastModifiedPath, function (err, data) {
log.info("Re-checking original, because more than " + options.revalidateAge + " seconds have passed since last check.");
downloadOriginal(cb, err?null:{"if-modified-since": data});
downloadOriginal(cb, err?null:{"if-modified-since": data.toString()});
});
}else{
cb();
......@@ -445,11 +478,11 @@ module.exports=function(options, plugins){
var slingStream;
if(!failoverMode){
log.info("Reading sling image: " + sc.baseUri + origRendition);
log.trace("Reading sling image: " + sc.baseUri + origRendition);
slingStream= sc.getStream(origRendition, "GET", headers);
}else{
usedOriginalFailover = true;
log.info("Reading sling image from failover: " + options.publicBaseUri + origRendition);
log.warning("Reading sling image from failover: " + options.publicBaseUri + origRendition);
slingStream = request({
method: "GET",
uri: options.publicBaseUri + origRendition,
......@@ -458,6 +491,7 @@ module.exports=function(options, plugins){
followRedirect: false
});
}
var originalTemp = getTempFile();
var statusCode;
......@@ -465,36 +499,35 @@ module.exports=function(options, plugins){
var lastModDate=new Date();
slingStream.on("response", function(slingResponse){
var lastModified=slingResponse.headers['last-modified'];
if(lastModified){
lastModDate=new Date(lastModified);
}
statusCode=slingResponse.statusCode;
var lastModified=slingResponse.headers['last-modified'];
if(lastModified){
lastModDate=new Date(lastModified);
}
statusCode=slingResponse.statusCode;
if(slingResponse.statusCode===200){
mkdirp(imageRoot, function (err) {
if (err){cb(err); return;}
if(slingResponse.statusCode===200){
mkdirp(imageRoot, function (err) {
if (err){cb(err); return;}
fs.writeFile(lastModifiedPath, lastModified, function(){
touchLastModifiedRecord();
}); // Keep record of sling's "Last-Modified" declaration, to validate later requests
});
}else
if(slingResponse.statusCode===304){
touchLastModifiedRecord();
cb();
}else{
if(options.publicBaseUri && !failoverMode){
log.warning("Failed to get image from initial sling-source, got: ", slingResponse.statusCode + " on: " + sc.baseUri + origRendition);
downloadOriginal(cb, headers, true); // attempt download again, this time from failover source
}else{
log.error("Failed to get image: ", sc.baseUri + origRendition);
if(emailError && isWorthEmailing()){emailError({subject: "Blacklight img-opt: Sling download error", text: "Status " + slingResponse.statusCode + " on: " + sc.baseUri + origRendition, req: req});}
res.status( (slingResponse.statusCode===404)? 404 : 500).send("Error " + slingResponse.statusCode + ": Problem getting image");
}
}
fs.writeFile(lastModifiedPath, lastModified, function(){
touchLastModifiedRecord();
}); // Keep record of sling's "Last-Modified" declaration, to validate later requests
});
}else if(slingResponse.statusCode===304){
touchLastModifiedRecord();
cb();
}else{
if(options.publicBaseUri && !failoverMode){
log.warning("Failed to get image from initial sling-source, got: ", slingResponse.statusCode + " on: " + sc.baseUri + origRendition);
downloadOriginal(cb, headers, true); // attempt download again, this time from failover source
}else{
log.error("Failed to get image: ", sc.baseUri + origRendition);
if(emailError && isWorthEmailing()){emailError({subject: "Blacklight img-opt: Sling download error", text: "Status " + slingResponse.statusCode + " on: " + sc.baseUri + origRendition, req: req});}
res.status( (slingResponse.statusCode===404)? 404 : 500).send("Error " + slingResponse.statusCode + ": Problem getting image");
}
}
});
slingStream.on("error",function(err){
if(headers && headers["if-modified-since"]){
......@@ -534,14 +567,6 @@ module.exports=function(options, plugins){
};
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
......
{
"name": "blacklight-core",
"version": "0.8.1",
"version": "0.8.2",
"description": "Blacklight CMS, core page rendering and service module",
"main": "index.js",
"scripts": {
......