Created
July 18, 2014 18:25
-
-
Save loomsen/f627c3f26b713f23493f 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'); | |
var debug = 1; // increase to increase verbosity | |
require('systemd'); | |
/* 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; | |
// ignore missing link entries | |
var muell = site.match(/kein-link/i); | |
if (muell !== null) { res.writeHead(400, { 'Connection': 'close' }); res.end(); } | |
var render = spawn('./node_modules/phantomjs/bin/phantomjs',['screenshot.js', site, id + '.png' ]); | |
var conn = new ssh2(); | |
conn.connect({ | |
"host": "imageserver", | |
"port": 22, | |
"username": "webshot", | |
"privateKey": fs.readFileSync("/web/.ssh/id_rsa") | |
}); | |
conn.on('error',function (err) { | |
if (debug >=1) { console.log( "- ssh connection error for " ); } | |
res.writeHead(500, { 'Connection': 'close' }); | |
res.end(); | |
conn.end(); // always end the connection | |
}); | |
conn.on('close', function() { | |
if (debug >= 2) { console.log( " - ssh connection closed "); } | |
conn.end(); | |
}); | |
conn.on('end', function () { | |
if (debug >= 2) { console.log( " - ssh connection ended "); } | |
conn.end(); // always end the connection | |
}); | |
conn.on('ready',function () { | |
if (debug >= 2) { console.log( "- ssh connection ready" ); } | |
}); | |
render.stdout.on("data", function(data) { | |
gm( id + '.png') | |
.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 " ); } | |
res.writeHead(500, { 'Connection': 'close' }); | |
res.end(); | |
conn.end(); | |
} | |
if (debug >= 2) { console.log( "- SFTP started" ); } | |
// setup the two streams | |
var readStream = fs.createReadStream( id + ".jpg" ); //local | |
var writeStream = sftp.createWriteStream( "/web/images.city-map.de/www/screenshots/" + id + ".jpg" ); //remote | |
writeStream.on('close', function () { // once the write stream closes, everything | |
// ran successfully, return 200 | |
// and remove the temp files | |
if (debug >= 1 ) { console.log( "- file transferred %s for site: %s", id, site ); } | |
res.writeHead(200); | |
res.end(); // end http connection | |
sftp.end(); // end sftp | |
conn.end(); // always end the connection | |
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 ); }; | |
res.writeHead(500, { 'Connection': 'close' }); // whoopsie, something went wrong | |
res.end(); | |
conn.end(); | |
}; | |
}); | |
}); | |
render.stderr.on("data", function(data) { | |
if (debug >= 1) { console.log("error executing phantom.js: ", data); }; | |
res.writeHead(500, { 'Connection': 'close' }); | |
res.end(); | |
conn.end(); | |
}); | |
render.on("exit", function(code) { | |
if (debug >= 2) { console.log("phantom exited"); }; | |
}); | |
}).listen('systemd'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment