Created
July 19, 2014 08:12
-
-
Save loomsen/82b2ff78e1bb2902911d to your computer and use it in GitHub Desktop.
This file contains hidden or 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'); | |
require('systemd'); | |
// increase to increase verbosity | |
var debug = 1; | |
/* 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 muell = site.match(/kein-link/i); | |
if (muell !== null) { sendErr('400'); }; | |
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(); | |
} | |
var conn = new ssh2(); | |
conn.connect({ | |
"host": "imageserver", | |
"port": 22, | |
"username": "user", | |
"privateKey": fs.readFileSync("/web/.ssh/id_rsa") | |
}); | |
conn.on('error',function (err) { | |
if (debug >=1) { console.log( "- ssh connection error for %s", site ); } | |
sendErr('500'); | |
}); | |
conn.on('close', function() { | |
if (debug >= 2) { console.log( " - ssh connection closed for %s", site); } | |
conn.end(); | |
}); | |
conn.on('end', function () { | |
if (debug >= 2) { console.log( " - ssh connection ended for %s", site); } | |
}); | |
conn.on('ready',function () { | |
if (debug >= 2) { console.log( "- ssh connection ready for %s", site ); } | |
}); | |
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(" - +++FAILED+++ shot for: %s to file %s ", site, id); }; | |
console.log(' - ' + e.message + 'for site ' + 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/images/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