2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-07-23 20:38:56 +08:00
|
|
|
const PSQL = require('cartodb-psql');
|
|
|
|
|
2018-07-23 21:02:20 +08:00
|
|
|
const remainingQuotaQuery = 'SELECT _CDB_UserQuotaInBytes() - CDB_UserDataSize(current_schema()) AS remaining_quota';
|
|
|
|
|
2018-07-23 20:38:56 +08:00
|
|
|
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);
|
|
|
|
}
|
2018-07-23 21:02:20 +08:00
|
|
|
client.query(remainingQuotaQuery, (err, result) => {
|
2018-07-23 20:38:56 +08:00
|
|
|
if(err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
2018-07-23 21:02:20 +08:00
|
|
|
const remainingQuota = result.rows[0].remaining_quota;
|
|
|
|
res.locals.dbRemainingQuota = remainingQuota;
|
2018-07-23 20:38:56 +08:00
|
|
|
done();
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|