wildduck/lib/handlers/on-get-quota.js

65 lines
1.6 KiB
JavaScript
Raw Normal View History

'use strict';
const db = require('../db');
const consts = require('../consts');
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
},
{
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'));
}
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
});
}
);
};