misskey/src/api/endpoints/drive/files/find.ts
2017-03-04 04:28:38 +09:00

40 lines
930 B
TypeScript

/**
* Module dependencies
*/
import it from '../../../it';
import DriveFile from '../../../models/drive-file';
import serialize from '../../../serializers/drive-file';
/**
* Find a file(s)
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name, nameErr] = it(params.name).expect.string().required().qed();
if (nameErr) return rej('invalid name param');
// Get 'folder_id' parameter
const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed();
if (folderIdErr) return rej('invalid folder_id param');
// Issue query
const files = await DriveFile
.find({
name: name,
user_id: user._id,
folder_id: folderId
}, {
fields: {
data: false
}
});
// Serialize
res(await Promise.all(files.map(async file =>
await serialize(file))));
});