Last active
October 30, 2018 13:17
-
-
Save loomsen/576bd1b291b1ea361c57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var url = require('url'); | |
var spawn = require('child_process').spawn, child; | |
var fs = require('fs'); | |
var util = require('util'); | |
var gm = require('gm'); | |
var ssh2 = require('ssh2'); | |
var debug = 1; // increase to increase verbosity | |
require('systemd'); | |
var conn = new ssh2(); | |
options = { | |
"host": "imageserver", | |
"port": 22, | |
"username": "username", | |
"privateKey": fs.readFileSync("/home/username/.ssh/id_rsa") | |
}; | |
conn.connect(options); | |
conn.on('error',function (err) { | |
if (debug >=1) { console.log( " - ssh connection error" ); }; | |
}); | |
conn.on('close', function() { | |
if (debug >= 2) { console.log( " - ssh connection closed, reconnecting in 2 seconds"); }; | |
setTimeout(function() { conn.connect(options); }, 2000); | |
}); | |
conn.on('end', function () { | |
if (debug >= 2) { console.log( " - ssh connection ended"); }; | |
// setTimeout(function() { conn.connect(options); }, 2000); | |
}); | |
conn.on('ready',function () { | |
if (debug >= 2) { console.log( "- ssh connection ready for %s"); } | |
}); | |
/* create a server to receive callbacks from the screenshot service*/ | |
http.createServer(function(req, res) { | |
var urldata = url.parse(req.url, true).query; | |
var site = urldata.url; | |
var id = urldata.id; | |
var render = spawn('./node_modules/phantomjs/bin/phantomjs',['screenshot.js', site, id + '.png' ]); | |
function sendErr(code) { | |
res.writeHead(code, { 'Connection': 'close' }); | |
res.end(); | |
// conn.end(); | |
} | |
function sendOk() { | |
res.writeHead(200); | |
res.end(); | |
// conn.end(); | |
} | |
render.stdout.on("data", function(data) { | |
if (debug >= 2) { console.log( " - taking shot of: ==> %s <== to file: %s", site, id ); } | |
}); | |
// once our rederer exits, the shot was taken | |
render.on("exit", function(code) { | |
// make sure we get the full file, so read sync | |
// if ENOENT, we can assume phantom failed and return 500 | |
try { | |
var src = fs.readFileSync('./'+id+'.png'); | |
} catch(e) { | |
if (e.code === 'ENOENT') { | |
if (debug >= 2) { console.log(" - taking shot for: %s to file %s +++FAILED!+++", site, id); }; | |
console.log(' - %s: for site %s', e.message, site); | |
} else { throw e; }; | |
sendErr('500'); | |
return; | |
}; | |
gm(src) | |
.flatten() | |
.background('#ffffff') | |
.resize(290, 220, "^") | |
.extent(290, 220) | |
.gravity('north') | |
.quality(100) | |
.sharpen(0, 0.8) | |
.write(id + '.jpg', function (err) { | |
if (!err) { | |
if (debug >= 2) { console.log("file: %s written for site: %s", id, site); }; | |
conn.sftp(function (err, sftp) { | |
if (err) { | |
if (debug >= 1) { console.log( "Error, problem starting SFTP for %s", site ); } | |
sendErr('500'); | |
} | |
if (debug >= 2) { console.log( "- SFTP started for %s", site ); } | |
// setup the two streams | |
var readStream = fs.createReadStream( id + ".jpg" ); //local | |
var writeStream = sftp.createWriteStream( "/web/screenshots/" + id + ".jpg" ); //remote | |
writeStream.on('close', function () { // once the write stream closes, everything ran successfully, return 200 | |
if (debug >= 1 ) { console.log( "- file transferred %s for site: %s", id, site ); } | |
sftp.end(); | |
sendOk(); | |
// clean up our mess | |
fs.unlink(id + '.png', function(err){ if (err && debug >= 1 ) { console.log("could not remove "+id+".png after transfer" ); }; }); | |
fs.unlink(id + '.jpg', function(err){ if (err && debug >= 1 ) { console.log("could not remove "+id+".jpg after transfer" ); }; }); | |
}); | |
readStream.pipe( writeStream ); // connect the two streams | |
}); | |
} else { | |
if (debug >= 1) { console.log( "something went wrong upon gm .write operation on %s for site %s", id, site ); }; | |
sendErr('500'); | |
}; | |
}); | |
}); | |
render.stderr.on("data", function(data) { | |
if (debug >= 1) { console.log("error executing phantom.js: ", data); }; | |
sendErr('500'); | |
}); | |
}).listen('systemd'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment