2016-12-28 23:49:51 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2016-12-28 23:49:51 +01:00
|
|
|
import DriveFolder from '../../../models/drive-folder';
|
|
|
|
import DriveFile from '../../../models/drive-file';
|
2018-02-02 02:31:17 +01:00
|
|
|
import { validateFileName }, { pack } from '../../../models/drive-file';
|
2017-11-16 15:46:36 +01:00
|
|
|
import { publishDriveStream } from '../../../event';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-08 19:50:09 +01:00
|
|
|
const [fileId, fileIdErr] = $(params.file_id).id().$;
|
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,
|
2017-11-06 08:22:18 +01:00
|
|
|
'metadata.user_id': user._id
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (file === null) {
|
|
|
|
return rej('file-not-found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'name' parameter
|
2017-03-08 19:50:09 +01:00
|
|
|
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
|
2017-03-03 11:33:14 +01:00
|
|
|
if (nameErr) return rej('invalid name param');
|
2017-11-07 01:30:51 +01:00
|
|
|
if (name) file.filename = name;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Get 'folder_id' parameter
|
2017-03-08 19:50:09 +01:00
|
|
|
const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
|
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) {
|
2017-11-06 08:49:07 +01:00
|
|
|
file.metadata.folder_id = null;
|
2016-12-28 23:49:51 +01:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2017-11-06 08:49:07 +01:00
|
|
|
file.metadata.folder_id = folder._id;
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 08:49:07 +01:00
|
|
|
await DriveFile.update(file._id, {
|
|
|
|
$set: {
|
2017-11-07 01:30:51 +01:00
|
|
|
filename: file.filename,
|
2017-11-06 08:49:07 +01:00
|
|
|
'metadata.folder_id': file.metadata.folder_id
|
|
|
|
}
|
2017-02-08 17:37:50 +01:00
|
|
|
});
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Serialize
|
2018-02-02 00:21:30 +01:00
|
|
|
const fileObj = await pack(file);
|
2017-11-06 07:18:45 +01:00
|
|
|
|
2016-12-28 23:49:51 +01:00
|
|
|
// Response
|
|
|
|
res(fileObj);
|
|
|
|
|
2017-11-16 15:46:36 +01:00
|
|
|
// Publish file_updated event
|
|
|
|
publishDriveStream(user._id, 'file_updated', fileObj);
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|