1
0
mirror of https://github.com/MisskeyIO/misskey synced 2025-01-09 19:33:49 +09:00
MisskeyIO/src/api/endpoints/drive/files/update.ts
2017-11-06 15:18:45 +09:00

87 lines
1.8 KiB
TypeScript

/**
* Module dependencies
*/
import $ from 'cafy';
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
*
* @param {any} params
* @param {any} user
* @return {Promise<any>}
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'file_id' parameter
const [fileId, fileIdErr] = $(params.file_id).id().$;
if (fileIdErr) return rej('invalid file_id param');
console.dir(user)
// Fetch file
const file = await DriveFile
.findOne({
_id: fileId,
metadata: {
user_id: user._id
}
});
console.dir(file)
if (file === null) {
return rej('file-not-found');
}
const updateQuery: any = {}
// Get 'name' parameter
const [name, nameErr] = $(params.name).optional.string().pipe(validateFileName).$;
if (nameErr) return rej('invalid name param');
if (name) updateQuery.name = name;
// Get 'folder_id' parameter
const [folderId, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) return rej('invalid folder_id param');
if (folderId !== undefined) {
if (folderId === null) {
updateQuery.folder_id = null;
} else {
// Fetch folder
const folder = await DriveFolder
.findOne({
_id: folderId,
user_id: user._id
});
if (folder === null) {
return rej('folder-not-found');
}
updateQuery.folder_id = folder._id;
}
}
const updated = await DriveFile.update(file._id, {
$set: { metadata: updateQuery }
});
console.dir(updated)
// Serialize
const fileObj = await serialize(updated);
console.dir(fileObj)
// Response
res(fileObj);
// Publish drive_file_updated event
event(user._id, 'drive_file_updated', fileObj);
});