2018-07-07 12:19:00 +02:00
|
|
|
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
|
2018-03-29 13:32:18 +02:00
|
|
|
import DriveFolder from '../../../../../models/drive-folder';
|
|
|
|
import DriveFile, { validateFileName, pack } from '../../../../../models/drive-file';
|
2018-07-07 12:19:00 +02:00
|
|
|
import { publishDriveStream } from '../../../../../stream';
|
2018-06-18 02:54:53 +02:00
|
|
|
import { ILocalUser } from '../../../../../models/user';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
|
|
|
ja: '指定したドライブのファイルの情報を更新します。',
|
|
|
|
en: 'Update specified file of drive.'
|
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
|
|
|
|
kind: 'drive-write'
|
|
|
|
};
|
|
|
|
|
2018-07-05 19:58:29 +02:00
|
|
|
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
2018-03-29 07:48:47 +02:00
|
|
|
// Get 'fileId' parameter
|
2018-05-02 11:06:16 +02:00
|
|
|
const [fileId, fileIdErr] = $.type(ID).get(params.fileId);
|
2018-03-29 07:48:47 +02:00
|
|
|
if (fileIdErr) return rej('invalid fileId 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,
|
2018-03-29 07:48:47 +02:00
|
|
|
'metadata.userId': user._id
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (file === null) {
|
|
|
|
return rej('file-not-found');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'name' parameter
|
2018-07-05 16:36:07 +02:00
|
|
|
const [name, nameErr] = $.str.optional.pipe(validateFileName).get(params.name);
|
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
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
// Get 'folderId' parameter
|
2018-07-05 16:36:07 +02:00
|
|
|
const [folderId, folderIdErr] = $.type(ID).optional.nullable.get(params.folderId);
|
2018-03-29 07:48:47 +02:00
|
|
|
if (folderIdErr) return rej('invalid folderId param');
|
2017-03-03 11:33:14 +01:00
|
|
|
|
2017-02-12 17:49:17 +01:00
|
|
|
if (folderId !== undefined) {
|
|
|
|
if (folderId === null) {
|
2018-03-29 07:48:47 +02:00
|
|
|
file.metadata.folderId = 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,
|
2018-03-29 07:48:47 +02:00
|
|
|
userId: user._id
|
2016-12-28 23:49:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (folder === null) {
|
2017-02-12 17:17:44 +01:00
|
|
|
return rej('folder-not-found');
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
file.metadata.folderId = 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,
|
2018-03-29 07:48:47 +02:00
|
|
|
'metadata.folderId': file.metadata.folderId
|
2017-11-06 08:49:07 +01:00
|
|
|
}
|
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
|
|
|
});
|