2016-12-29 07:49:51 +09:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import DriveFolder from '../models/drive-folder';
|
2017-03-19 00:02:40 +09:00
|
|
|
import DriveFile from '../models/drive-file';
|
2017-01-03 06:03:19 +09:00
|
|
|
import deepcopy = require('deepcopy');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize a drive folder
|
|
|
|
*
|
2017-03-01 17:37:01 +09:00
|
|
|
* @param {any} folder
|
|
|
|
* @param {any} options?
|
|
|
|
* @return {Promise<any>}
|
2016-12-29 07:49:51 +09:00
|
|
|
*/
|
|
|
|
const self = (
|
|
|
|
folder: any,
|
|
|
|
options?: {
|
2017-02-16 16:55:01 +09:00
|
|
|
detail: boolean
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
2017-03-01 17:37:01 +09:00
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
2017-02-16 16:55:01 +09:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false
|
|
|
|
}, options);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
let _folder: any;
|
|
|
|
|
|
|
|
// Populate the folder if 'folder' is ID
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(folder)) {
|
2017-03-01 17:37:01 +09:00
|
|
|
_folder = await DriveFolder.findOne({ _id: folder });
|
2016-12-29 07:49:51 +09:00
|
|
|
} else if (typeof folder === 'string') {
|
2017-03-01 17:37:01 +09:00
|
|
|
_folder = await DriveFolder.findOne({ _id: new mongo.ObjectID(folder) });
|
2016-12-29 07:49:51 +09:00
|
|
|
} else {
|
|
|
|
_folder = deepcopy(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename _id to id
|
|
|
|
_folder.id = _folder._id;
|
|
|
|
delete _folder._id;
|
|
|
|
|
2017-03-19 00:02:40 +09:00
|
|
|
if (opts.detail) {
|
|
|
|
const childFoldersCount = await DriveFolder.count({
|
|
|
|
parent_id: _folder.id
|
|
|
|
});
|
|
|
|
|
|
|
|
const childFilesCount = await DriveFile.count({
|
2017-11-06 16:22:18 +09:00
|
|
|
'metadata.folder_id': _folder.id
|
2017-03-19 00:02:40 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
_folder.folders_count = childFoldersCount;
|
|
|
|
_folder.files_count = childFilesCount;
|
|
|
|
}
|
|
|
|
|
2017-02-16 16:55:01 +09:00
|
|
|
if (opts.detail && _folder.parent_id) {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Populate parent folder
|
|
|
|
_folder.parent = await self(_folder.parent_id, {
|
2017-02-16 16:55:01 +09:00
|
|
|
detail: true
|
2016-12-29 07:49:51 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_folder);
|
|
|
|
});
|
|
|
|
|
|
|
|
export default self;
|