First version of the dbQuotaMiddleware

This commit is contained in:
Rafa de la Torre 2018-07-23 14:38:56 +02:00
parent ea46db5923
commit 7e54912ee4
2 changed files with 23 additions and 0 deletions

View File

@ -6,6 +6,7 @@ const authorizationMiddleware = require('../middlewares/authorization');
const connectionParamsMiddleware = require('../middlewares/connection-params');
const { initializeProfilerMiddleware } = require('../middlewares/profiler');
const rateLimitsMiddleware = require('../middlewares/rate-limit');
const dbQuotaMiddleware = require('../middlewares/db-quota');
const { RATE_LIMIT_ENDPOINTS_GROUPS } = rateLimitsMiddleware;
const errorHandlerFactory = require('../services/error_handler_factory');
const StreamCopy = require('../services/stream_copy');
@ -31,6 +32,7 @@ CopyController.prototype.route = function (app) {
authorizationMiddleware(this.metadataBackend),
connectionParamsMiddleware(this.userDatabaseService),
validateCopyQuery(),
dbQuotaMiddleware(),
handleCopyFrom(this.logger),
errorHandler(),
errorMiddleware()

View File

@ -0,0 +1,21 @@
const PSQL = require('cartodb-psql');
module.exports = function dbQuota () {
return function dbQuotaMiddleware (req, res, next) {
const { userDbParams } = res.locals;
const pg = new PSQL(userDbParams);
pg.connect((err, client, done) => {
if (err) {
return next(err);
}
client.query('SELECT _CDB_UserQuotaInBytes() - CDB_UserDataSize(current_schema())', (err, res) => {
if(err) {
return next(err);
}
console.warn(res);
done();
next();
});
});
};
};