2016-12-28 22:49:51 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2018-04-24 09:13:06 +00:00
|
|
|
import $ from 'cafy'; import ID from '../../../../../cafy-id';
|
2018-03-29 11:32:18 +00:00
|
|
|
import DriveFolder from '../../../../../models/drive-folder';
|
|
|
|
import DriveFile, { validateFileName, pack } from '../../../../../models/drive-file';
|
2018-04-02 04:33:46 +00:00
|
|
|
import { publishDriveStream } from '../../../../../publishers/stream';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a file
|
|
|
|
*/
|
2017-03-03 19:28:38 +00:00
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
2018-03-29 05:48:47 +00:00
|
|
|
// Get 'fileId' parameter
|
2018-04-27 10:12:15 +00:00
|
|
|
const [fileId, fileIdErr] = $(params.fileId).type(ID).get();
|
2018-03-29 05:48:47 +00:00
|
|
|
if (fileIdErr) return rej('invalid fileId param');
|
2017-02-11 21:01:31 +00:00
|
|
|
|
2017-03-03 10:33:14 +00:00
|
|
|
// Fetch file
|
2016-12-28 22:49:51 +00:00
|
|
|
const file = await DriveFile
|
|
|
|
.findOne({
|
2017-03-03 10:33:14 +00:00
|
|
|
_id: fileId,
|
2018-03-29 05:48:47 +00:00
|
|
|
'metadata.userId': user._id
|
2016-12-28 22:49:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (file === null) {
|
|
|
|
return rej('file-not-found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'name' parameter
|
2018-04-27 10:12:15 +00:00
|
|
|
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).get();
|
2017-03-03 10:33:14 +00:00
|
|
|
if (nameErr) return rej('invalid name param');
|
2017-11-07 00:30:51 +00:00
|
|
|
if (name) file.filename = name;
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-03-29 05:48:47 +00:00
|
|
|
// Get 'folderId' parameter
|
2018-04-27 10:12:15 +00:00
|
|
|
const [folderId, folderIdErr] = $(params.folderId).optional.nullable.type(ID).get();
|
2018-03-29 05:48:47 +00:00
|
|
|
if (folderIdErr) return rej('invalid folderId param');
|
2017-03-03 10:33:14 +00:00
|
|
|
|
2017-02-12 16:49:17 +00:00
|
|
|
if (folderId !== undefined) {
|
|
|
|
if (folderId === null) {
|
2018-03-29 05:48:47 +00:00
|
|
|
file.metadata.folderId = null;
|
2016-12-28 22:49:51 +00:00
|
|
|
} else {
|
2017-03-03 10:33:14 +00:00
|
|
|
// Fetch folder
|
2017-02-12 16:49:17 +00:00
|
|
|
const folder = await DriveFolder
|
2016-12-28 22:49:51 +00:00
|
|
|
.findOne({
|
|
|
|
_id: folderId,
|
2018-03-29 05:48:47 +00:00
|
|
|
userId: user._id
|
2016-12-28 22:49:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (folder === null) {
|
2017-02-12 16:17:44 +00:00
|
|
|
return rej('folder-not-found');
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 05:48:47 +00:00
|
|
|
file.metadata.folderId = folder._id;
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 07:49:07 +00:00
|
|
|
await DriveFile.update(file._id, {
|
|
|
|
$set: {
|
2017-11-07 00:30:51 +00:00
|
|
|
filename: file.filename,
|
2018-03-29 05:48:47 +00:00
|
|
|
'metadata.folderId': file.metadata.folderId
|
2017-11-06 07:49:07 +00:00
|
|
|
}
|
2017-02-08 16:37:50 +00:00
|
|
|
});
|
2016-12-28 22:49:51 +00:00
|
|
|
|
|
|
|
// Serialize
|
2018-02-01 23:21:30 +00:00
|
|
|
const fileObj = await pack(file);
|
2017-11-06 06:18:45 +00:00
|
|
|
|
2016-12-28 22:49:51 +00:00
|
|
|
// Response
|
|
|
|
res(fileObj);
|
|
|
|
|
2017-11-16 14:46:36 +00:00
|
|
|
// Publish file_updated event
|
|
|
|
publishDriveStream(user._id, 'file_updated', fileObj);
|
2016-12-28 22:49:51 +00:00
|
|
|
});
|