2016-12-28 16:49:51 -06:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
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 02:37:01 -06:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 16:49:51 -06:00
|
|
|
*/
|
|
|
|
module.exports = (params, user) =>
|
|
|
|
new Promise(async (res, rej) =>
|
|
|
|
{
|
|
|
|
// Get 'file_id' parameter
|
|
|
|
const fileId = params.file_id;
|
|
|
|
if (fileId === undefined || fileId === null) {
|
|
|
|
return rej('file_id is required');
|
|
|
|
}
|
|
|
|
|
2017-02-11 15:01:31 -06:00
|
|
|
// Validate id
|
|
|
|
if (!mongo.ObjectID.isValid(fileId)) {
|
|
|
|
return rej('incorrect file_id');
|
|
|
|
}
|
|
|
|
|
2016-12-28 16:49:51 -06:00
|
|
|
const file = await DriveFile
|
|
|
|
.findOne({
|
|
|
|
_id: new mongo.ObjectID(fileId),
|
|
|
|
user_id: user._id
|
|
|
|
}, {
|
2017-01-17 15:21:22 -06:00
|
|
|
fields: {
|
|
|
|
data: false
|
|
|
|
}
|
2016-12-28 16:49:51 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
if (file === null) {
|
|
|
|
return rej('file-not-found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'name' parameter
|
|
|
|
let name = params.name;
|
|
|
|
if (name) {
|
|
|
|
name = name.trim();
|
|
|
|
if (validateFileName(name)) {
|
|
|
|
file.name = name;
|
|
|
|
} else {
|
|
|
|
return rej('invalid file name');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'folder_id' parameter
|
|
|
|
let folderId = params.folder_id;
|
2017-02-12 10:49:17 -06:00
|
|
|
if (folderId !== undefined) {
|
|
|
|
if (folderId === null) {
|
2016-12-28 16:49:51 -06:00
|
|
|
file.folder_id = null;
|
|
|
|
} else {
|
2017-02-12 10:49:17 -06:00
|
|
|
// Validate id
|
|
|
|
if (!mongo.ObjectID.isValid(folderId)) {
|
|
|
|
return rej('incorrect folder_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
folderId = new mongo.ObjectID(folderId);
|
|
|
|
|
|
|
|
const folder = await DriveFolder
|
2016-12-28 16:49:51 -06:00
|
|
|
.findOne({
|
|
|
|
_id: folderId,
|
|
|
|
user_id: user._id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (folder === null) {
|
2017-02-12 10:17:44 -06:00
|
|
|
return rej('folder-not-found');
|
2016-12-28 16:49:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
file.folder_id = folder._id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 10:37:50 -06:00
|
|
|
DriveFile.update(file._id, {
|
2017-02-08 11:20:47 -06:00
|
|
|
$set: {
|
|
|
|
name: file.name,
|
|
|
|
folder_id: file.folder_id
|
|
|
|
}
|
2017-02-08 10:37:50 -06:00
|
|
|
});
|
2016-12-28 16:49:51 -06:00
|
|
|
|
|
|
|
// Serialize
|
|
|
|
const fileObj = await serialize(file);
|
|
|
|
|
|
|
|
// Response
|
|
|
|
res(fileObj);
|
|
|
|
|
|
|
|
// Publish drive_file_updated event
|
|
|
|
event(user._id, 'drive_file_updated', fileObj);
|
|
|
|
});
|