-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
executable file
·381 lines (313 loc) · 11 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env node
/*** MODULES ******************************************************************/
var fs = require('fs');
var http = require('http');
var url = require('url');
var util = require('util');
var curry = require('curry');
var express = require('express');
var flags = require('flags');
var gm = require('gm');
var jade = require('jade');
var winston = require('winston');
var enrage = require(__dirname + '/lib/enrage.js');
/*** RUNTIME #DEFINES *********************************************************/
var I_DONE_GOOFED = 'I DONE GOOFED (ERROR 500)';
var NO_FACES_MSG = 'No faces found'
var NO_FACES_SYM = 'nofaces';
var LOAD_JPG_SYM = 'hit';
// If the contents of a cache file are longer than this, we assume it's a valid
// JPEG image; otherwise, we assume it's a special symbol denoting that, e.g.,
// there are no faces in the image, or Face.com couldn't get a valid image. If
// any of these symbols exceed this length ceiling, bad things will happen.
var SYM_LENGTH_CEIL = 100;
var QUEEN_ELIZABETH = 'http://www.librarising.com/astrology/celebs/images2' +
'/QR/queenelizabethii.jpg';
// This is safe to use when pairing integers and face.com API error messages,
// since neither of these things will contain a pipe.
var FACE_COM_SAFE_DELIMITER = '|';
var FACE_COM_ERR_CACHE_TTL = 600;
var RAGE_ERR_REGEX = /(^\d{2,3})\|([^\|]*)\|(\d+)$/;
var IMAGES_DIR = 'resources/images';
// these are all in seconds
var ONE_DAY = 24 * 60 * 60;
var ONE_WEEK = ONE_DAY * 7;
var ONE_YEAR = ONE_DAY * 365;
// when there's a change in how images are drawn, this field should be updated.
// used in image caching headers
var IMAGE_DRAWING_LAST_CHANGE = enrage.lastModificationDate;
/*** GLOBALS ******************************************************************/
// express.js app object
var app = express.createServer();
// jade function
var renderIndex;
// track in-flight cache misses so other requests for the same image can hook
// into it, minimizing work for each cache miss.
var inFlight = {};
/*** UTILITY BELT *************************************************************/
var cryMeARiver = function (err) {
winston.error(err);
winston.error(err.stack);
}
var inTheFuture = function (interval) {
var now = new Date();
var then = new Date(now.getTime() + interval * 1000);
return then.toGMTString();
}
var sendImg = function (req, res, type, imageData) {
if (req && req.headers['if-modified-since']) {
var lastSaw = new Date(req.headers['if-modified-since']);
if (lastSaw >= IMAGE_DRAWING_LAST_CHANGE || lastSaw > new Date()) {
res.writeHead(304 , { 'Content-Type': 'image/' + type
, 'Expires': inTheFuture(ONE_DAY)
});
res.end();
return;
}
}
res.writeHead(200, { 'Cache-Control': 'max-age=' + ONE_DAY + ', public'
, 'Content-Type': 'image/' + type
, 'Content-Length': imageData.length
, 'Expires': inTheFuture(ONE_DAY)
, 'Last-Modified': IMAGE_DRAWING_LAST_CHANGE.toGMTString()
});
res.write(imageData);
res.end();
}
var sendHTML = function (res, err, html) {
if (err) {
cryMeARiver(err);
send500(res);
return;
}
// make a buffer so we know the byte length
var htmlData = html ? new Buffer(html, 'utf8') : {length: 0};
res.writeHead(200, { 'Content-Type': 'text/html;charset=utf-8'
, 'Content-Length': htmlData.length
});
if (html) {
res.write(htmlData);
}
res.end();
}
var sendNoFaces = function(res) {
res.writeHead(200, { 'Cache-Control': 'max-age=' + ONE_YEAR
, 'Content-Type': 'text/html;charset=utf-8'
, 'Content-Length': NO_FACES_MSG.length
, 'Expires': inTheFuture(ONE_YEAR)
});
res.write(NO_FACES_MSG);
res.end();
}
var sendErr = function(res, errCode, errMsg) {
res.writeHead(errCode, errMsg, { 'Content-Type': 'text/plain'
, 'Content-Length': errMsg.length
});
res.write(errMsg);
res.end();
}
var send500 = function(res) {
sendErr(res, 500, I_DONE_GOOFED);
}
var deleteIfErr = function (path, err) {
if (err) {
cryMeARiver(err);
fs.unlink(path, reportIfErr);
}
}
var reportIfErr = function (err) {
if (err) cryMeARiver(err);
}
var imagePath = function (src, extension) {
var b64 = (new Buffer(src)).toString('base64');
if (extension === undefined) extension = '.jpg';
return __dirname + '/' + IMAGES_DIR + '/' + b64 + extension;
}
var staticImage = function (path, req, res) {
var wres = {res: res, expects: 'image'};
fs.readFile('resources/' + path, curry([req, wres, null, null], gotFile));
}
/*** SERVER LOGIC *************************************************************/
// This is almost exactly the same as the handler below, except that we don't
// send the image, just an empty 200 response, if all goes well.
// This is used by the client-side js to check for bad requests and whatnot, so
// it can display error messages to the user, without fetching the image all
// over again.
app.get('/status/', function (req, res) {
var request = url.parse(req.url, true);
var src = request.query.src;
if (!src) { sendErr(res, 400, 'No "src" param found'); return }
var fname = imagePath(src);
var wres = {res: res, expects: 'text'};
fs.readFile(fname, curry([req, wres, src, fname], gotFile));
})
app.get('/image/', function (req, res) {
var request = url.parse(req.url, true);
var src = request.query.src;
if (!src) { sendErr(res, 400, 'No "src" param found'); return }
winston.info('enraging URL "' + src + '"');
var fname = imagePath(src);
var wres = {res: res, expects: 'image'};
fs.readFile(fname, curry([req, wres, src, fname], gotFile));
})
app.get('/favicon.png', curry(['favicon.png'], staticImage));
app.get('/forkme.png', curry(['forkme.png'], staticImage));
app.get('/:b64?', function (req, res) {
var request = url.parse(req.url, true);
var src = request.query.src;
if (req.params.b64) {
var imgUrl = (new Buffer(req.params.b64, 'base64')).toString('utf8');
}
var index = renderIndex({image: imgUrl || QUEEN_ELIZABETH });
sendHTML(res, null, index);
return;
})
var gotFile = function (req, wres, src, fname, err, data) {
var res = wres.res;
var expects = wres.expects;
if (err) {
// if it's just a ENOENT error, then the cache file DNE
if (err.code === 'ENOENT') {
// see if there's an in-flight request for this image
if (inFlight[src] === undefined) {
// nope, set the key to signal we've got this
inFlight[src] = [];
enrage.enrageUrl(src, curry([wres, src], gotRage));
} else {
// yup, add the response object to the list of those to be responded to
// upon completion
inFlight[src].push(wres);
}
} else {
send500(res);
if (err.message !== 'Could not load image') {
cryMeARiver(err);
}
}
return;
}
if (data.length <= SYM_LENGTH_CEIL) {
if (data.toString('utf8') === NO_FACES_SYM) {
sendNoFaces(res);
} else if ((rage_err = data.toString('utf8').match(RAGE_ERR_REGEX))) {
var code = rage_err[1] * 1;
var msg = rage_err[2];
var timeout = rage_err[3] * 1;
sendErr(res, code, msg);
if (timeout < ((new Date) * 1)) {
removeCachedFile(fname);
}
} else {
var code_and_message = data.toString('utf8').split(FACE_COM_SAFE_DELIMITER);
sendErr(res, code_and_message[0], code_and_message[1]);
}
return;
}
if (expects === 'image') {
sendImg(req, res, 'jpeg', data);
} else if (true || expects === 'text') {
sendHTML(res, null);
}
}
var gotRage = function(wres, src, err, data, info) {
var res = wres.res;
var expects = wres.expects;
var path = imagePath(src, '');
if (err) {
if (err.statuscode) {
sendErr(res, err.statuscode, err.message);
if (err.statuscode === 404) {
// a 404 statuscode means the input image was screwed up somehow. this
// should be cached indefinitely because it's unlikely to be a transient
// issue
var expiration = 0;
} else {
// 500s are errors that could be transient (for example, if we go over
// our API usage)
var expiration = (new Date) * 1 + FACE_COM_ERR_CACHE_TTL;
if (err.message !== 'Could not load image') {
cryMeARiver(err);
}
}
var RAGE_ERR = err.statuscode + FACE_COM_SAFE_DELIMITER + err.message +
FACE_COM_SAFE_DELIMITER + expiration;
var fname = path + '.jpg';
fs.writeFile(fname, new Buffer(RAGE_ERR), curry([fname], deleteIfErr));
} else {
send500(res);
cryMeARiver(err);
}
return;
}
inFlight[src].push(wres);
var wresps = inFlight[src];
for (var ri in wresps) {
var res = wresps[ri].res;
var expects = wresps[ri].expects;
if (!data) {
sendNoFaces(res);
} else if (expects === 'image') {
sendImg(null, res, 'png', data);
} else if (true || expects === 'text') {
sendHTML(res, null);
}
}
delete inFlight[src];
if (!data) {
var fname = path + '.jpg';
fs.writeFile(fname, new Buffer(NO_FACES_SYM), curry([fname], deleteIfErr));
} else {
fs.writeFile(path + '.png', data, curry([src, path], convertToJPEG));
}
}
var convertToJPEG = function (src, path, err) {
gm(path + '.png').write(path + '.prg.jpg', curry([path], conversionComplete));
}
var conversionComplete = function (path, err) {
if (err) {
removeCachedFile(path + '.jpg');
return;
}
fs.rename(path + '.prg.jpg', path + '.jpg', reportIfErr);
fs.unlink(path + '.png', reportIfErr);
}
var removeCachedFile = function (fname) {
fs.unlink(fname, reportIfErr);
}
/*** MAIN & INITIALIZATION ****************************************************/
var switchToOwner = function (err, stats) {
if (err) {
cryMeARiver(err);
return;
}
process.setgid(stats.gid);
process.setuid(stats.uid);
}
flags.defineString('conf',
__dirname + '/conf/default.json',
'Configuration file');
flags.parse();
var confPath = flags.get('conf');
var confText = fs.readFileSync(confPath, 'utf8');
try {
var conf = JSON.parse(confText);
} catch(err) {
console.error(err);
console.error(err.stack);
process.exit(1);
}
var indexTemplate = fs.readFileSync(__dirname + '/views/index.jade');
renderIndex = jade.compile(indexTemplate, {});
enrage.configure(conf.face_com.api_key, conf.face_com.api_secret);
if (conf.log.path !== "STDOUT") {
winston.add(winston.transports.File, { filename: conf.log.path
, handleExceptions: true
, level: conf.log.level
})
winston.remove(winston.transports.Console);
}
app.listen(conf.server.port, function() {
if (process.getuid() === 0) {
fs.stat(__filename, switchToOwner);
}
})