egirlskey/src/server/api/endpoints/drive/files/update.ts

126 lines
2.8 KiB
TypeScript
Raw Normal View History

2018-11-01 18:32:24 +00:00
import $ from 'cafy'; import ID, { transform } from '../../../../../misc/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-07-07 10:19:00 +00:00
import { publishDriveStream } from '../../../../../stream';
2018-06-18 00:54:53 +00:00
import { ILocalUser } from '../../../../../models/user';
2018-07-19 17:40:37 +00:00
import getParams from '../../../get-params';
2018-09-25 12:09:38 +00:00
import Note from '../../../../../models/note';
2016-12-28 22:49:51 +00:00
2018-07-16 19:36:44 +00:00
export const meta = {
desc: {
2018-08-28 21:59:43 +00:00
'ja-JP': '指定したドライブのファイルの情報を更新します。',
'en-US': 'Update specified file of drive.'
2018-07-16 19:36:44 +00:00
},
requireCredential: true,
2018-07-19 17:40:37 +00:00
kind: 'drive-write',
params: {
2018-11-01 18:32:24 +00:00
fileId: {
validator: $.type(ID),
transform: transform,
2018-07-19 17:40:37 +00:00
desc: {
2018-08-28 21:59:43 +00:00
'ja-JP': '対象のファイルID'
2018-07-19 17:40:37 +00:00
}
2018-11-01 18:32:24 +00:00
},
2018-07-19 17:40:37 +00:00
2018-11-01 18:32:24 +00:00
folderId: {
validator: $.type(ID).optional.nullable,
transform: transform,
default: undefined as any,
2018-07-19 17:40:37 +00:00
desc: {
2018-08-28 21:59:43 +00:00
'ja-JP': 'フォルダID'
2018-07-19 17:40:37 +00:00
}
2018-11-01 18:32:24 +00:00
},
2018-07-19 17:40:37 +00:00
2018-11-01 18:32:24 +00:00
name: {
validator: $.str.optional.pipe(validateFileName),
default: undefined as any,
2018-07-19 17:40:37 +00:00
desc: {
2018-08-28 21:59:43 +00:00
'ja-JP': 'ファイル名',
'en-US': 'Name of the file'
2018-07-19 17:40:37 +00:00
}
2018-11-01 18:32:24 +00:00
},
2018-07-19 17:40:37 +00:00
2018-11-01 18:32:24 +00:00
isSensitive: {
validator: $.bool.optional,
default: undefined as any,
2018-07-19 17:40:37 +00:00
desc: {
2018-08-28 21:59:43 +00:00
'ja-JP': 'このメディアが「閲覧注意」(NSFW)かどうか',
'en-US': 'Whether this media is NSFW'
2018-07-19 17:40:37 +00:00
}
2018-11-01 18:32:24 +00:00
}
2018-07-19 17:40:37 +00:00
}
2018-07-16 19:36:44 +00:00
};
2018-07-05 17:58:29 +00:00
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
2018-07-19 17:40:37 +00:00
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
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({
2018-07-19 17:40:37 +00:00
_id: ps.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');
}
2018-07-19 17:40:37 +00:00
if (ps.name) file.filename = ps.name;
2016-12-28 22:49:51 +00:00
2018-08-18 18:36:51 +00:00
if (ps.isSensitive !== undefined) file.metadata.isSensitive = ps.isSensitive;
2017-03-03 10:33:14 +00:00
2018-07-19 17:40:37 +00:00
if (ps.folderId !== undefined) {
if (ps.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({
2018-07-19 17:40:37 +00:00
_id: ps.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
}
}
await DriveFile.update(file._id, {
$set: {
filename: file.filename,
2018-07-19 17:40:37 +00:00
'metadata.folderId': file.metadata.folderId,
'metadata.isSensitive': file.metadata.isSensitive
}
2017-02-08 16:37:50 +00:00
});
2016-12-28 22:49:51 +00:00
2018-09-25 12:09:38 +00:00
// ドライブのファイルが非正規化されているドキュメントも更新
Note.find({
'_files._id': file._id
}).then(notes => {
notes.forEach(note => {
note._files[note._files.findIndex(f => f._id.equals(file._id))] = file;
2018-10-14 01:06:10 +00:00
Note.update({ _id: note._id }, {
$set: {
_files: note._files
}
2018-09-25 12:09:38 +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);
2018-10-22 22:01:43 +00:00
// Publish fileUpdated event
publishDriveStream(user._id, 'fileUpdated', fileObj);
2016-12-28 22:49:51 +00:00
});