-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
119 lines (99 loc) · 3.2 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var banquo = require('banquo');
var config = require('./config.json')
var AWS,
s3;
var app = express();
var error_msgs = {
"opts": "Please check the syntax and spelling of the variables you are passing in the url hash: ",
"domain": "You are attempting to access this server from an unauthorized domain. To install this service on your own server, see it on Github: http://github.com/ajam/banquo-server"
}
var opts_whitelist = ["mode", "url", "viewport_width", "delay", "selector", "css_hide", "css_file", "out_file"];
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
function errorResponse(res, type, more_info){
more_info = (more_info) ? more_info : ''
res.jsonp(500, { error: error_msgs[type] + more_info })
return false;
}
function assembleSettings(url, opts){
var settings = {};
settings.url = url;
if (opts){
opts = opts.split('&');
for(var i = 0; i < opts.length; i++){
var opt_arr = opts[i].split('=');
if (opts_whitelist.indexOf(opt_arr[0]) != -1){
settings[opt_arr[0]] = opt_arr[1];
}else{
return {status: false, error: opt_arr[0]};
}
}
}
return {status: true, settings: settings};
}
function uploadToS3(image_data, timestamp){
AWS = require('aws-sdk');
AWS.config.loadFromPath(config.credentials);
s3 = new AWS.S3();
var key_info = config.output_path + config.file_name + timestamp + '.png';
var img_blog = new Buffer(image_data, 'base64')
var data = {
Bucket: config.bucket,
Key: key_info,
Body: img_blog,
ACL: 'public-read',
ContentType: 'image/png',
ContentLength: img_blog.length
};
s3.client.putObject( data , function (resp) {
if (resp == null){
console.log('Successful upload: ' + key_info);
}else{
console.log('ERROR IN ' + timestamp);
};
});
}
app.enable("jsonp callback");
app.get("/:url/:opts?*", function(req, res) {
if (config.disable_whitelist || config.referer_whitelist.indexOf(String(req.headers.referer)) != -1){
var result = assembleSettings(req.params.url, req.params.opts);
if (result.status){
banquo.capture(result.settings, function(image_data){
var timestamp = new Date().getTime();
res.jsonp(200, {image_data: image_data, timestamp: timestamp});
if (config.upload_to_s3){
uploadToS3(image_data, timestamp);
}
});
}else{
errorResponse(res, 'opts', result.error);
}
}else{
errorResponse(res, 'incorrect referer, please look at your whitelist. Incoming refer is', String(req.headers.referer) );
}
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});