2017-07-16 00:08:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const db = require('../db');
|
2019-01-26 18:50:51 +08:00
|
|
|
const consts = require('../consts');
|
2017-07-16 00:08:33 +08:00
|
|
|
|
|
|
|
module.exports = server => (quotaRoot, session, callback) => {
|
|
|
|
server.logger.debug(
|
|
|
|
{
|
|
|
|
tnx: 'quota',
|
|
|
|
cid: session.id
|
|
|
|
},
|
|
|
|
'[%s] Requested quota info for "%s"',
|
|
|
|
session.id,
|
|
|
|
quotaRoot
|
|
|
|
);
|
|
|
|
|
|
|
|
if (quotaRoot !== '') {
|
|
|
|
return callback(null, 'NONEXISTENT');
|
|
|
|
}
|
|
|
|
|
2019-01-24 05:38:29 +08:00
|
|
|
db.users.collection('users').findOne(
|
|
|
|
{
|
|
|
|
_id: session.user.id
|
|
|
|
},
|
|
|
|
{
|
2019-01-26 18:50:51 +08:00
|
|
|
maxTimeMS: consts.DB_MAX_TIME_USERS
|
2019-01-24 05:38:29 +08:00
|
|
|
},
|
|
|
|
(err, user) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
if (!user) {
|
|
|
|
return callback(new Error('User data not found'));
|
|
|
|
}
|
2017-07-16 00:08:33 +08:00
|
|
|
|
2022-07-05 16:57:57 +08:00
|
|
|
let getQuota = next => {
|
|
|
|
if (user.quota) {
|
|
|
|
return next(null, user.quota);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!server.options.settingsHandler) {
|
|
|
|
return next(null, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
server.options.settingsHandler
|
|
|
|
.get('const:max:storage')
|
|
|
|
.then(maxStorage => next(null, maxStorage))
|
|
|
|
.catch(err => next(err));
|
|
|
|
};
|
|
|
|
|
|
|
|
getQuota((err, maxStorage) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, {
|
|
|
|
root: '',
|
|
|
|
quota: user.quota || maxStorage || 0,
|
|
|
|
storageUsed: Math.max(user.storageUsed || 0, 0)
|
|
|
|
});
|
2019-01-24 05:38:29 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
2017-07-16 00:08:33 +08:00
|
|
|
};
|