-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
179 lines (163 loc) · 4.79 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
/**
* Module dependencies.
*/
var express = require('express')
, fs = require('fs')
, Img = require('./lib/image.js')
, gm = require('gm')
, morgan = require('morgan')
, Montage = require('./lib/montage')
, util = require('util')
, async = require('async')
;
/**
* Options.
*/
var img_path = __dirname + '/img/';
if (!fs.existsSync(img_path)) {
fs.mkdirSync(img_path);
}
var resize_message = 'Specify an url \'u\', width \'w\' and height \'h\'';
var crop_message = 'Specify an url \'u\', width \'w\', height \'h\', x-offset \'x\' and y-offset \'y\'';
/**
* Express server.
*/
var app = express()
.use(morgan('dev'))
.set('static_server', process.env.NODE_STATIC_SERVER || "https://zooniversestatic.z13.web.core.windows.net/")
.set('port', process.env.NODE_PORT || 80)
.set('env', process.env.NODE_ENV || "development")
.set('debug', process.env.NODE_DEBUG || false);
// server to find the images on
var static_host = app.settings.static_server;
// function cropImage(u, w, h, x, y, res, next){
function cropImage(paramSource, res, next){
var u = paramSource.u
, w = paramSource.w
, h = paramSource.h
, x = paramSource.x
, y = paramSource.y
;
if (!u || !w || !h || !x || !y) res.send(400, {error: crop_message});
var url = computeURL(static_host, u);
var img = new Img(url,img_path).load(function(err,im) {
if (err) res.status(500).send({ error: err })
else {
if (app.get("debug")) {
console.log("\nCrop image:");
console.log(im);
}
var outFile = im.cropHashFilename(w,h,x,y);
fs.stat(outFile, function(err, stat) {
if (!err) {
res.sendFile(outFile);
}else {
gm(im.destHashed)
.crop(w, h, x, y)
.write(outFile, function (err) {
if (err) {
next(new Error(err));
} else {
res.sendFile(outFile);
};
});
}
});
}
});
};
function escapeRegExp(string){
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
};
function validUrl(arr, url) {
return arr.some(function(arrVal) {
var host_regex = new RegExp(escapeRegExp(arrVal), 'g');
return url.match(host_regex);
});
}
function computeURL(static_host, u){
var whitelists = [
's3.amazonaws.com/astronomy-rewind',
'panoptes-uploads.zooniverse.org'
];
if (validUrl(whitelists, u)) {
return 'https://' + u;
} else {
return static_host + u;
}
};
/**
* Express routes.
*/
app.get('/resize', function(req, res, next){
var u = req.query.u // URL
, w = req.query.w // width
, h = req.query.h // height
, o = req.query.o // opts, support http://www.graphicsmagick.org/GraphicsMagick.html#details-resize
;
if (!u || !w || !h) res.send(400, {error: resize_message});
var img = new Img(computeURL(static_host, u),img_path).load(function(err,im) {
if (err) res.status(500).send({ error: err })
else {
gm(im.destHashed)
.resize(w, h, o)
.stream(function streamOut (err, stdout, stderr) {
if (err) res.status(500).send({ error: err })
stdout.pipe(res); //pipe to response
stdout.on('error', next);
});
}
});
});
app.get('/resize_crop', function(req, res, next){
var u = req.query.u // URL
, w = req.query.w // width
, h = req.query.h // height
;
if (!u || !w || !h) res.send(400, {error: resize_message});
var img = new Img(computeURL(static_host, u),img_path).load(function(err,im) {
if (err) res.status(500).send({ error: err })
else {
im.resize(w,h,function(err,im) {
if (err) res.status(500).send({ error: err })
else {
res.writeHead(200, {"Content-Type": "image/" + im.extension});
res.end(im.resized, "binary");
}
})
}
});
});
app.get('/crop/:w/:h/:x/:y/:u', function(req, res, next){
cropImage(req.params, res, next);
});
app.get('/crop', function(req, res, next){
cropImage(req.query, res, next);
});
/**
* Montage route. Takes multiple images and combines them into a pleasing grid/tile layout
*/
app.get('/montage', function(req, res, next){
// Get url(s)
var u = req.query.u;
if (!Array.isArray(u)) {
return next(new Error('Please specify at least two images'));
}
// Load all source images
async.map(req.query.u, function (u, done) {
new Img(computeURL(static_host, u),img_path).load(done);
}, function (err, images) {
if (err) next(new Error(err));
var montage = new Montage(images, req.query.w, req.query.mw);
montage.construct(function (err, outfile) {
if (err) next(new Error(err));
res.sendFile(outfile);
});
});
});
/**
* Start server.
*/
app.listen(app.get("port"), function() {
console.log("Express server listening on port %d in %s mode", app.settings.port || 3000, app.settings.env);
});