CartoDB-SQL-API/app/controllers/copy_controller.js

154 lines
4.9 KiB
JavaScript
Raw Normal View History

'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-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-05-04 21:35:23 +08:00
function CopyController(metadataBackend, userDatabaseService, userLimitsService) {
this.metadataBackend = metadataBackend;
this.userDatabaseService = userDatabaseService;
this.userLimitsService = userLimitsService;
}
CopyController.prototype.route = function (app) {
const { base_url } = global.settings;
2018-05-08 18:52:33 +08:00
const copyFromMiddlewares = endpointGroup => {
return [
initializeProfilerMiddleware('copyfrom'),
userMiddleware(),
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
authorizationMiddleware(this.metadataBackend),
connectionParamsMiddleware(this.userDatabaseService),
timeoutLimitsMiddleware(this.metadataBackend),
this.handleCopyFrom.bind(this),
2018-05-08 18:52:33 +08:00
this.responseCopyFrom.bind(this),
errorMiddleware()
];
};
2018-05-08 18:52:33 +08:00
const copyToMiddlewares = endpointGroup => {
return [
initializeProfilerMiddleware('copyto'),
userMiddleware(),
rateLimitsMiddleware(this.userLimitsService, endpointGroup),
authorizationMiddleware(this.metadataBackend),
connectionParamsMiddleware(this.userDatabaseService),
timeoutLimitsMiddleware(this.metadataBackend),
this.handleCopyTo.bind(this),
errorMiddleware()
];
};
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));
};
CopyController.prototype.handleCopyTo = function (req, res, next) {
2018-05-11 20:51:43 +08:00
const { sql } = req.query;
const filename = req.query.filename || 'carto-sql-copyto.dmp';
2018-05-08 19:08:29 +08:00
if (!sql) {
throw new Error("Parameter 'sql' is missing");
}
2018-05-08 18:52:33 +08:00
// Only accept SQL that starts with 'COPY'
if (!sql.toUpperCase().startsWith("COPY ")) {
throw new Error("SQL must start with COPY");
}
2018-05-08 18:52:33 +08:00
try {
// Open pgsql COPY pipe and stream out to HTTP response
2018-05-08 19:10:25 +08:00
const pg = new PSQL(res.locals.userDbParams);
2018-05-08 19:08:29 +08:00
pg.connect(function (err, client) {
if (err) {
return next(err);
}
let copyToStream = copyTo(sql);
const pgstream = client.query(copyToStream);
res.on('error', next);
pgstream.on('error', next);
2018-05-08 19:08:29 +08:00
pgstream.on('end', next);
2018-05-11 19:33:54 +08:00
res.setHeader("Content-Disposition", `attachment; filename=${encodeURIComponent(filename)}`);
res.setHeader("Content-Type", "application/octet-stream");
2018-04-26 16:04:38 +08:00
pgstream.pipe(res);
});
} catch (err) {
next(err);
}
2018-05-08 18:52:33 +08:00
2018-04-26 16:04:38 +08:00
};
2018-05-08 18:52:33 +08:00
CopyController.prototype.handleCopyFrom = function (req, res, next) {
2018-05-11 16:38:33 +08:00
const { sql } = req.query;
2018-05-11 19:33:54 +08:00
if (!sql) {
2018-05-11 16:38:33 +08:00
return next(new Error("Parameter 'sql' is missing, must be in URL or first field in POST"));
}
2018-05-11 16:38:33 +08:00
// Only accept SQL that starts with 'COPY'
if (!sql.toUpperCase().startsWith("COPY ")) {
return next(new Error("SQL must start with COPY"));
}
try {
const start_time = Date.now();
// Connect and run the COPY
const pg = new PSQL(res.locals.userDbParams);
pg.connect(function (err, client) {
if (err) {
return next(err);
2018-05-08 18:52:33 +08:00
}
2018-05-08 19:08:29 +08:00
let copyFromStream = copyFrom(sql);
const pgstream = client.query(copyFromStream);
pgstream.on('error', next);
pgstream.on('end', function () {
const end_time = Date.now();
res.body = {
time: (end_time - start_time) / 1000,
total_rows: copyFromStream.rowCount
};
return next();
});
2018-04-26 16:04:38 +08:00
2018-05-11 19:33:54 +08:00
if (req.get('content-encoding') === 'gzip') {
req.pipe(zlib.createGunzip()).pipe(pgstream);
} else {
req.pipe(pgstream);
}
});
} catch (err) {
next(err);
}
2018-05-08 18:52:33 +08:00
};
CopyController.prototype.responseCopyFrom = function (req, res, next) {
if (!res.body || !res.body.total_rows) {
return next(new Error("No rows copied"));
2018-04-26 16:20:21 +08:00
}
2018-05-08 18:52:33 +08:00
res.send(res.body);
};
2018-05-04 00:31:49 +08:00
module.exports = CopyController;