yumechi-no-kuni/src/server/api/endpoints/drive/files/find.ts
2018-07-06 02:58:29 +09:00

28 lines
835 B
TypeScript

import $ from 'cafy'; import ID from '../../../../../cafy-id';
import DriveFile, { pack } from '../../../../../models/drive-file';
import { ILocalUser } from '../../../../../models/user';
/**
* Find a file(s)
*/
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name, nameErr] = $.str.get(params.name);
if (nameErr) return rej('invalid name param');
// Get 'folderId' parameter
const [folderId = null, folderIdErr] = $.type(ID).optional.nullable.get(params.folderId);
if (folderIdErr) return rej('invalid folderId param');
// Issue query
const files = await DriveFile
.find({
filename: name,
'metadata.userId': user._id,
'metadata.folderId': folderId
});
// Serialize
res(await Promise.all(files.map(async file =>
await pack(file))));
});