From 434bced6f4aadb8b3291e32f87c4f55454e029d4 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 14 Feb 2017 08:24:18 +0900 Subject: [PATCH] #160 --- src/api/common/add-file-to-drive.ts | 25 ++++++++++++++----------- src/api/endpoints/drive.js | 23 ++++++++++++++--------- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts index bbff93ac7a..714eeb520d 100644 --- a/src/api/common/add-file-to-drive.ts +++ b/src/api/common/add-file-to-drive.ts @@ -79,17 +79,20 @@ export default ( } } - // Fetch all files to calculate drive usage - const files = await DriveFile - .find({ user_id: user._id }, { - fields: { - 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; log(`drive usage is ${usage}`); 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,