2016-12-28 23:49:51 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-05 04:00:39 +01:00
|
|
|
import it from 'cafy';
|
2016-12-28 23:49:51 +01: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 09:37:01 +01:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 23:49:51 +01:00
|
|
|
*/
|
2017-03-03 20:28:38 +01:00
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Get 'file_id' parameter
|
2017-03-05 04:00:39 +01:00
|
|
|
const [fileId, fileIdErr] = it(params.file_id).expect.id().required().get();
|
2017-03-03 11:33:14 +01:00
|
|
|
if (fileIdErr) return rej('invalid file_id param');
|
2017-02-11 22:01:31 +01:00
|
|
|
|
2017-03-03 11:33:14 +01:00
|
|
|
// Fetch file
|
2016-12-28 23:49:51 +01:00
|
|
|
const file = await DriveFile
|
|
|
|
.findOne({
|
2017-03-03 11:33:14 +01:00
|
|
|
_id: fileId,
|
2016-12-28 23:49:51 +01:00
|
|
|
user_id: user._id
|
|
|
|
}, {
|
2017-01-17 22:21:22 +01:00
|
|
|
fields: {
|
|
|
|
data: false
|
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (file === null) {
|
|
|
|
return rej('file-not-found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'name' parameter
|
2017-03-05 04:00:39 +01:00
|
|
|
const [name, nameErr] = it(params.name).expect.string().validate(validateFileName).get();
|
2017-03-03 11:33:14 +01:00
|
|
|
if (nameErr) return rej('invalid name param');
|
|
|
|
if (name) file.name = name;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get 'folder_id' parameter
|
2017-03-05 04:00:39 +01:00
|
|
|
const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().get();
|
2017-03-03 11:33:14 +01:00
|
|
|
if (folderIdErr) return rej('invalid folder_id param');
|
|
|
|
|
2017-02-12 17:49:17 +01:00
|
|
|
if (folderId !== undefined) {
|
|
|
|
if (folderId === null) {
|
2016-12-28 23:49:51 +01:00
|
|
|
file.folder_id = null;
|
|
|
|
} else {
|
2017-03-03 11:33:14 +01:00
|
|
|
// Fetch folder
|
2017-02-12 17:49:17 +01:00
|
|
|
const folder = await DriveFolder
|
2016-12-28 23:49:51 +01:00
|
|
|
.findOne({
|
|
|
|
_id: folderId,
|
|
|
|
user_id: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (folder === null) {
|
2017-02-12 17:17:44 +01:00
|
|
|
return rej('folder-not-found');
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
file.folder_id = folder._id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:37:50 +01:00
|
|
|
DriveFile.update(file._id, {
|
2017-02-08 18:20:47 +01:00
|
|
|
$set: {
|
|
|
|
name: file.name,
|
|
|
|
folder_id: file.folder_id
|
|
|
|
}
|
2017-02-08 17:37:50 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Serialize
|
|
|
|
const fileObj = await serialize(file);
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res(fileObj);
|
|
|
|
|
|
|
|
// Publish drive_file_updated event
|
|
|
|
event(user._id, 'drive_file_updated', fileObj);
|
|
|
|
});
|