2016-12-28 15:49:51 -07:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 11:50:09 -07:00
|
|
|
import $ from 'cafy';
|
2016-12-28 15:49:51 -07:00
|
|
|
import DriveFolder from '../../../models/drive-folder';
|
|
|
|
import DriveFile from '../../../models/drive-file';
|
|
|
|
import { validateFileName } from '../../../models/drive-file';
|
|
|
|
import serialize from '../../../serializers/drive-file';
|
|
|
|
import event from '../../../event';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a file
|
|
|
|
*
|
2017-03-01 01:37:01 -07:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 15:49:51 -07:00
|
|
|
*/
|
2017-03-03 12:28:38 -07:00
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
2016-12-28 15:49:51 -07:00
|
|
|
// Get 'file_id' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [fileId, fileIdErr] = $(params.file_id).id().$;
|
2017-03-03 03:33:14 -07:00
|
|
|
if (fileIdErr) return rej('invalid file_id param');
|
2017-02-11 14:01:31 -07:00
|
|
|
|
2017-11-05 23:18:45 -07:00
|
|
|
console.dir(user)
|
|
|
|
|
2017-03-03 03:33:14 -07:00
|
|
|
// Fetch file
|
2016-12-28 15:49:51 -07:00
|
|
|
const file = await DriveFile
|
|
|
|
.findOne({
|
2017-03-03 03:33:14 -07:00
|
|
|
_id: fileId,
|
2017-11-05 23:18:45 -07:00
|
|
|
metadata: {
|
|
|
|
user_id: user._id
|
2017-01-17 14:21:22 -07:00
|
|
|
}
|
2016-12-28 15:49:51 -07:00
|
|
|
});
|
|
|
|
|
2017-11-05 23:18:45 -07:00
|
|
|
console.dir(file)
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
if (file === null) {
|
|
|
|
return rej('file-not-found');
|
|
|
|
}
|
|
|
|
|
2017-11-05 23:18:45 -07:00
|
|
|
const updateQuery: any = {}
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// Get 'name' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
|
2017-03-03 03:33:14 -07:00
|
|
|
if (nameErr) return rej('invalid name param');
|
2017-11-05 23:18:45 -07:00
|
|
|
if (name) updateQuery.name = name;
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
// Get 'folder_id' parameter
|
2017-03-08 11:50:09 -07:00
|
|
|
const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
|
2017-03-03 03:33:14 -07:00
|
|
|
if (folderIdErr) return rej('invalid folder_id param');
|
|
|
|
|
2017-02-12 09:49:17 -07:00
|
|
|
if (folderId !== undefined) {
|
|
|
|
if (folderId === null) {
|
2017-11-05 23:18:45 -07:00
|
|
|
updateQuery.folder_id = null;
|
2016-12-28 15:49:51 -07:00
|
|
|
} else {
|
2017-03-03 03:33:14 -07:00
|
|
|
// Fetch folder
|
2017-02-12 09:49:17 -07:00
|
|
|
const folder = await DriveFolder
|
2016-12-28 15:49:51 -07:00
|
|
|
.findOne({
|
|
|
|
_id: folderId,
|
|
|
|
user_id: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (folder === null) {
|
2017-02-12 09:17:44 -07:00
|
|
|
return rej('folder-not-found');
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
|
2017-11-05 23:18:45 -07:00
|
|
|
updateQuery.folder_id = folder._id;
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 23:18:45 -07:00
|
|
|
const updated = await DriveFile.update(file._id, {
|
|
|
|
$set: { metadata: updateQuery }
|
2017-02-08 09:37:50 -07:00
|
|
|
});
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-11-05 23:18:45 -07:00
|
|
|
console.dir(updated)
|
|
|
|
|
2016-12-28 15:49:51 -07:00
|
|
|
// Serialize
|
2017-11-05 23:18:45 -07:00
|
|
|
const fileObj = await serialize(updated);
|
|
|
|
|
|
|
|
console.dir(fileObj)
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
// Response
|
|
|
|
res(fileObj);
|
|
|
|
|
|
|
|
// Publish drive_file_updated event
|
|
|
|
event(user._id, 'drive_file_updated', fileObj);
|
|
|
|
});
|