-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
127 lines (107 loc) · 3.25 KB
/
server.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
120
121
122
123
124
125
126
127
'use strict';
var express = require('express'),
phantom = require('phantom'),
MD5 = require('MD5'),
fs = require('fs'),
rimraf = require('rimraf');
var listenPort = 9242;
console.log("Starting Phantom JS process...");
phantom.create(function (ph) {
console.log("Phantom JS started.");
console.log("Cleaning up screenshot directory.");
rimraf('screenshots', function (error) {
if (error) {
console.log("Error cleaning up screenshots");
} else {
console.log("Screenshot directory is all clean.");
}
});
var app = express();
app.get('/getimage', function (req, res) {
var query = req.query,
url = query.url || "",
screenSize = query.screenSize || "1600x1200",
scaledToHeight = query.scaledToHeight || -1,
scaledToWidth = query.scaledToWidth || -1;
// maxFileSize = query.maxFileSize || 700000,
// format = query.format || "png";
// Naive validation
if (typeof url !== 'string' || url.substr(0, 4) !== 'http' || url.indexOf('://') < 0) {
invalidParams();
console.log("Invalid URL: " + url);
return;
}
var screenDims = screenSize.split('x'),
width = Math.min(1600, screenDims[0]),
height = Math.min(1200, screenDims[1]);
// Handle scaling
var zoomFactor = 1;
if (scaledToHeight > 0) {
zoomFactor = scaledToHeight / height;
width = width * zoomFactor;
height = scaledToHeight;
} else if (scaledToWidth > 0) {
zoomFactor = scaledToWidth / width;
width = scaledToWidth;
height = height * zoomFactor;
}
var filePath = 'screenshots/' + screenSize + '/' + width + 'x' + height + '/' + MD5(url) + '.png';
if (fs.existsSync(filePath)) {
console.log("Found image in file system already: " + filePath);
res.sendfile(filePath);
return;
}
ph.createPage(function (page) {
page.set('zoomFactor', zoomFactor);
page.set('viewportSize', { width: width, height: height });
page.set('clipRect', { top: 0, left: 0, width: width, height: height });
page.open(url, function(status) {
page.set('navigationLocked', true);
if (status !== 'success') {
res.send(500, 'Remote request failed');
console.log("Failed to load URL: " + url);
closePage();
} else {
setTimeout(function () {
if (fs.existsSync(filePath)) {
console.log("Found image in file system already: " + filePath);
imageReady(filePath);
} else {
page.render(filePath, { format: 'png', quality: '100' }, imageReady);
}
function imageReady() {
var cleanupFinished = false;
res.on("end", cleanup);
res.on("error", cleanup);
setTimeout(cleanup, 60000);
res.sendfile(filePath);
closePage();
function cleanup() {
if (!cleanupFinished) {
fs.unlink(filePath, function (exc) {
if (exc) {
console.log("Failed to remove file: " + filePath);
} else {
console.log("Removed file: " + filePath);
}
});
cleanupFinished = true;
}
}
}
}, 0);
}
});
function closePage() {
setTimeout(function () {
page.close();
}, 0);
}
});
function invalidParams() {
res.send(400, 'Invalid request parameters');
}
});
app.listen(listenPort);
console.log("w2i2 is now active and listening on port " + listenPort);
});