summaryrefslogtreecommitdiff
path: root/src/api/endpoints
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-02-14 08:24:18 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-02-14 08:24:18 +0900
commit434bced6f4aadb8b3291e32f87c4f55454e029d4 (patch)
treec3007e2f346467c913f38196ce248c7ed69e48b8 /src/api/endpoints
parent[Test] Better /drive test (diff)
downloadsharkey-434bced6f4aadb8b3291e32f87c4f55454e029d4.tar.gz
sharkey-434bced6f4aadb8b3291e32f87c4f55454e029d4.tar.bz2
sharkey-434bced6f4aadb8b3291e32f87c4f55454e029d4.zip
#160
Diffstat (limited to 'src/api/endpoints')
-rw-r--r--src/api/endpoints/drive.js23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/api/endpoints/drive.js b/src/api/endpoints/drive.js
index 00f9be7807..f0782d8ac7 100644
--- a/src/api/endpoints/drive.js
+++ b/src/api/endpoints/drive.js
@@ -15,15 +15,20 @@ import DriveFile from '../models/drive-file';
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
- });
-
- // Calculate drive usage (in byte)
- const usage = files.map(file => file.datasize).reduce((x, y) => x + y, 0);
+ // Calculate drive usage
+ const usage = ((await DriveFile
+ .aggregate([
+ { $match: { user_id: user._id } },
+ { $project: {
+ datasize: true
+ }},
+ { $group: {
+ _id: null,
+ usage: { $sum: '$datasize' }
+ }}
+ ]))[0] || {
+ usage: 0
+ }).usage;
res({
capacity: user.drive_capacity,