2011-08-24 04:42:27 +08:00
|
|
|
// CartoDB SQL API
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
|
|
|
// all requests expect the following URL args:
|
|
|
|
// - `sql` {String} SQL to execute
|
|
|
|
//
|
|
|
|
// for private (read/write) queries:
|
2011-08-24 04:42:27 +08:00
|
|
|
// - OAuth. Must have proper OAuth 1.1 headers. For OAuth 1.1 spec see Google
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
2011-08-24 04:42:27 +08:00
|
|
|
// eg. /api/v1/?sql=SELECT 1 as one (with a load of OAuth headers or URL arguments)
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
|
|
|
// for public (read only) queries:
|
2011-08-24 04:42:27 +08:00
|
|
|
// - sql only, provided the subdomain exists in CartoDB and the table's sharing options are public
|
2011-06-13 11:23:02 +08:00
|
|
|
//
|
2011-08-24 04:42:27 +08:00
|
|
|
// eg. vizzuality.cartodb.com/api/v1/?sql=SELECT * from my_table
|
2012-07-24 16:29:47 +08:00
|
|
|
//
|
|
|
|
//
|
2012-11-12 19:37:34 +08:00
|
|
|
|
|
|
|
var path = require('path');
|
|
|
|
|
2012-04-13 22:37:09 +08:00
|
|
|
var express = require('express')
|
2011-09-21 10:15:43 +08:00
|
|
|
, app = express.createServer(
|
2011-12-14 08:50:56 +08:00
|
|
|
express.logger({
|
|
|
|
buffer: true,
|
|
|
|
format: '[:date] :req[X-Real-IP] \033[90m:method\033[0m \033[36m:req[Host]:url\033[0m \033[90m:status :response-time ms -> :res[Content-Type]\033[0m'
|
|
|
|
}))
|
2012-05-01 23:46:30 +08:00
|
|
|
, Step = require('step')
|
|
|
|
, csv = require('csv')
|
|
|
|
, crypto = require('crypto')
|
2012-10-15 16:13:39 +08:00
|
|
|
, fs = require('fs')
|
|
|
|
, zlib = require('zlib')
|
2012-12-07 21:27:02 +08:00
|
|
|
, strftime = require('strftime')
|
2012-10-25 18:38:45 +08:00
|
|
|
, util = require('util')
|
2012-10-15 16:13:39 +08:00
|
|
|
, spawn = require('child_process').spawn
|
2012-05-01 23:46:30 +08:00
|
|
|
, Meta = require(global.settings.app_root + '/app/models/metadata')
|
|
|
|
, oAuth = require(global.settings.app_root + '/app/models/oauth')
|
|
|
|
, PSQL = require(global.settings.app_root + '/app/models/psql')
|
|
|
|
, ApiKeyAuth = require(global.settings.app_root + '/app/models/apikey_auth')
|
|
|
|
, _ = require('underscore')
|
|
|
|
, tableCache = {};
|
2011-08-24 04:42:27 +08:00
|
|
|
|
2011-09-07 19:05:10 +08:00
|
|
|
app.use(express.bodyParser());
|
2011-08-24 04:42:27 +08:00
|
|
|
app.enable('jsonp callback');
|
2011-09-07 19:05:10 +08:00
|
|
|
|
2012-05-01 23:46:30 +08:00
|
|
|
// basic routing
|
|
|
|
app.all('/api/v1/sql', function(req, res) { handleQuery(req, res) } );
|
2011-11-08 03:24:08 +08:00
|
|
|
app.all('/api/v1/sql.:f', function(req, res) { handleQuery(req, res) } );
|
2012-05-01 23:46:30 +08:00
|
|
|
app.get('/api/v1/cachestatus', function(req, res) { handleCacheStatus(req, res) } );
|
|
|
|
|
2012-10-15 19:20:37 +08:00
|
|
|
// Return true of the given query may write to the database
|
|
|
|
//
|
|
|
|
// NOTE: this is a fuzzy check, the return could be true even
|
|
|
|
// if the query doesn't really write anything.
|
|
|
|
// But you can be pretty sure of a false return.
|
|
|
|
//
|
|
|
|
function queryMayWrite(sql) {
|
2012-11-13 23:47:04 +08:00
|
|
|
var mayWrite = false;
|
2012-10-15 19:40:04 +08:00
|
|
|
var pattern = RegExp("(alter|insert|update|delete|create|drop)", "i");
|
2012-10-15 19:20:37 +08:00
|
|
|
if ( pattern.test(sql) ) {
|
|
|
|
mayWrite = true;
|
|
|
|
}
|
|
|
|
return mayWrite;
|
|
|
|
}
|
|
|
|
|
2012-10-18 17:33:35 +08:00
|
|
|
// Return database username from user_id
|
|
|
|
// NOTE: a "null" user_id is a request to use the public user
|
|
|
|
function userid_to_dbuser(user_id) {
|
2012-11-13 23:47:04 +08:00
|
|
|
if ( _.isString(user_id) )
|
2012-10-18 17:33:35 +08:00
|
|
|
return _.template(global.settings.db_user, {user_id: user_id});
|
|
|
|
return "publicuser" // FIXME: make configurable
|
|
|
|
};
|
|
|
|
|
2012-11-12 19:37:34 +08:00
|
|
|
function sanitize_filename(filename) {
|
|
|
|
filename = path.basename(filename, path.extname(filename));
|
|
|
|
filename = filename.replace(/[;()\[\]<>'"\s]/g, '_');
|
|
|
|
//console.log("Sanitized: " + filename);
|
|
|
|
return filename;
|
|
|
|
}
|
2012-10-18 17:33:35 +08:00
|
|
|
|
2012-05-01 23:46:30 +08:00
|
|
|
// request handlers
|
2012-06-30 07:54:53 +08:00
|
|
|
function handleQuery(req, res) {
|
2011-07-04 23:28:39 +08:00
|
|
|
|
2013-01-10 00:43:23 +08:00
|
|
|
var supportedFormats = ['json', 'geojson', 'topojson', 'csv', 'svg', 'shp', 'kml'];
|
2012-10-12 18:17:35 +08:00
|
|
|
var svg_width = 1024.0;
|
|
|
|
var svg_height = 768.0;
|
|
|
|
|
2012-06-07 02:47:21 +08:00
|
|
|
// extract input
|
2011-09-07 19:05:10 +08:00
|
|
|
var body = (req.body) ? req.body : {};
|
2012-06-07 02:47:21 +08:00
|
|
|
var sql = req.query.q || body.q; // HTTP GET and POST store in different vars
|
2011-12-27 02:16:41 +08:00
|
|
|
var api_key = req.query.api_key || body.api_key;
|
2012-11-13 23:47:04 +08:00
|
|
|
var database = req.query.database; // TODO: Deprecate
|
2011-08-24 04:42:27 +08:00
|
|
|
var limit = parseInt(req.query.rows_per_page);
|
|
|
|
var offset = parseInt(req.query.page);
|
2012-11-13 23:47:04 +08:00
|
|
|
var requestedFormat = req.query.format || body.format;
|
|
|
|
var format = _.isArray(requestedFormat) ? _.last(requestedFormat) : requestedFormat;
|
|
|
|
var requestedFilename = req.query.filename || body.filename
|
|
|
|
var filename = requestedFilename;
|
|
|
|
var requestedSkipfields = req.query.skipfields || body.skipfields;
|
|
|
|
var skipfields = requestedSkipfields ? requestedSkipfields.split(',') : [];
|
|
|
|
var dp = req.query.dp || body.dp; // decimal point digits (defaults to 6)
|
|
|
|
var gn = "the_geom"; // TODO: read from configuration file
|
2012-10-15 16:13:39 +08:00
|
|
|
var user_id;
|
2012-09-11 18:22:27 +08:00
|
|
|
|
2012-06-07 02:47:21 +08:00
|
|
|
// sanitize and apply defaults to input
|
2012-06-30 07:54:53 +08:00
|
|
|
dp = (dp === "" || _.isUndefined(dp)) ? '6' : dp;
|
2012-10-12 18:17:35 +08:00
|
|
|
format = (format === "" || _.isUndefined(format)) ? 'json' : format.toLowerCase();
|
2012-11-12 19:37:34 +08:00
|
|
|
filename = (filename === "" || _.isUndefined(filename)) ? 'cartodb-query' : sanitize_filename(filename);
|
2012-06-07 02:47:21 +08:00
|
|
|
sql = (sql === "" || _.isUndefined(sql)) ? null : sql;
|
|
|
|
database = (database === "" || _.isUndefined(database)) ? null : database;
|
2011-08-24 04:42:27 +08:00
|
|
|
limit = (_.isNumber(limit)) ? limit : null;
|
2012-06-07 02:47:21 +08:00
|
|
|
offset = (_.isNumber(offset)) ? offset * limit : null;
|
2011-08-24 04:42:27 +08:00
|
|
|
|
2011-08-24 19:54:26 +08:00
|
|
|
// setup step run
|
2011-08-24 04:42:27 +08:00
|
|
|
var start = new Date().getTime();
|
2012-05-01 23:46:30 +08:00
|
|
|
|
2011-08-24 04:42:27 +08:00
|
|
|
try {
|
2012-10-12 18:17:35 +08:00
|
|
|
|
|
|
|
if ( -1 === supportedFormats.indexOf(format) )
|
|
|
|
throw new Error("Invalid format: " + format);
|
|
|
|
|
2011-08-24 04:42:27 +08:00
|
|
|
if (!_.isString(sql)) throw new Error("You must indicate a sql query");
|
2012-05-08 22:28:22 +08:00
|
|
|
|
|
|
|
// initialise MD5 key of sql for cache lookups
|
|
|
|
var sql_md5 = generateMD5(sql);
|
|
|
|
|
|
|
|
// placeholder for connection
|
2012-05-08 22:25:19 +08:00
|
|
|
var pg;
|
2011-08-24 04:42:27 +08:00
|
|
|
|
2012-10-15 19:20:37 +08:00
|
|
|
var authenticated;
|
|
|
|
|
2011-08-24 04:42:27 +08:00
|
|
|
// 1. Get database from redis via the username stored in the host header subdomain
|
|
|
|
// 2. Run the request through OAuth to get R/W user id if signed
|
2012-04-21 02:34:18 +08:00
|
|
|
// 3. Get the list of tables affected by the query
|
|
|
|
// 4. Run query with r/w or public user
|
|
|
|
// 5. package results and send back
|
2011-08-24 04:42:27 +08:00
|
|
|
Step(
|
2012-06-30 07:54:53 +08:00
|
|
|
function getDatabaseName() {
|
|
|
|
if (_.isNull(database)) {
|
|
|
|
Meta.getDatabase(req, this);
|
|
|
|
} else {
|
|
|
|
// database hardcoded in query string (deprecated??): don't use redis
|
|
|
|
return database;
|
|
|
|
}
|
2011-08-24 04:42:27 +08:00
|
|
|
},
|
|
|
|
function setDBGetUser(err, data) {
|
|
|
|
if (err) throw err;
|
2012-06-30 07:54:53 +08:00
|
|
|
|
|
|
|
database = (data === "" || _.isNull(data) || _.isUndefined(data)) ? database : data;
|
2012-06-07 02:47:21 +08:00
|
|
|
|
|
|
|
// If the database could not be found, the user is non-existant
|
|
|
|
if (_.isNull(database)) {
|
|
|
|
var msg = "Sorry, we can't find this CartoDB. Please check that you have entered the correct domain.";
|
|
|
|
err = new Error(msg);
|
|
|
|
err.http_status = 404;
|
2012-05-12 04:46:45 +08:00
|
|
|
throw err;
|
|
|
|
}
|
2012-05-08 22:25:19 +08:00
|
|
|
|
2011-12-27 02:16:41 +08:00
|
|
|
if(api_key) {
|
|
|
|
ApiKeyAuth.verifyRequest(req, this);
|
|
|
|
} else {
|
|
|
|
oAuth.verifyRequest(req, this);
|
|
|
|
}
|
2011-08-24 04:42:27 +08:00
|
|
|
},
|
2012-10-15 16:13:39 +08:00
|
|
|
function queryExplain(err, data){
|
2011-08-24 04:42:27 +08:00
|
|
|
if (err) throw err;
|
2012-10-15 16:13:39 +08:00
|
|
|
user_id = data;
|
2012-04-21 02:34:18 +08:00
|
|
|
// store postgres connection
|
2011-08-24 04:42:27 +08:00
|
|
|
pg = new PSQL(user_id, database, limit, offset);
|
2011-10-28 19:11:18 +08:00
|
|
|
|
2012-10-15 19:20:37 +08:00
|
|
|
authenticated = ! _.isNull(user_id);
|
|
|
|
|
2012-05-01 23:46:30 +08:00
|
|
|
// get all the tables from Cache or SQL
|
|
|
|
if (!_.isNull(tableCache[sql_md5]) && !_.isUndefined(tableCache[sql_md5])){
|
|
|
|
tableCache[sql_md5].hits++;
|
|
|
|
return true;
|
2012-10-15 19:20:37 +08:00
|
|
|
} else {
|
|
|
|
pg.query("SELECT CDB_QueryTables($quotesql$" + sql + "$quotesql$)", this);
|
2012-05-01 23:46:30 +08:00
|
|
|
}
|
2012-04-21 02:34:18 +08:00
|
|
|
},
|
|
|
|
function queryResult(err, result){
|
|
|
|
if (err) throw err;
|
2012-04-21 04:09:31 +08:00
|
|
|
|
2012-05-01 23:46:30 +08:00
|
|
|
// store explain result in local Cache
|
|
|
|
if (_.isUndefined(tableCache[sql_md5])){
|
|
|
|
tableCache[sql_md5] = result;
|
2012-10-15 19:20:37 +08:00
|
|
|
tableCache[sql_md5].may_write = queryMayWrite(sql);
|
2012-05-01 23:46:30 +08:00
|
|
|
tableCache[sql_md5].hits = 1; //initialise hit counter
|
|
|
|
}
|
2012-04-21 02:34:18 +08:00
|
|
|
|
2011-10-28 19:11:18 +08:00
|
|
|
// TODO: refactor formats to external object
|
2013-01-10 00:43:23 +08:00
|
|
|
if (format === 'geojson' || format === 'topojson' ){
|
2013-01-12 01:57:45 +08:00
|
|
|
sql = 'SELECT *, ST_AsGeoJSON(' + gn + ',' + dp
|
|
|
|
+ ') as the_geom FROM (' + sql + ') as foo';
|
2013-01-12 01:43:05 +08:00
|
|
|
if ( format === 'topojson' ) {
|
2013-01-12 01:57:45 +08:00
|
|
|
// see https://github.com/Vizzuality/CartoDB-SQL-API/issues/80
|
|
|
|
sql += ' where ' + gn + ' is not null';
|
2013-01-12 01:43:05 +08:00
|
|
|
}
|
2012-10-15 16:13:39 +08:00
|
|
|
} else if (format === 'shp') {
|
|
|
|
return null;
|
2012-10-12 18:17:35 +08:00
|
|
|
} else if (format === 'svg') {
|
2012-09-11 18:22:27 +08:00
|
|
|
var svg_ratio = svg_width/svg_height;
|
2012-11-13 23:47:04 +08:00
|
|
|
sql = 'WITH source AS ( ' + sql + '), extent AS ( '
|
2012-09-11 18:22:27 +08:00
|
|
|
+ ' SELECT ST_Extent(' + gn + ') AS e FROM source '
|
|
|
|
+ '), extent_info AS ( SELECT e, '
|
|
|
|
+ 'st_xmin(e) as ex0, st_ymax(e) as ey0, '
|
|
|
|
+ 'st_xmax(e)-st_xmin(e) as ew, '
|
|
|
|
+ 'st_ymax(e)-st_ymin(e) as eh FROM extent )'
|
|
|
|
+ ', trans AS ( SELECT CASE WHEN '
|
|
|
|
+ 'eh = 0 THEN ' + svg_width
|
|
|
|
+ '/ COALESCE(NULLIF(ew,0),' + svg_width +') WHEN '
|
|
|
|
+ svg_ratio + ' <= (ew / eh) THEN ('
|
|
|
|
+ svg_width + '/ew ) ELSE ('
|
|
|
|
+ svg_height + '/eh ) END as s '
|
|
|
|
+ ', ex0 as x0, ey0 as y0 FROM extent_info ) '
|
|
|
|
+ 'SELECT st_TransScale(e, -x0, -y0, s, s)::box2d as '
|
|
|
|
+ gn + '_box, ST_Dimension(' + gn + ') as ' + gn
|
|
|
|
+ '_dimension, ST_AsSVG(ST_TransScale(' + gn + ', '
|
|
|
|
+ '-x0, -y0, s, s), 0, ' + dp + ') as ' + gn
|
|
|
|
//+ ', ex0, ey0, ew, eh, s ' // DEBUG ONLY
|
|
|
|
+ ' FROM trans, extent_info, source';
|
2011-10-28 19:11:18 +08:00
|
|
|
}
|
|
|
|
|
2011-08-24 04:42:27 +08:00
|
|
|
pg.query(sql, this);
|
|
|
|
},
|
2012-04-13 22:37:09 +08:00
|
|
|
function setHeaders(err, result){
|
|
|
|
if (err) throw err;
|
|
|
|
|
2012-10-25 19:40:21 +08:00
|
|
|
// configure headers for given format
|
2012-11-14 23:30:18 +08:00
|
|
|
var use_inline = !requestedFormat && !requestedFilename;
|
2012-11-13 02:44:16 +08:00
|
|
|
res.header("Content-Disposition", getContentDisposition(format, filename, use_inline));
|
2012-04-13 22:37:09 +08:00
|
|
|
res.header("Content-Type", getContentType(format));
|
|
|
|
|
|
|
|
// allow cross site post
|
|
|
|
setCrossDomain(res);
|
|
|
|
|
|
|
|
// set cache headers
|
2012-10-15 19:20:37 +08:00
|
|
|
res.header('X-Cache-Channel', generateCacheKey(database, tableCache[sql_md5], authenticated));
|
2012-11-13 02:14:20 +08:00
|
|
|
var cache_policy = req.query.cache_policy;
|
|
|
|
if ( cache_policy == 'persist' ) {
|
|
|
|
res.header('Cache-Control', 'public,max-age=31536000'); // 1 year
|
|
|
|
} else {
|
|
|
|
// TODO: set ttl=0 when tableCache[sql_md5].may_write is true ?
|
|
|
|
var ttl = 3600;
|
|
|
|
res.header('Last-Modified', new Date().toUTCString());
|
|
|
|
res.header('Cache-Control', 'no-cache,max-age='+ttl+',must-revalidate,public');
|
|
|
|
}
|
|
|
|
|
2012-04-13 22:37:09 +08:00
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
2011-08-24 04:42:27 +08:00
|
|
|
function packageResults(err, result){
|
|
|
|
if (err) throw err;
|
2011-10-28 19:11:18 +08:00
|
|
|
|
2012-11-13 00:10:16 +08:00
|
|
|
if ( skipfields.length ){
|
|
|
|
for ( var i=0; i<result.rows.length; ++i ) {
|
|
|
|
for ( var j=0; j<skipfields.length; ++j ) {
|
|
|
|
delete result.rows[i][skipfields[j]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-28 19:11:18 +08:00
|
|
|
// TODO: refactor formats to external object
|
|
|
|
if (format === 'geojson'){
|
2013-01-10 00:43:23 +08:00
|
|
|
toGeoJSON(result, gn, this);
|
|
|
|
} else if (format === 'topojson'){
|
2013-01-11 18:10:02 +08:00
|
|
|
toTopoJSON(result, gn, skipfields, this);
|
2012-09-11 18:22:27 +08:00
|
|
|
} else if (format === 'svg'){
|
|
|
|
toSVG(result.rows, gn, this);
|
2012-04-13 07:30:45 +08:00
|
|
|
} else if (format === 'csv'){
|
|
|
|
toCSV(result, res, this);
|
2012-10-15 16:13:39 +08:00
|
|
|
} else if ( format === 'shp'){
|
2012-11-15 01:04:38 +08:00
|
|
|
toSHP(database, user_id, gn, sql, skipfields, filename, res, this);
|
2012-10-25 18:38:45 +08:00
|
|
|
} else if ( format === 'kml'){
|
2012-11-15 01:04:38 +08:00
|
|
|
toKML(database, user_id, gn, sql, skipfields, res, this);
|
2012-10-12 18:57:03 +08:00
|
|
|
} else if ( format === 'json'){
|
2011-10-28 19:11:18 +08:00
|
|
|
var end = new Date().getTime();
|
2012-06-30 08:34:43 +08:00
|
|
|
|
|
|
|
var json_result = {'time' : (end - start)/1000};
|
|
|
|
json_result.total_rows = result.rowCount;
|
2012-09-17 17:50:19 +08:00
|
|
|
json_result.rows = result.rows;
|
2012-06-30 08:34:43 +08:00
|
|
|
return json_result;
|
2011-10-28 19:11:18 +08:00
|
|
|
}
|
2012-10-12 18:57:03 +08:00
|
|
|
else throw new Error("Unexpected format in packageResults: " + format);
|
2011-10-28 19:11:18 +08:00
|
|
|
},
|
|
|
|
function sendResults(err, out){
|
|
|
|
if (err) throw err;
|
|
|
|
|
|
|
|
// return to browser
|
2012-10-15 16:13:39 +08:00
|
|
|
if ( out ) res.send(out);
|
2011-08-24 04:42:27 +08:00
|
|
|
},
|
|
|
|
function errorHandle(err, result){
|
|
|
|
handleException(err, res);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} catch (err) {
|
2011-09-07 19:05:10 +08:00
|
|
|
console.log('[ERROR]\n' + err);
|
2011-08-18 01:42:19 +08:00
|
|
|
handleException(err, res);
|
2011-08-24 04:42:27 +08:00
|
|
|
}
|
2011-09-07 19:05:10 +08:00
|
|
|
}
|
|
|
|
|
2012-05-01 23:46:30 +08:00
|
|
|
function handleCacheStatus(req, res){
|
|
|
|
var tableCacheValues = _.values(tableCache);
|
|
|
|
var totalExplainHits = _.reduce(tableCacheValues, function(memo, res) { return memo + res.hits}, 0);
|
|
|
|
var totalExplainKeys = tableCacheValues.length;
|
|
|
|
|
|
|
|
res.send({explain: {hits: totalExplainHits, keys : totalExplainKeys }});
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper functions
|
2013-01-10 00:43:23 +08:00
|
|
|
function toGeoJSON(data, gn, callback){
|
2011-10-28 19:11:18 +08:00
|
|
|
try{
|
|
|
|
var out = {
|
|
|
|
type: "FeatureCollection",
|
|
|
|
features: []
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each(data.rows, function(ele){
|
|
|
|
var geojson = {
|
|
|
|
type: "Feature",
|
|
|
|
properties: { },
|
|
|
|
geometry: { }
|
|
|
|
};
|
2013-01-10 00:43:23 +08:00
|
|
|
geojson.geometry = JSON.parse(ele[gn]);
|
|
|
|
delete ele[gn];
|
|
|
|
delete ele["the_geom_webmercator"]; // TODO: use skipfields
|
2011-10-28 19:11:18 +08:00
|
|
|
geojson.properties = ele;
|
|
|
|
out.features.push(geojson);
|
|
|
|
});
|
|
|
|
|
|
|
|
// return payload
|
|
|
|
callback(null, out);
|
|
|
|
} catch (err) {
|
|
|
|
callback(err,null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-11 18:10:02 +08:00
|
|
|
function toTopoJSON(data, gn, skipfields, callback){
|
2013-01-10 00:43:23 +08:00
|
|
|
toGeoJSON(data, gn, function(err, geojson) {
|
|
|
|
if ( err ) {
|
|
|
|
callback(err, null);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var TopoJSON = require('topojson');
|
2013-01-11 18:10:02 +08:00
|
|
|
var topology = TopoJSON.topology(geojson.features, {
|
|
|
|
/* TODO: expose option to API for requesting an identifier
|
|
|
|
"id": function(o) {
|
|
|
|
console.log("id called with obj: "); console.dir(o);
|
|
|
|
return o;
|
|
|
|
},
|
|
|
|
*/
|
|
|
|
"quantization": 1e4, // TODO: expose option to API (use existing "dp" for this ?)
|
|
|
|
"force-clockwise": true,
|
|
|
|
"property-filter": function(d) {
|
|
|
|
// TODO: delegate skipfields handling to toGeoJSON
|
|
|
|
return skipfields.indexOf(d) != -1 ? null : d;
|
|
|
|
}
|
|
|
|
});
|
2013-01-10 00:43:23 +08:00
|
|
|
callback(err, topology);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-09-11 18:22:27 +08:00
|
|
|
function toSVG(rows, gn, callback){
|
|
|
|
|
|
|
|
var radius = 5; // in pixels (based on svg_width and svg_height)
|
|
|
|
var stroke_width = 1; // in pixels (based on svg_width and svg_height)
|
|
|
|
var stroke_color = 'black';
|
|
|
|
// fill settings affect polygons and points (circles)
|
|
|
|
var fill_opacity = 0.5; // 0.0 is fully transparent, 1.0 is fully opaque
|
|
|
|
// unused if fill_color='none'
|
|
|
|
var fill_color = 'none'; // affects polygons and circles
|
|
|
|
|
|
|
|
var bbox; // will be computed during the results scan
|
|
|
|
var polys = [];
|
|
|
|
var lines = [];
|
|
|
|
var points = [];
|
|
|
|
_.each(rows, function(ele){
|
2013-01-21 17:06:51 +08:00
|
|
|
if ( ! ele.hasOwnProperty(gn) ) {
|
|
|
|
throw new Error('column "' + gn + '" does not exist');
|
|
|
|
}
|
2012-09-11 18:22:27 +08:00
|
|
|
var g = ele[gn];
|
|
|
|
if ( ! g ) return; // null or empty
|
|
|
|
var gdims = ele[gn + '_dimension'];
|
|
|
|
|
|
|
|
// TODO: add an identifier, if any of "cartodb_id", "oid", "id", "gid" are found
|
|
|
|
// TODO: add "class" attribute to help with styling ?
|
|
|
|
if ( gdims == '0' ) {
|
|
|
|
points.push('<circle r="[RADIUS]" ' + g + ' />');
|
|
|
|
} else if ( gdims == '1' ) {
|
|
|
|
// Avoid filling closed linestrings
|
|
|
|
var linetag = '<path ';
|
|
|
|
if ( fill_color != 'none' ) linetag += 'fill="none" '
|
|
|
|
linetag += 'd="' + g + '" />';
|
|
|
|
lines.push(linetag);
|
|
|
|
} else if ( gdims == '2' ) {
|
|
|
|
polys.push('<path d="' + g + '" />');
|
|
|
|
}
|
|
|
|
|
2012-11-13 23:47:04 +08:00
|
|
|
if ( ! bbox ) {
|
2012-09-11 18:22:27 +08:00
|
|
|
// Parse layer extent: "BOX(x y, X Y)"
|
|
|
|
// NOTE: the name of the extent field is
|
|
|
|
// determined by the same code adding the
|
|
|
|
// ST_AsSVG call (in queryResult)
|
|
|
|
//
|
|
|
|
bbox = ele[gn + '_box'];
|
|
|
|
bbox = bbox.match(/BOX\(([^ ]*) ([^ ,]*),([^ ]*) ([^)]*)\)/);
|
|
|
|
bbox = {
|
|
|
|
xmin: parseFloat(bbox[1]),
|
2012-11-13 23:47:04 +08:00
|
|
|
ymin: parseFloat(bbox[2]),
|
2012-09-11 18:22:27 +08:00
|
|
|
xmax: parseFloat(bbox[3]),
|
2012-11-13 23:47:04 +08:00
|
|
|
ymax: parseFloat(bbox[4])
|
2012-09-11 18:22:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set point radius
|
|
|
|
for (var i=0; i<points.length; ++i) {
|
|
|
|
points[i] = points[i].replace('[RADIUS]', radius);
|
|
|
|
}
|
|
|
|
|
|
|
|
var header_tags = [
|
|
|
|
'<?xml version="1.0" standalone="no"?>',
|
|
|
|
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">',
|
|
|
|
];
|
|
|
|
|
|
|
|
var root_tag = '<svg ';
|
|
|
|
if ( bbox ) {
|
|
|
|
// expand box by "radius" + "stroke-width"
|
|
|
|
// TODO: use a Box2d class for these ops
|
|
|
|
var growby = radius+stroke_width;
|
|
|
|
bbox.xmin -= growby;
|
|
|
|
bbox.ymin -= growby;
|
|
|
|
bbox.xmax += growby;
|
|
|
|
bbox.ymax += growby;
|
|
|
|
bbox.width = bbox.xmax - bbox.xmin;
|
|
|
|
bbox.height = bbox.ymax - bbox.ymin;
|
|
|
|
root_tag += 'viewBox="' + bbox.xmin + ' ' + (-bbox.ymax) + ' '
|
2012-11-13 23:47:04 +08:00
|
|
|
+ bbox.width + ' ' + bbox.height + '" ';
|
2012-09-11 18:22:27 +08:00
|
|
|
}
|
|
|
|
root_tag += 'style="fill-opacity:' + fill_opacity
|
|
|
|
+ '; stroke:' + stroke_color
|
|
|
|
+ '; stroke-width:' + stroke_width
|
|
|
|
+ '; fill:' + fill_color
|
|
|
|
+ '" ';
|
|
|
|
root_tag += 'xmlns="http://www.w3.org/2000/svg" version="1.1">';
|
|
|
|
|
|
|
|
header_tags.push(root_tag);
|
|
|
|
|
|
|
|
// Render points on top of lines and lines on top of polys
|
|
|
|
var out = header_tags.concat(polys, lines, points);
|
|
|
|
|
|
|
|
out.push('</svg>');
|
|
|
|
|
|
|
|
// return payload
|
|
|
|
callback(null, out.join("\n"));
|
|
|
|
}
|
|
|
|
|
2012-04-13 07:30:45 +08:00
|
|
|
function toCSV(data, res, callback){
|
|
|
|
try{
|
2012-04-13 22:37:09 +08:00
|
|
|
// pull out keys for column headers
|
2012-10-25 19:05:37 +08:00
|
|
|
var columns = data.rows.length ? _.keys(data.rows[0]) : [];
|
2012-04-13 22:37:09 +08:00
|
|
|
|
|
|
|
// stream the csv out over http
|
|
|
|
csv()
|
2012-11-20 01:44:53 +08:00
|
|
|
.from(data.rows)
|
2012-12-07 21:27:02 +08:00
|
|
|
.toStream(res, {end: true, columns: columns, header: true})
|
|
|
|
.transform( function(data) {
|
|
|
|
for (var i in data) {
|
|
|
|
// convert dates to string
|
|
|
|
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/77
|
|
|
|
// TODO: take a format string as a parameter
|
|
|
|
if ( data[i] instanceof Date ) {
|
|
|
|
// ISO time
|
|
|
|
data[i] = strftime('%F %H:%M:%S', data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
})
|
|
|
|
;
|
|
|
|
|
2012-04-13 22:37:09 +08:00
|
|
|
return true;
|
2012-04-13 07:30:45 +08:00
|
|
|
} catch (err) {
|
|
|
|
callback(err,null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 23:45:21 +08:00
|
|
|
// Internal function usable by all OGR-driven outputs
|
2012-11-15 01:04:38 +08:00
|
|
|
function toOGR(dbname, user_id, gcol, sql, skipfields, res, out_format, out_filename, callback) {
|
2012-10-15 16:13:39 +08:00
|
|
|
var ogr2ogr = 'ogr2ogr'; // FIXME: make configurable
|
2012-11-13 23:47:04 +08:00
|
|
|
var dbhost = global.settings.db_host;
|
|
|
|
var dbport = global.settings.db_port;
|
2012-10-23 23:45:21 +08:00
|
|
|
var dbuser = userid_to_dbuser(user_id);
|
2012-10-15 16:13:39 +08:00
|
|
|
var dbpass = ''; // turn into a parameter..
|
2012-10-23 23:45:21 +08:00
|
|
|
|
|
|
|
var tmpdir = '/tmp'; // FIXME: make configurable
|
2012-10-15 16:13:39 +08:00
|
|
|
var columns = [];
|
|
|
|
|
|
|
|
Step (
|
|
|
|
|
2012-10-23 23:45:21 +08:00
|
|
|
function fetchColumns() {
|
2012-10-15 16:13:39 +08:00
|
|
|
var colsql = 'SELECT * FROM (' + sql + ') as _cartodbsqlapi LIMIT 1';
|
|
|
|
var pg = new PSQL(user_id, dbname, 1, 0);
|
|
|
|
pg.query(colsql, this);
|
|
|
|
},
|
|
|
|
function spawnDumper(err, result) {
|
|
|
|
if (err) throw err;
|
|
|
|
|
2012-11-13 23:47:04 +08:00
|
|
|
if ( ! result.rows.length )
|
2012-10-15 16:13:39 +08:00
|
|
|
throw new Error("Query returns no rows");
|
|
|
|
|
|
|
|
// Skip system columns
|
|
|
|
for (var k in result.rows[0]) {
|
2012-11-13 00:10:16 +08:00
|
|
|
if ( skipfields.indexOf(k) != -1 ) continue;
|
2012-11-15 01:04:38 +08:00
|
|
|
if ( k == "the_geom_webmercator" ) continue;
|
2012-10-15 16:13:39 +08:00
|
|
|
columns.push('"' + k + '"');
|
|
|
|
}
|
2012-10-18 19:19:08 +08:00
|
|
|
//console.log(columns.join(','));
|
2012-10-15 16:13:39 +08:00
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
sql = 'SELECT ' + columns.join(',')
|
|
|
|
+ ' FROM (' + sql + ') as _cartodbsqlapi';
|
|
|
|
|
|
|
|
var child = spawn(ogr2ogr, [
|
2012-11-13 23:47:04 +08:00
|
|
|
'-f', out_format,
|
2012-11-16 19:50:21 +08:00
|
|
|
'-lco', 'ENCODING=UTF-8',
|
2012-10-23 23:45:21 +08:00
|
|
|
out_filename,
|
2012-10-15 16:13:39 +08:00
|
|
|
"PG:host=" + dbhost
|
|
|
|
+ " user=" + dbuser
|
|
|
|
+ " dbname=" + dbname
|
|
|
|
+ " password=" + dbpass
|
2012-11-13 23:47:04 +08:00
|
|
|
+ " tables=fake" // trick to skip query to geometry_columns
|
2012-10-15 16:13:39 +08:00
|
|
|
+ "",
|
2012-11-13 23:47:04 +08:00
|
|
|
'-sql', sql
|
2012-10-15 16:13:39 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
console.log(['ogr2ogr',
|
2012-10-23 23:45:21 +08:00
|
|
|
'-f', out_format,
|
|
|
|
out_filename,
|
2012-10-15 16:13:39 +08:00
|
|
|
"'PG:host=" + dbhost
|
|
|
|
+ " user=" + dbuser
|
|
|
|
+ " dbname=" + dbname
|
|
|
|
+ " password=" + dbpass
|
|
|
|
+ " tables=fake" // trick to skip query to geometry_columns
|
|
|
|
+ "'",
|
|
|
|
'-sql "', sql, '"'].join(' '));
|
|
|
|
*/
|
|
|
|
|
|
|
|
var stdout = '';
|
|
|
|
child.stdout.on('data', function(data) {
|
|
|
|
stdout += data;
|
|
|
|
//console.log('stdout: ' + data);
|
|
|
|
});
|
|
|
|
|
|
|
|
var stderr = '';
|
|
|
|
child.stderr.on('data', function(data) {
|
|
|
|
stderr += data;
|
|
|
|
console.log('ogr2ogr stderr: ' + data);
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on('exit', function(code) {
|
|
|
|
if ( code ) {
|
2013-01-12 00:09:22 +08:00
|
|
|
var emsg = stderr.split('\n')[0];
|
|
|
|
// TODO: add more info about this error ?
|
|
|
|
//if ( RegExp(/attempt to write non-.*geometry.*to.*type shapefile/i).exec(emsg) )
|
|
|
|
next(new Error(emsg));
|
2012-10-15 16:13:39 +08:00
|
|
|
} else {
|
2012-10-31 18:56:55 +08:00
|
|
|
next(null);
|
2012-10-15 16:13:39 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2012-10-23 23:45:21 +08:00
|
|
|
function finish(err) {
|
2012-11-13 23:47:04 +08:00
|
|
|
callback(err);
|
2012-10-23 23:45:21 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:04:38 +08:00
|
|
|
function toSHP(dbname, user_id, gcol, sql, skipfields, filename, res, callback) {
|
2012-10-23 23:45:21 +08:00
|
|
|
var zip = 'zip'; // FIXME: make configurable
|
|
|
|
var tmpdir = '/tmp'; // FIXME: make configurable
|
2012-10-31 18:56:55 +08:00
|
|
|
var outdirpath = tmpdir + '/sqlapi-shapefile-' + generateMD5(sql);
|
2012-11-12 19:37:34 +08:00
|
|
|
var shapefile = outdirpath + '/' + filename + '.shp';
|
2012-10-23 23:45:21 +08:00
|
|
|
|
|
|
|
// TODO: following tests:
|
|
|
|
// - fetch with no auth [done]
|
|
|
|
// - fetch with auth [done]
|
|
|
|
// - fetch same query concurrently
|
|
|
|
// - fetch query with no "the_geom" column
|
|
|
|
|
|
|
|
// TODO: Check if the file already exists
|
|
|
|
// (should mean another export of the same query is in progress)
|
|
|
|
|
|
|
|
Step (
|
|
|
|
|
|
|
|
function createOutDir() {
|
|
|
|
fs.mkdir(outdirpath, 0777, this);
|
|
|
|
},
|
|
|
|
function spawnDumper(err) {
|
2012-10-25 18:38:45 +08:00
|
|
|
if ( err ) {
|
|
|
|
if ( err.code == 'EEXIST' ) {
|
|
|
|
// TODO: this could mean another request for the same
|
|
|
|
// resource is in progress, in which case we might want
|
|
|
|
// to queue the response to after it's completed...
|
|
|
|
console.log("Reusing existing SHP output directory for query: " + sql);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 01:04:38 +08:00
|
|
|
toOGR(dbname, user_id, gcol, sql, skipfields, res, 'ESRI Shapefile', shapefile, this);
|
2012-10-23 23:45:21 +08:00
|
|
|
},
|
|
|
|
function zipAndSendDump(err) {
|
2012-10-15 16:13:39 +08:00
|
|
|
if ( err ) throw err;
|
|
|
|
|
|
|
|
var next = this;
|
2012-10-23 23:45:21 +08:00
|
|
|
var dir = outdirpath;
|
2012-10-15 16:13:39 +08:00
|
|
|
|
|
|
|
var zipfile = dir + '.zip';
|
|
|
|
|
|
|
|
var child = spawn(zip, ['-qrj', '-', dir ]);
|
|
|
|
|
2012-11-16 19:18:29 +08:00
|
|
|
// TODO: convert to a stream operation
|
2012-10-15 16:13:39 +08:00
|
|
|
child.stdout.on('data', function(data) {
|
|
|
|
res.write(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
var stderr = '';
|
|
|
|
child.stderr.on('data', function(data) {
|
|
|
|
stderr += data;
|
|
|
|
console.log('zip stderr: ' + data);
|
|
|
|
});
|
|
|
|
|
|
|
|
child.on('exit', function(code) {
|
|
|
|
if (code) {
|
|
|
|
res.statusCode = 500;
|
|
|
|
//res.send(stderr);
|
|
|
|
}
|
|
|
|
//console.log("Zip complete, zip return code was " + code);
|
|
|
|
next(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
function cleanupDir(topError) {
|
|
|
|
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
//console.log("Cleaning up " + outdirpath);
|
|
|
|
|
|
|
|
// Unlink the dir content
|
|
|
|
var unlinkall = function(dir, files, finish) {
|
|
|
|
var f = files.shift();
|
|
|
|
if ( ! f ) { finish(null); return; }
|
|
|
|
var fn = dir + '/' + f;
|
|
|
|
fs.unlink(fn, function(err) {
|
|
|
|
if ( err ) {
|
|
|
|
console.log("Unlinking " + fn + ": " + err);
|
|
|
|
finish(err);
|
|
|
|
}
|
|
|
|
else unlinkall(dir, files, finish)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fs.readdir(outdirpath, function(err, files) {
|
|
|
|
if ( err ) {
|
|
|
|
if ( err.code != 'ENOENT' ) {
|
|
|
|
next(new Error([topError, err].join('\n')));
|
|
|
|
} else {
|
|
|
|
next(topError);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unlinkall(outdirpath, files, function(err) {
|
|
|
|
fs.rmdir(outdirpath, function(err) {
|
|
|
|
if ( err ) console.log("Removing dir " + path + ": " + err);
|
|
|
|
next(topError);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2012-11-13 23:47:04 +08:00
|
|
|
if ( err ) callback(err);
|
2012-10-15 16:13:39 +08:00
|
|
|
else {
|
|
|
|
res.end();
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-15 01:04:38 +08:00
|
|
|
function toKML(dbname, user_id, gcol, sql, skipfields, res, callback) {
|
2012-10-25 18:38:45 +08:00
|
|
|
var zip = 'zip'; // FIXME: make configurable
|
|
|
|
var tmpdir = '/tmp'; // FIXME: make configurable
|
|
|
|
var outdirpath = tmpdir + '/sqlapi-kmloutput-' + generateMD5(sql);
|
|
|
|
var dumpfile = outdirpath + '/cartodb-query.kml';
|
|
|
|
|
|
|
|
// TODO: following tests:
|
2012-11-13 23:47:04 +08:00
|
|
|
// - fetch with no auth
|
|
|
|
// - fetch with auth
|
2012-10-25 18:38:45 +08:00
|
|
|
// - fetch same query concurrently
|
|
|
|
// - fetch query with no "the_geom" column
|
|
|
|
|
|
|
|
Step (
|
|
|
|
|
|
|
|
function createOutDir() {
|
|
|
|
fs.mkdir(outdirpath, 0777, this);
|
|
|
|
},
|
|
|
|
function spawnDumper(err) {
|
|
|
|
if ( err ) {
|
|
|
|
if ( err.code == 'EEXIST' ) {
|
|
|
|
// TODO: this could mean another request for the same
|
|
|
|
// resource is in progress, in which case we might want
|
|
|
|
// to queue the response to after it's completed...
|
|
|
|
console.log("Reusing existing KML output directory for query: " + sql);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 01:04:38 +08:00
|
|
|
toOGR(dbname, user_id, gcol, sql, skipfields, res, 'KML', dumpfile, this);
|
2012-10-25 18:38:45 +08:00
|
|
|
},
|
|
|
|
function sendResults(err) {
|
|
|
|
|
|
|
|
if ( ! err ) {
|
2012-11-16 19:18:29 +08:00
|
|
|
fs.createReadStream(dumpfile).pipe(res);
|
2012-10-25 18:38:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup output dir (should be safe to unlink)
|
|
|
|
var topError = err;
|
|
|
|
var next = this;
|
|
|
|
|
|
|
|
//console.log("Cleaning up " + outdirpath);
|
|
|
|
|
|
|
|
// Unlink the dir content
|
|
|
|
var unlinkall = function(dir, files, finish) {
|
|
|
|
var f = files.shift();
|
|
|
|
if ( ! f ) { finish(null); return; }
|
|
|
|
var fn = dir + '/' + f;
|
|
|
|
fs.unlink(fn, function(err) {
|
|
|
|
if ( err ) {
|
|
|
|
console.log("Unlinking " + fn + ": " + err);
|
|
|
|
finish(err);
|
|
|
|
}
|
|
|
|
else unlinkall(dir, files, finish)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
fs.readdir(outdirpath, function(err, files) {
|
|
|
|
if ( err ) {
|
|
|
|
if ( err.code != 'ENOENT' ) {
|
|
|
|
next(new Error([topError, err].join('\n')));
|
|
|
|
} else {
|
|
|
|
next(topError);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
unlinkall(outdirpath, files, function(err) {
|
|
|
|
fs.rmdir(outdirpath, function(err) {
|
|
|
|
if ( err ) console.log("Removing dir " + path + ": " + err);
|
|
|
|
next(topError);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function finish(err) {
|
2012-11-13 23:47:04 +08:00
|
|
|
if ( err ) callback(err);
|
2012-10-25 18:38:45 +08:00
|
|
|
else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-11-13 02:44:16 +08:00
|
|
|
function getContentDisposition(format, filename, inline) {
|
2012-04-13 07:30:45 +08:00
|
|
|
var ext = 'json';
|
|
|
|
if (format === 'geojson'){
|
|
|
|
ext = 'geojson';
|
|
|
|
}
|
2013-01-10 00:43:23 +08:00
|
|
|
else if (format === 'topojson'){
|
|
|
|
ext = 'topojson';
|
|
|
|
}
|
2012-09-11 18:22:27 +08:00
|
|
|
else if (format === 'csv'){
|
2012-04-13 07:30:45 +08:00
|
|
|
ext = 'csv';
|
|
|
|
}
|
2012-09-11 18:22:27 +08:00
|
|
|
else if (format === 'svg'){
|
|
|
|
ext = 'svg';
|
|
|
|
}
|
2012-10-15 16:13:39 +08:00
|
|
|
else if (format === 'shp'){
|
|
|
|
ext = 'zip';
|
|
|
|
}
|
2012-10-25 18:38:45 +08:00
|
|
|
else if (format === 'kml'){
|
|
|
|
ext = 'kml';
|
|
|
|
}
|
2011-10-28 19:11:18 +08:00
|
|
|
var time = new Date().toUTCString();
|
2012-11-13 02:44:16 +08:00
|
|
|
return ( inline ? 'inline' : 'attachment' ) +'; filename=' + filename + '.' + ext + '; modification-date="' + time + '";';
|
2011-10-28 19:11:18 +08:00
|
|
|
}
|
2011-09-07 19:05:10 +08:00
|
|
|
|
2012-04-13 07:30:45 +08:00
|
|
|
function getContentType(format){
|
|
|
|
var type = "application/json; charset=utf-8";
|
|
|
|
if (format === 'csv'){
|
2012-10-25 19:40:21 +08:00
|
|
|
type = "text/csv; charset=utf-8; header=present";
|
2012-04-13 07:30:45 +08:00
|
|
|
}
|
2012-09-11 18:22:27 +08:00
|
|
|
else if (format === 'svg'){
|
|
|
|
type = "image/svg+xml; charset=utf-8";
|
|
|
|
}
|
2012-10-15 16:13:39 +08:00
|
|
|
else if (format === 'shp'){
|
|
|
|
type = "application/zip; charset=utf-8";
|
|
|
|
}
|
2012-10-25 18:38:45 +08:00
|
|
|
else if (format === 'kml'){
|
|
|
|
type = "application/kml; charset=utf-8";
|
|
|
|
}
|
2012-04-13 07:30:45 +08:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCrossDomain(res){
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
2012-11-01 20:16:46 +08:00
|
|
|
res.header("Access-Control-Allow-Headers", "X-Requested-With, X-Prototype-Version, X-CSRF-Token");
|
2012-04-13 07:30:45 +08:00
|
|
|
}
|
|
|
|
|
2012-10-15 19:20:37 +08:00
|
|
|
function generateCacheKey(database,tables,is_authenticated){
|
|
|
|
if ( is_authenticated && tables.may_write ) {
|
|
|
|
return "NONE";
|
|
|
|
} else {
|
2012-11-13 23:47:04 +08:00
|
|
|
return database + ":" + tables.rows[0].cdb_querytables.split(/^\{(.*)\}$/)[1];
|
2012-10-15 19:20:37 +08:00
|
|
|
}
|
2012-04-21 02:34:18 +08:00
|
|
|
}
|
|
|
|
|
2012-05-01 23:46:30 +08:00
|
|
|
function generateMD5(data){
|
|
|
|
var hash = crypto.createHash('md5');
|
|
|
|
hash.update(data);
|
|
|
|
return hash.digest('hex');
|
|
|
|
}
|
|
|
|
|
2011-08-18 00:27:45 +08:00
|
|
|
function handleException(err, res){
|
2011-08-24 04:42:27 +08:00
|
|
|
var msg = (global.settings.environment == 'development') ? {error:[err.message], stack: err.stack} : {error:[err.message]}
|
2011-08-25 03:47:10 +08:00
|
|
|
if (global.settings.environment !== 'test'){
|
|
|
|
// TODO: email this Exception report
|
|
|
|
console.log("EXCEPTION REPORT")
|
|
|
|
console.log(err.message);
|
|
|
|
console.log(err.stack);
|
|
|
|
}
|
2011-10-28 19:11:18 +08:00
|
|
|
|
2012-04-13 07:30:45 +08:00
|
|
|
// allow cross site post
|
|
|
|
setCrossDomain(res);
|
|
|
|
|
2012-06-07 02:47:21 +08:00
|
|
|
// if the exception defines a http status code, use that, else a 400
|
2011-11-22 08:06:14 +08:00
|
|
|
if (!_.isUndefined(err.http_status)){
|
|
|
|
res.send(msg, err.http_status);
|
|
|
|
} else {
|
|
|
|
res.send(msg, 400);
|
|
|
|
}
|
2011-06-13 20:07:21 +08:00
|
|
|
}
|
|
|
|
|
2011-06-20 21:39:12 +08:00
|
|
|
module.exports = app;
|