-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
102 lines (89 loc) · 2.92 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
var phantom = require('phantom');
var express = require('express');
var app = require('express').createServer();
var random = require('./lib/random').randomString;
var im = require('imagemagick');
function dumpError(err) {
if (typeof err === 'object') {
if (err.message) {
console.log('\nMessage: ' + err.message)
}
if (err.stack) {
console.log('\nStacktrace:')
console.log('====================')
console.log(err.stack);
}
} else {
console.log('dumpError :: argument is not an object');
}
}
app.configure(function(){
app.use(express.methodOverride());
});
app.configure('development', function(){
app.use(express.static(__dirname + '/public'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
var oneYear = 31557600000;
app.use(express.static(__dirname + '/public', { maxAge: oneYear }));
app.use(express.errorHandler());
});
app.get('/api/1.0/export/:format', function(req, res, next) {
if(req.query && req.query.page) {
phantom.create(function(ph) {
ph.createPage(function(page) {
page.open(req.query.page, function(status) {
if(status !== 'success') {
console.log("Error, page not found.");
res.send({success: false, message: 'Page not found'});
} else {
var fileName = random(64) + '.' + req.params.format;
var output = __dirname + "/public/images/" + fileName;
console.log(output);
page.render(output);
try {
if(req.query.size) {
var split = req.query.size.split("x");
if(split.length > 1) {
var size = {};
size.width = parseInt(split[0]);
size.height = parseInt(split[1]);
var Fs = require('fs');
console.log(Fs.lstatSync(output));
im.resize({
srcPath: output,
dstPath: output,
width: size.width,
height: size.height
}, function(err,stdout,stderr){
if(err)
console.log(err);
else
console.log(size);
});
}
}
} catch(err) {
console.log("Image magic failed resizing image: "+fileName);
} finally {
ph.exit();
}
try {
res.send({
success: true,
path: 'http://' + req.headers.host + '/images/'+fileName
});
} catch(err) {
dumpError(err);
res.send({success: false, message: "Unknown error"});
}
}
});
});
});
} else {
res.send({success: false, message: 'Page param missing'});
}
});
app.listen(8888);