summaryrefslogtreecommitdiff
path: root/src/api/endpoints/drive.js
blob: 4df4ac33fac093cba517d615fce1fb2270cacecb (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
'use strict';

/**
 * Module dependencies
 */
import DriveFile from './models/drive-file';

/**
 * Get drive information
 *
 * @param {Object} params
 * @param {Object} user
 * @return {Promise<object>}
 */
module.exports = (params, user) =>
	new Promise(async (res, rej) =>
{
	// Fetch all files to calculate drive usage
	const files = await DriveFile
		.find({ user_id: user._id }, {
			datasize: true,
			_id: false
		})
		.toArray();

	// Calculate drive usage (in byte)
	const usage = files.map(file => file.datasize).reduce((x, y) => x + y, 0);

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