summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/drive.ts
blob: eb21853916ed79e85897ac6c7d08e75cc98e6b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
 * Module dependencies
 */
import DriveFile from '../models/drive-file';

/**
 * Get drive information
 *
 * @param {any} params
 * @param {any} user
 * @return {Promise<any>}
 */
module.exports = (params, user) => new Promise(async (res, rej) => {
	// Calculate drive usage
	const usage = ((await DriveFile
		.aggregate([
			{ $match: { 'metadata.userId': user._id } },
			{
				$project: {
					length: true
				}
			},
			{
				$group: {
					_id: null,
					usage: { $sum: '$length' }
				}
			}
		]))[0] || {
			usage: 0
		}).usage;

	res({
		capacity: user.driveCapacity,
		usage: usage
	});
});