2016-12-29 07:49:51 +09:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-02-14 06:42:01 +09:00
|
|
|
import DriveFile from '../models/drive-file';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2017-02-14 06:42:01 +09:00
|
|
|
const files = await DriveFile
|
|
|
|
.find({ user_id: user._id }, {
|
|
|
|
datasize: true,
|
|
|
|
_id: false
|
|
|
|
});
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
// 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
|
|
|
|
});
|
|
|
|
});
|