Sharkey/src/api/endpoints/drive.ts

38 lines
611 B
TypeScript
Raw Normal View History

2016-12-29 00:49:51 +02:00
/**
* Module dependencies
*/
2017-02-13 23:42:01 +02:00
import DriveFile from '../models/drive-file';
2016-12-29 00:49:51 +02:00
/**
* Get drive information
*
2017-03-01 10:37:01 +02:00
* @param {any} params
* @param {any} user
* @return {Promise<any>}
2016-12-29 00:49:51 +02:00
*/
2017-03-03 21:28:38 +02:00
module.exports = (params, user) => new Promise(async (res, rej) => {
// Calculate drive usage
const usage = ((await DriveFile
.aggregate([
2017-11-06 08:18:45 +02:00
{ $match: { metadata: { user_id: user._id } } },
2017-03-03 21:28:38 +02:00
{
$project: {
2017-11-06 08:18:45 +02:00
length: true
2017-03-03 21:28:38 +02:00
}
},
{
$group: {
_id: null,
2017-11-06 08:18:45 +02:00
usage: { $sum: '$length' }
2017-03-01 10:37:01 +02:00
}
2017-03-03 21:28:38 +02:00
}
]))[0] || {
usage: 0
}).usage;
2016-12-29 00:49:51 +02:00
2017-03-03 21:28:38 +02:00
res({
capacity: user.drive_capacity,
usage: usage
2016-12-29 00:49:51 +02:00
});
2017-03-03 21:28:38 +02:00
});