2018-04-13 03:25:28 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const userMiddleware = require('../middlewares/user');
|
|
|
|
const errorMiddleware = require('../middlewares/error');
|
|
|
|
const authorizationMiddleware = require('../middlewares/authorization');
|
|
|
|
const connectionParamsMiddleware = require('../middlewares/connection-params');
|
|
|
|
const timeoutLimitsMiddleware = require('../middlewares/timeout-limits');
|
|
|
|
const { initializeProfilerMiddleware } = require('../middlewares/profiler');
|
|
|
|
const rateLimitsMiddleware = require('../middlewares/rate-limit');
|
|
|
|
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
|
2018-05-22 01:13:44 +08:00
|
|
|
const { getFormatFromCopyQuery } = require('../utils/query_info');
|
2018-04-13 03:25:28 +08:00
|
|
|
|
2018-05-11 19:33:54 +08:00
|
|
|
const zlib = require('zlib');
|
2018-05-08 19:10:25 +08:00
|
|
|
const PSQL = require('cartodb-psql');
|
|
|
|
const copyTo = require('pg-copy-streams').to;
|
|
|
|
const copyFrom = require('pg-copy-streams').from;
|
2018-04-13 03:25:28 +08:00
|
|
|
|
|
|
|
|
2018-05-22 17:54:10 +08:00
|
|
|
function CopyController(metadataBackend, userDatabaseService, userLimitsService, statsClient) {
|
2018-04-13 03:25:28 +08:00
|
|
|
this.metadataBackend = metadataBackend;
|
|
|
|
this.userDatabaseService = userDatabaseService;
|
|
|
|
this.userLimitsService = userLimitsService;
|
2018-05-22 17:54:10 +08:00
|
|
|
this.statsClient = statsClient;
|
2018-04-13 03:25:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CopyController.prototype.route = function (app) {
|
|
|
|
const { base_url } = global.settings;
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-04-13 03:25:28 +08:00
|
|
|
const copyFromMiddlewares = endpointGroup => {
|
|
|
|
return [
|
2018-04-24 19:07:57 +08:00
|
|
|
initializeProfilerMiddleware('copyfrom'),
|
2018-04-13 03:25:28 +08:00
|
|
|
userMiddleware(),
|
|
|
|
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
|
|
|
authorizationMiddleware(this.metadataBackend),
|
|
|
|
connectionParamsMiddleware(this.userDatabaseService),
|
|
|
|
timeoutLimitsMiddleware(this.metadataBackend),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2018-05-22 17:56:50 +08:00
|
|
|
handleCopyFrom(),
|
|
|
|
responseCopyFrom(),
|
2018-04-13 03:25:28 +08:00
|
|
|
errorMiddleware()
|
|
|
|
];
|
|
|
|
};
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-04-24 19:07:57 +08:00
|
|
|
const copyToMiddlewares = endpointGroup => {
|
|
|
|
return [
|
|
|
|
initializeProfilerMiddleware('copyto'),
|
|
|
|
userMiddleware(),
|
|
|
|
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
|
|
|
|
authorizationMiddleware(this.metadataBackend),
|
|
|
|
connectionParamsMiddleware(this.userDatabaseService),
|
|
|
|
timeoutLimitsMiddleware(this.metadataBackend),
|
2018-05-22 21:42:57 +08:00
|
|
|
validateCopyQuery(),
|
2018-05-22 17:54:10 +08:00
|
|
|
handleCopyTo(this.statsClient),
|
2018-04-24 19:07:57 +08:00
|
|
|
errorMiddleware()
|
|
|
|
];
|
|
|
|
};
|
2018-04-13 03:25:28 +08:00
|
|
|
|
2018-05-04 00:50:13 +08:00
|
|
|
app.post(`${base_url}/sql/copyfrom`, copyFromMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_FROM));
|
|
|
|
app.get(`${base_url}/sql/copyto`, copyToMiddlewares(RATE_LIMIT_ENDPOINTS_GROUPS.COPY_TO));
|
2018-04-13 03:25:28 +08:00
|
|
|
};
|
|
|
|
|
2018-05-22 17:54:10 +08:00
|
|
|
function handleCopyTo (statsClient) {
|
|
|
|
return function handleCopyToMiddleware (req, res, next) {
|
2018-05-22 21:42:57 +08:00
|
|
|
const sql = req.query.q;
|
2018-05-22 17:54:10 +08:00
|
|
|
const filename = req.query.filename || 'carto-sql-copyto.dmp';
|
|
|
|
|
|
|
|
let metrics = {
|
|
|
|
size: 0,
|
|
|
|
time: null,
|
2018-05-22 21:42:57 +08:00
|
|
|
format: getFormatFromCopyQuery(sql),
|
2018-05-22 21:29:08 +08:00
|
|
|
total_rows: null
|
2018-05-22 17:54:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
res.header("Content-Disposition", `attachment; filename=${encodeURIComponent(filename)}`);
|
|
|
|
res.header("Content-Type", "application/octet-stream");
|
|
|
|
|
|
|
|
try {
|
2018-05-22 20:15:55 +08:00
|
|
|
const startTime = Date.now();
|
2018-05-22 17:54:10 +08:00
|
|
|
|
|
|
|
// Open pgsql COPY pipe and stream out to HTTP response
|
|
|
|
const pg = new PSQL(res.locals.userDbParams);
|
|
|
|
pg.connect(function (err, client) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
2018-05-22 20:15:55 +08:00
|
|
|
const copyToStream = copyTo(sql);
|
|
|
|
const pgstream = client.query(copyToStream);
|
2018-05-22 17:54:10 +08:00
|
|
|
pgstream
|
|
|
|
.on('error', next)
|
|
|
|
.on('data', data => metrics.size += data.length)
|
|
|
|
.on('end', () => {
|
2018-05-22 20:15:55 +08:00
|
|
|
metrics.time = (Date.now() - startTime) / 1000;
|
2018-05-22 21:29:08 +08:00
|
|
|
metrics.total_rows = copyToStream.rowCount;
|
2018-05-22 17:54:10 +08:00
|
|
|
statsClient.set('copyTo', JSON.stringify(metrics));
|
|
|
|
})
|
|
|
|
.pipe(res);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-04-24 19:07:57 +08:00
|
|
|
|
2018-05-22 17:56:50 +08:00
|
|
|
function handleCopyFrom () {
|
|
|
|
return function handleCopyFromMiddleware (req, res, next) {
|
2018-05-22 21:42:57 +08:00
|
|
|
const sql = req.query.q;
|
2018-05-08 18:52:33 +08:00
|
|
|
|
2018-05-22 17:56:50 +08:00
|
|
|
res.locals.copyFromSize = 0;
|
2018-05-18 23:31:29 +08:00
|
|
|
|
2018-05-22 17:56:50 +08:00
|
|
|
try {
|
2018-05-22 20:15:55 +08:00
|
|
|
const startTime = Date.now();
|
2018-05-18 23:31:29 +08:00
|
|
|
|
2018-05-22 17:56:50 +08:00
|
|
|
// Connect and run the COPY
|
|
|
|
const pg = new PSQL(res.locals.userDbParams);
|
|
|
|
pg.connect(function (err, client) {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let copyFromStream = copyFrom(sql);
|
|
|
|
const pgstream = client.query(copyFromStream);
|
2018-05-22 20:15:55 +08:00
|
|
|
pgstream
|
|
|
|
.on('error', next)
|
|
|
|
.on('end', function () {
|
|
|
|
res.body = {
|
|
|
|
time: (Date.now() - startTime) / 1000,
|
|
|
|
total_rows: copyFromStream.rowCount
|
|
|
|
};
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
2018-05-22 17:56:50 +08:00
|
|
|
|
|
|
|
if (req.get('content-encoding') === 'gzip') {
|
|
|
|
req
|
|
|
|
.pipe(zlib.createGunzip())
|
|
|
|
.on('data', data => res.locals.copyFromSize += data.length)
|
|
|
|
.pipe(pgstream);
|
|
|
|
} else {
|
|
|
|
req
|
|
|
|
.on('data', data => res.locals.copyFromSize += data.length)
|
|
|
|
.pipe(pgstream);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function responseCopyFrom () {
|
|
|
|
return function responseCopyFromMiddleware (req, res, next) {
|
|
|
|
if (!res.body || !res.body.total_rows) {
|
|
|
|
return next(new Error("No rows copied"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.profiler) {
|
|
|
|
const copyFromMetrics = {
|
|
|
|
size: res.locals.copyFromSize, //bytes
|
2018-05-22 21:42:57 +08:00
|
|
|
format: getFormatFromCopyQuery(req.query.q),
|
2018-05-22 17:56:50 +08:00
|
|
|
time: res.body.time, //seconds
|
|
|
|
total_rows: res.body.total_rows,
|
|
|
|
gzip: req.get('content-encoding') === 'gzip'
|
|
|
|
};
|
|
|
|
|
|
|
|
req.profiler.add({ copyFrom: copyFromMetrics });
|
|
|
|
res.header('X-SQLAPI-Profiler', req.profiler.toJSONString());
|
|
|
|
}
|
|
|
|
|
|
|
|
res.send(res.body);
|
|
|
|
};
|
|
|
|
}
|
2018-04-13 03:25:28 +08:00
|
|
|
|
2018-05-22 21:42:57 +08:00
|
|
|
function validateCopyQuery () {
|
|
|
|
return function validateCopyQueryMiddleware (req, res, next) {
|
|
|
|
const sql = req.query.q;
|
|
|
|
|
|
|
|
if (!sql) {
|
|
|
|
next(new Error("SQL is missing"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only accept SQL that starts with 'COPY'
|
|
|
|
if (!sql.toUpperCase().startsWith("COPY ")) {
|
|
|
|
next(new Error("SQL must start with COPY"));
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-04 00:31:49 +08:00
|
|
|
module.exports = CopyController;
|