mizzkey/src/server/api/endpoints/drive/folders/delete.ts

77 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-02-05 03:48:08 +01:00
import $ from 'cafy';
import ID, { transform } from '../../../../../misc/cafy-id';
2018-10-24 22:33:40 +02:00
import DriveFolder from '../../../../../models/drive-folder';
2018-11-02 05:47:44 +01:00
import define from '../../../define';
2019-02-05 06:14:23 +01:00
import { publishDriveStream } from '../../../../../services/stream';
2018-10-24 11:37:58 +02:00
import DriveFile from '../../../../../models/drive-file';
import { ApiError } from '../../../error';
2018-10-24 11:37:58 +02:00
export const meta = {
stability: 'stable',
desc: {
'ja-JP': '指定したドライブのフォルダを削除します。',
'en-US': 'Delete specified folder of drive.'
},
tags: ['drive'],
2018-10-24 11:37:58 +02:00
requireCredential: true,
kind: 'drive-write',
params: {
2018-11-01 19:32:24 +01:00
folderId: {
validator: $.type(ID),
transform: transform,
2018-10-24 11:37:58 +02:00
desc: {
'ja-JP': '対象のフォルダID',
'en-US': 'Target folder ID'
}
2018-11-01 19:32:24 +01:00
}
},
errors: {
noSuchFolder: {
message: 'No such folder.',
code: 'NO_SUCH_FOLDER',
id: '1069098f-c281-440f-b085-f9932edbe091'
},
hasChildFilesOrFolders: {
message: 'This folder has child files or folders.',
code: 'HAS_CHILD_FILES_OR_FOLDERS',
id: 'b0fc8a17-963c-405d-bfbc-859a487295e1'
},
2018-10-24 11:37:58 +02:00
}
};
export default define(meta, async (ps, user) => {
2018-10-24 11:37:58 +02:00
// Get folder
const folder = await DriveFolder
.findOne({
_id: ps.folderId,
userId: user._id
});
if (folder === null) {
throw new ApiError(meta.errors.noSuchFolder);
2018-10-24 11:37:58 +02:00
}
const [childFoldersCount, childFilesCount] = await Promise.all([
DriveFolder.count({ parentId: folder._id }),
2018-10-26 08:06:27 +02:00
DriveFile.count({ 'metadata.folderId': folder._id })
2018-10-24 11:37:58 +02:00
]);
if (childFoldersCount !== 0 || childFilesCount !== 0) {
throw new ApiError(meta.errors.hasChildFilesOrFolders);
2018-10-24 11:37:58 +02:00
}
await DriveFolder.remove({ _id: folder._id });
// Publish folderCreated event
publishDriveStream(user._id, 'folderDeleted', folder._id);
return;
});