jormungandr-bite/src/api/endpoints/drive/files/show.ts
2017-11-06 16:09:51 +09:00

39 lines
699 B
TypeScript

/**
* Module dependencies
*/
import $ from 'cafy';
import DriveFile from '../../../models/drive-file';
import serialize from '../../../serializers/drive-file';
/**
* Show a file
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = async (params, user) => {
// Get 'file_id' parameter
const [fileId, fileIdErr] = $(params.file_id).id().$;
if (fileIdErr) throw 'invalid file_id param';
// Fetch file
const file = await DriveFile
.findOne({
_id: fileId,
metadata: {
user_id: user._id
}
});
if (file === null) {
throw 'file-not-found';
}
// Serialize
const _file = await serialize(file, {
detail: true
});
return _file
};