jormungandr-bite/src/server/api/endpoints/drive/files/update.ts

105 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-07-07 04:19:00 -06:00
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
2018-03-29 05:32:18 -06:00
import DriveFolder from '../../../../../models/drive-folder';
import DriveFile, { validateFileName, pack } from '../../../../../models/drive-file';
2018-07-07 04:19:00 -06:00
import { publishDriveStream } from '../../../../../stream';
2018-06-17 18:54:53 -06:00
import { ILocalUser } from '../../../../../models/user';
2018-07-19 11:40:37 -06:00
import getParams from '../../../get-params';
2016-12-28 15:49:51 -07:00
2018-07-16 13:36:44 -06:00
export const meta = {
desc: {
ja: '指定したドライブのファイルの情報を更新します。',
en: 'Update specified file of drive.'
},
requireCredential: true,
2018-07-19 11:40:37 -06:00
kind: 'drive-write',
params: {
fileId: $.type(ID).note({
desc: {
ja: '対象のファイルID'
}
}),
folderId: $.type(ID).optional.nullable.note({
default: undefined,
desc: {
ja: 'フォルダID'
}
}),
name: $.str.optional.pipe(validateFileName).note({
default: undefined,
desc: {
ja: 'ファイル名',
en: 'Name of the file'
}
}),
isSensitive: $.bool.optional.note({
default: undefined,
desc: {
ja: 'このメディアが「閲覧注意」(NSFW)かどうか',
en: 'Whether this media is NSFW'
}
})
}
2018-07-16 13:36:44 -06:00
};
2018-07-05 11:58:29 -06:00
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
2018-07-19 11:40:37 -06:00
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
2017-02-11 14:01:31 -07:00
2017-03-03 03:33:14 -07:00
// Fetch file
2016-12-28 15:49:51 -07:00
const file = await DriveFile
.findOne({
2018-07-19 11:40:37 -06:00
_id: ps.fileId,
2018-03-28 23:48:47 -06:00
'metadata.userId': user._id
2016-12-28 15:49:51 -07:00
});
if (file === null) {
return rej('file-not-found');
}
2018-07-19 11:40:37 -06:00
if (ps.name) file.filename = ps.name;
2016-12-28 15:49:51 -07:00
2018-07-19 11:40:37 -06:00
if (ps.isSensitive) file.metadata.isSensitive = ps.isSensitive;
2017-03-03 03:33:14 -07:00
2018-07-19 11:40:37 -06:00
if (ps.folderId !== undefined) {
if (ps.folderId === null) {
2018-03-28 23:48:47 -06:00
file.metadata.folderId = null;
2016-12-28 15:49:51 -07:00
} else {
2017-03-03 03:33:14 -07:00
// Fetch folder
2017-02-12 09:49:17 -07:00
const folder = await DriveFolder
2016-12-28 15:49:51 -07:00
.findOne({
2018-07-19 11:40:37 -06:00
_id: ps.folderId,
2018-03-28 23:48:47 -06:00
userId: user._id
2016-12-28 15:49:51 -07:00
});
if (folder === null) {
2017-02-12 09:17:44 -07:00
return rej('folder-not-found');
2016-12-28 15:49:51 -07:00
}
2018-03-28 23:48:47 -06:00
file.metadata.folderId = folder._id;
2016-12-28 15:49:51 -07:00
}
}
await DriveFile.update(file._id, {
$set: {
filename: file.filename,
2018-07-19 11:40:37 -06:00
'metadata.folderId': file.metadata.folderId,
'metadata.isSensitive': file.metadata.isSensitive
}
2017-02-08 09:37:50 -07:00
});
2016-12-28 15:49:51 -07:00
// Serialize
2018-02-01 16:21:30 -07:00
const fileObj = await pack(file);
2017-11-05 23:18:45 -07:00
2016-12-28 15:49:51 -07:00
// Response
res(fileObj);
2017-11-16 07:46:36 -07:00
// Publish file_updated event
publishDriveStream(user._id, 'file_updated', fileObj);
2016-12-28 15:49:51 -07:00
});