forked from CartoDB/mobile-tile-packager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
306 lines (288 loc) · 11.4 KB
/
service.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
var fs = require('fs'),
p = require('path');
var archiver = require('archiver');
var conf = require('./config.js');
var hlprs = require('./helpers.js');
var crypto = require('crypto');
var api = require('./api.js');
var express = require('express'),
bodyParser = require('body-parser'),
service = express();
var kue = require('kue');
var queue = kue.createQueue({
prefix: conf.kue_redis_prefix,
redis: {
port: conf.kue_redis_port,
host: conf.kue_redis_host,
//db: 3, // if provided select a non-default redis db
}
});
function getTiles(job_id, username, template, minzoom, maxzoom, bounds, cb) {
function err404(err) {
if (!('toJSON' in Error.prototype)) {
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
var alt = {};
Object.getOwnPropertyNames(this).forEach(function (key) {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true,
writable: true
});
}
var error = new Error(err);
error.time = hlprs.currdatetime();
error.user = username;
error.template = template;
console.error('%s JOB:%s, USER:%s, TEMPLATE:%s, ERROR:%s', hlprs.currdatetime(), job_id, username, template, err);
return cb(error);
}
if (conf.private) {
api.inst_priv(username, template, OnInstantiate);
} else {
api.inst_pub(username, template, OnInstantiate);
}
function OnInstantiate(err, layerId, meta) {
if (err) err404(err);
var mbTilesFile = conf.output + '/' + username + '_' + job_id + '.mbtiles';
var layersFile = conf.output + '/' + username + '_' + job_id+ '.layers.json';
var zipFile = conf.output + '/' + username + '_' + job_id + '.zip';
api.getMbtiles(conf.tile_url.replace('{username}', username).replace('{layergroupid}', layerId),
mbTilesFile, minzoom, maxzoom, bounds, meta, conf.report_log, onGetMbtiles);
function onGetMbtiles(err) {
if (err) err404(err.message);
var archive = archiver('zip');
fs.appendFile(layersFile, JSON.stringify(meta), function AchiveMbtiles(err){
if(err) err404(err.message);
var files = [mbTilesFile, layersFile];
for(var i in files) {
archive.file(files[i], { name: p.basename(files[i]) });
}
archive.on('error', function(err) {
err404(err.message);
});
archive.on('end', function() {
console.log('%s USER:%s, Archive wrote %d bytes.', hlprs.currdatetime(), username, archive.pointer());
fs.unlink(mbTilesFile, function(err){
if(err) err404(err.message);
fs.unlink(layersFile, function(err){
if(err) err404(err.message);
return cb();
});
});
});
archive.pipe(fs.createWriteStream(zipFile));
archive.finalize();
});
}
}
}
function getMbtilesBySQL(job_id, username, template, sql, minzoom, maxzoom, cb) {
function err404(err) {
if (!('toJSON' in Error.prototype)) {
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
var alt = {};
Object.getOwnPropertyNames(this).forEach(function (key) {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true,
writable: true
});
}
var error = new Error(err);
error.time = hlprs.currdatetime();
error.user = username;
error.sql = sql;
console.error('%s JOB:%s, USER:%s, SQL:%s, ERROR:%s', hlprs.currdatetime(), job_id, username, sql, err);
return cb(error);
}
if (conf.private) {
api.inst_priv(username, template, OnInstantiate);
} else {
api.inst_pub(username, template, OnInstantiate);
}
function OnInstantiate(err, layerId, meta) {
if (err) err404(err);
var geoJSONFile = conf.output + '/' + username + '_' + job_id + '.geojson';
var mbtilesFile = conf.output + '/' + username + '_' + job_id + '.mbtiles';
var layersFile = conf.output + '/' + username + '_' + job_id+ '.layers.json';
var zipFile = conf.output + '/' + username + '_' + job_id + '.zip';
var request = api.getGeoJSON(conf.sql_api_url.replace('{username}', username).replace('{sql}', sql)
.replace('{minzoom}', minzoom).replace('{maxzoom}', maxzoom)).pipe(fs.createWriteStream(geoJSONFile))
request.on('error', function(err) {
err404(err)
});
request.on('close', MakeMbtilesWithTC);
function MakeMbtilesWithTC() {
api.ExecTippecanoe(mbtilesFile, geoJSONFile, minzoom, maxzoom, OnRunningTC);
function OnRunningTC(err, exit_code){
if (err) err404(err);
var archive = archiver('zip');
fs.appendFile(layersFile, JSON.stringify(meta), AchiveMbtiles);
function AchiveMbtiles(err){
if(err) err404(err.message);
var files = [mbtilesFile, layersFile];
for(var i in files) {
archive.file(files[i], { name: p.basename(files[i]) });
}
archive.on('error', function(err) {
err404(err.message);
});
archive.on('end', function() {
console.log('%s USER:%s, Archive wrote %d bytes.', hlprs.currdatetime(), username, archive.pointer());
fs.unlink(mbtilesFile, function(err){
if(err) err404(err.message);
fs.unlink(layersFile, function(err){
if(err) err404(err.message);
fs.unlink(geoJSONFile, function(err){
if(err) err404(err.message);
return cb();
});
});
});
});
archive.pipe(fs.createWriteStream(zipFile));
archive.finalize();
}
}
}
}
}
service.use(bodyParser.json());
service.post('/api/v1/package_exports', function(req, res){
var err404 = function(err) {
console.error('%s ERROR:%s', hlprs.currdatetime(), err);
res.status(404).setHeader('Content-Type', 'application/json');
res.json({'errors': [{'message': err.toString()}]});
}
var contype = req.headers['content-type'];
if (!contype || contype.indexOf('application/json') !== 0) {
err404('Content-type is not json');
}
else if (!req.body.data.username) { err404('data.username missing') }
else if (!req.body.data.template) { err404('data.template missing') }
else if (!req.body.data.maxzoom || req.body.data.maxzoom < 0 || req.body.data.maxzoom > 22) { err404('data.maxzoom value must between 1 and 22') }
else if (!req.body.data.bounds) { err404('data.bounds: "lon, lat, lon, lat"') }
else {
var params = {
username: req.body.data.username,
template: req.body.data.template,
minzoom: req.body.data.minzoom ? req.body.data.minzoom : 0,
maxzoom: req.body.data.maxzoom,
bounds: req.body.data.bounds
};
console.log( 'creating job...');
job = queue.create('tiles', params).save(function(err){
if(err)
console.error(err.message);
else {
res.status(200).setHeader('Content-Type', 'application/json');
res.json({'id': job.id, 'created_at': new Date(parseInt(job.created_at))});
}
});
queue.process('tiles', function(job, done){
console.log('processing job: #%d', job.id);
getTiles(job.id, job.data.username, job.data.template, job.data.minzoom, job.data.maxzoom, job.data.bounds, done);
});
}
});
service.post('/api/v1/package_exports_by_sql', function(req, res){
var err404 = function(err) {
console.error('%s ERROR:%s', hlprs.currdatetime(), err);
res.status(404).setHeader('Content-Type', 'application/json');
res.json({'errors': [{'message': err.toString()}]});
}
var contype = req.headers['content-type'];
if (!contype || contype.indexOf('application/json') !== 0) {
err404('Content-type is not json');
}
else if (!req.body.data.username) { err404('data.username missing') }
else if (!req.body.data.template) { err404('data.template missing') }
else if (!req.body.data.sql) { err404('data.sql missing') }
else if (!req.body.data.maxzoom || req.body.data.maxzoom < 0 || req.body.data.maxzoom > 22) { err404('data.maxzoom value must between 1 and 22') }
else {
var params = {
username: req.body.data.username,
template: req.body.data.template,
sql: req.body.data.sql,
minzoom: req.body.data.minzoom ? req.body.data.minzoom : 0,
maxzoom: req.body.data.maxzoom
};
console.log( 'creating job...');
job = queue.create('geojson', params).save(function(err){
if(err) console.error(err.message);
else {
res.status(200).setHeader('Content-Type', 'application/json');
res.json({'id': job.id, 'created_at': new Date(parseInt(job.created_at))});
}
});
queue.process('geojson', function(job, cb){
console.log('processing job: #%d', job.id);
getMbtilesBySQL(job.id, job.data.username, job.data.template, job.data.sql, job.data.minzoom, job.data.maxzoom, cb);
});
}
});
service.get('/api/v1/package_exports/:username/:job/', function(req, res){
var err404 = function(err) {
console.error('%s USER:%s, JOB:%s, ERROR:%s', hlprs.currdatetime(), req.params['username'], req.params['job'], err);
res.status(404).setHeader('Content-Type', 'application/json');
res.json({'params': [{'username': req.params['username'], 'job': req.params['job']}], 'errors': [{'message': err.toString()}]});
}
kue.Job.get(req.params['job'], function(err, job){
if (err) err404(err);
else if (job.data.username !== req.params['username']) err404('Not accessible');
else {
var params = {
id: job.id,
username: job.data.username,
template: job.data.template,
minzoom: job.data.minzoom,
maxzoom: job.data.maxzoom,
bounds: job.data.bounds,
state: job._state,
created_at: new Date(parseInt(job.created_at)),
updated_at: new Date(parseInt(job.updated_at)),
started_at: new Date(parseInt(job.started_at)),
}
if (job._state == 'complete') {
params.download_url = conf.download_url.replace('{username}', job.data.username).replace('{id}', job.id);
params.duration = hlprs.msToTime(job.duration);
}
if (job._state == 'failed') {
params.error = job._error;
params.failed_at = new Date(parseInt(job.failed_at))
}
res.status(200).setHeader('Content-Type', 'application/json');
res.json(params);
}
});
});
service.get('/api/v1/package/get/:username/:job/', function(req, res){
var err404 = function(err) {
console.error('%s USER:%s, JOB:%s, ERROR:%s', hlprs.currdatetime(), req.params['username'], req.params['job'], err);
res.status(404).setHeader('Content-Type', 'application/json');
res.json({'params': [{'username': req.params['username'], 'job': req.params['job']}], 'errors': [{'message': err.toString()}]});
}
kue.Job.get(req.params['job'], function(err, job){
if (err) err404(err);
else if (job.data.username !== req.params['username']) err404('Not accessible');
if (job._state == 'complete') {
var filename = conf.output + '/' + req.params['username'] + '_' + req.params['job'];
if (fs.existsSync(filename + '.zip')) {
console.log('downloaded - ' + filename + '.zip');
res.download(filename + '.zip');
}
else err404('No file - ' + filename);
}
else {
err404('Couldnt get package, job status: ' + job._state);
}
});
});
console.log('Listening server at port ' + conf.server_port.toString());
service.listen(conf.server_port);