2018-02-01 17:06:01 -06:00
|
|
|
import * as mongo from 'mongodb';
|
2018-06-17 19:54:53 -05:00
|
|
|
const deepcopy = require('deepcopy');
|
2018-03-29 06:32:18 -05:00
|
|
|
import db from '../db/mongodb';
|
2018-10-15 21:38:09 -05:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-02-01 17:06:01 -06:00
|
|
|
import DriveFile from './drive-file';
|
2017-01-16 18:12:33 -06:00
|
|
|
|
2018-04-01 07:53:44 -05:00
|
|
|
const DriveFolder = db.get<IDriveFolder>('driveFolders');
|
2018-10-12 11:00:43 -05:00
|
|
|
DriveFolder.createIndex('userId');
|
2018-02-01 17:06:01 -06:00
|
|
|
export default DriveFolder;
|
|
|
|
|
|
|
|
export type IDriveFolder = {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 00:48:47 -05:00
|
|
|
createdAt: Date;
|
2018-02-01 17:21:30 -06:00
|
|
|
name: string;
|
2018-03-29 00:48:47 -05:00
|
|
|
userId: mongo.ObjectID;
|
|
|
|
parentId: mongo.ObjectID;
|
2018-02-01 17:06:01 -06:00
|
|
|
};
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
export function isValidFolderName(name: string): boolean {
|
|
|
|
return (
|
|
|
|
(name.trim().length > 0) &&
|
|
|
|
(name.length <= 200)
|
|
|
|
);
|
|
|
|
}
|
2018-02-01 17:06:01 -06:00
|
|
|
|
2018-04-11 14:22:06 -05:00
|
|
|
/**
|
|
|
|
* DriveFolderを物理削除します
|
|
|
|
*/
|
|
|
|
export async function deleteDriveFolder(driveFolder: string | mongo.ObjectID | IDriveFolder) {
|
|
|
|
let d: IDriveFolder;
|
|
|
|
|
|
|
|
// Populate
|
2018-10-15 21:38:09 -05:00
|
|
|
if (isObjectId(driveFolder)) {
|
2018-04-11 14:22:06 -05:00
|
|
|
d = await DriveFolder.findOne({
|
|
|
|
_id: driveFolder
|
|
|
|
});
|
|
|
|
} else if (typeof driveFolder === 'string') {
|
|
|
|
d = await DriveFolder.findOne({
|
|
|
|
_id: new mongo.ObjectID(driveFolder)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
d = driveFolder as IDriveFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d == null) return;
|
|
|
|
|
|
|
|
// このDriveFolderに格納されているDriveFileがあればすべてルートに移動
|
|
|
|
await DriveFile.update({
|
|
|
|
'metadata.folderId': d._id
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
'metadata.folderId': null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// このDriveFolderに格納されているDriveFolderがあればすべてルートに移動
|
|
|
|
await DriveFolder.update({
|
|
|
|
parentId: d._id
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
parentId: null
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// このDriveFolderを削除
|
|
|
|
await DriveFolder.remove({
|
|
|
|
_id: d._id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
/**
|
|
|
|
* Pack a drive folder for API response
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
folder: any,
|
|
|
|
options?: {
|
|
|
|
detail: boolean
|
|
|
|
}
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
let _folder: any;
|
|
|
|
|
|
|
|
// Populate the folder if 'folder' is ID
|
2018-10-15 21:38:09 -05:00
|
|
|
if (isObjectId(folder)) {
|
2018-02-01 17:06:01 -06:00
|
|
|
_folder = await DriveFolder.findOne({ _id: folder });
|
|
|
|
} else if (typeof folder === 'string') {
|
|
|
|
_folder = await DriveFolder.findOne({ _id: new mongo.ObjectID(folder) });
|
|
|
|
} else {
|
|
|
|
_folder = deepcopy(folder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename _id to id
|
|
|
|
_folder.id = _folder._id;
|
|
|
|
delete _folder._id;
|
|
|
|
|
|
|
|
if (opts.detail) {
|
|
|
|
const childFoldersCount = await DriveFolder.count({
|
2018-03-29 00:48:47 -05:00
|
|
|
parentId: _folder.id
|
2018-02-01 17:06:01 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
const childFilesCount = await DriveFile.count({
|
2018-03-29 00:48:47 -05:00
|
|
|
'metadata.folderId': _folder.id
|
2018-02-01 17:06:01 -06:00
|
|
|
});
|
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
_folder.foldersCount = childFoldersCount;
|
|
|
|
_folder.filesCount = childFilesCount;
|
2018-02-01 17:06:01 -06:00
|
|
|
}
|
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
if (opts.detail && _folder.parentId) {
|
2018-02-01 17:06:01 -06:00
|
|
|
// Populate parent folder
|
2018-03-29 00:48:47 -05:00
|
|
|
_folder.parent = await pack(_folder.parentId, {
|
2018-02-01 17:06:01 -06:00
|
|
|
detail: true
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_folder);
|
|
|
|
});
|