-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
218 lines (197 loc) · 5.81 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
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
var fs = require('fs');
if(fs.existsSync('./newrelic.js'))
require('newrelic');
var express = require('express');
var app = express();
var moment = require('moment');
var im = require('imagemagick');
var cons = require('consolidate');
var mongo = new (require("mongolian"))({log:{debug:function(){}}});
var tc = mongo.db("leo");
var db = {};
db.images = tc.collection("images");
var includeIP = function(req, res, next){
req.addr = req.header('cf-connecting-ip');
if(!req.addr){
req.addr = req.header('x-forwarded-for');
if(req.addr){
var temp = req.addr.split(',');
if(temp.length > 1)
req.addr = temp[temp.length - 1];
}
}if(!req.addr)
req.addr = req.socket.remoteAddress;
next();
}
var allowCrossDomain = function(req, res, next){
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST,GET,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
app.configure(function(){
app.use(express.compress());
app.use(allowCrossDomain);
app.use(includeIP);
app.use(express.static(__dirname + '/static'));
app.engine("ejs", cons.ejs);
});
// If you're not using a reverse-proxy, change this to app.listen(80);
app.listen(8009, '127.0.0.1');
var logStream = fs.createWriteStream('log.txt', {'flags': 'w'});
function log(message){
console.log(getTimestamp() + " " + message);
logStream.write(getLongTimestamp() + " " + message + '\n');
}function getTimestamp(){
return moment().format("H:mm");
}function getLongTimestamp(){
return moment().format("MM/DD H:mm");
}
app.get('/', function(req, res){
var host = req.headers.host;
var index = req.headers.host.indexOf("www.");
if(index != -1)
host = host.substr(index + 4);
var site = host.split(".")[0];
var vars = {
host: host,
site: site
};
res.render(__dirname + '/index.ejs', {$: vars, open: "<?", close: "?>"});
});
function addImage(old, imgs, i, res){
var image = imgs[i];
if(old.indexOf(image) === -1){
im.identify("./img/" + image, function(err, data){
if(err || !data)
return log("ERROR: " + err);
db.images.insert({
src: image,
width: data.width,
height: data.height
});
log("Added " + image);
i++;
if(i < imgs.length)
addImage(old, imgs, i, res);
else
res.send("Done!");
});
}else{
i++;
if(i < imgs.length)
addImage(old, imgs, i, res);
else
res.send("Done!");
}
}
app.get('/update', function(req, res){
log("UPDATE\t" + req.addr);
var old = [];
db.images.find().forEach(function(img){
old.push(img.src);
});
// TODO: account for images being removed from the directory
fs.readdir("img", function(err, imgs){
if(!err && imgs){
addImage(old, imgs, 0, res);
}else
res.send({error: err});
});
});
function getCandidates(width, height, callback){
// Lazy version: consider all images equal
// TODO: choose images based on optimal resolution
db.images.find().toArray(callback);
}
function chooseCandidate(width, height, imgs, callback){
/*
* Currently, we're just using a random image from the list of candidates.
* In the future, some sort of hashing algorithm could be used here such
* that requesting an image with certain dimensions would always select the
* same candidate. However, since results are cached, this shouldn't be
* noticeable. Function is left asynchronous in case a future hashing method
* requires it.
*/
callback(null, imgs[Math.floor(Math.random() * imgs.length)]);
}
function serveFromCache(filename, req, res){
// Future cache stats and cleanup could happen here
log("REQUEST\t" + filename + "\t" + req.addr);
res.sendfile("./cache/" + filename + ".jpg");
}
app.get('/:width/:height', function(req, res){
var width = parseInt(req.params.width);
var height = parseInt(req.params.height);
if(width > 9000 || height > 9000)
return res.send("Max size is 9000x9000");
var size = width + "x" + height;
fs.exists("cache/" + size + ".jpg", function(exists){
if(exists)
return serveFromCache(size, req, res);
log("CREATE\t" + size + "\t" + req.addr);
getCandidates(width, height, function(err, imgs){
if(!err && imgs){
chooseCandidate(width, height, imgs, function(err, img){
if(!err && imgs){
im.convert(["img/" + img.src,
"-resize", size + "^",
"-gravity", "center",
"-extent", size,
"cache/" + size + ".jpg"],
function(err, stdout, stderr){
fs.exists("cache/" + size + ".jpg", function(exists){
if(!exists){
log(JSON.stringify([err, stdout, stderr]));
res.send("Error.");
}else
serveFromCache(size, req, res);
});
});
}else
res.send(err);
});
}else
res.send(err);
});
});
});
app.get('/g/:width/:height', function(req, res){
var width = parseInt(req.params.width);
var height = parseInt(req.params.height);
if(width > 9000 || height > 9000)
return res.send("Max size is 9000x9000");
var size = width + "x" + height;
fs.exists("cache/g" + size + ".jpg", function(exists){
if(exists)
return serveFromCache("g" + size, req, res);
log("CREATE\tg" + size + "\t" + req.addr);
getCandidates(width, height, function(err, imgs){
if(!err && imgs){
chooseCandidate(width, height, imgs, function(err, img){
if(!err && imgs){
// TODO: Check the cache for a color version of the same resolution
im.convert(["img/" + img.src,
"-resize", size + "^",
"-gravity", "center",
"-extent", size,
"-colorspace", "Gray",
"cache/g" + size + ".jpg"],
function(err, stdout, stderr){
fs.exists("cache/g" + size + ".jpg", function(exists){
if(!exists){
log(JSON.stringify([err, stdout, stderr]));
res.send("Error.");
}else
serveFromCache("g" + size, req, res);
});
});
}else
res.send(err);
});
}else
res.send(err);
});
});
});
log("Running.");