2019-02-05 04:48:08 +02:00
|
|
|
import $ from 'cafy';
|
|
|
|
import ID, { transform } from '../../../../../misc/cafy-id';
|
2018-03-29 14:32:18 +03:00
|
|
|
import DriveFolder from '../../../../../models/drive-folder';
|
|
|
|
import DriveFile, { validateFileName, pack } from '../../../../../models/drive-file';
|
2019-02-05 07:14:23 +02:00
|
|
|
import { publishDriveStream } from '../../../../../services/stream';
|
2018-11-02 06:47:44 +02:00
|
|
|
import define from '../../../define';
|
2018-09-25 15:09:38 +03:00
|
|
|
import Note from '../../../../../models/note';
|
2019-02-22 04:46:58 +02:00
|
|
|
import { ApiError } from '../../../error';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2018-08-29 00:59:43 +03:00
|
|
|
'ja-JP': '指定したドライブのファイルの情報を更新します。',
|
|
|
|
'en-US': 'Update specified file of drive.'
|
2018-07-16 22:36:44 +03:00
|
|
|
},
|
|
|
|
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['drive'],
|
|
|
|
|
2018-07-16 22:36:44 +03:00
|
|
|
requireCredential: true,
|
|
|
|
|
2018-07-19 20:40:37 +03:00
|
|
|
kind: 'drive-write',
|
|
|
|
|
|
|
|
params: {
|
2018-11-01 20:32:24 +02:00
|
|
|
fileId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
transform: transform,
|
2018-07-19 20:40:37 +03:00
|
|
|
desc: {
|
2018-08-29 00:59:43 +03:00
|
|
|
'ja-JP': '対象のファイルID'
|
2018-07-19 20:40:37 +03:00
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-19 20:40:37 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
folderId: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 20:32:24 +02:00
|
|
|
transform: transform,
|
|
|
|
default: undefined as any,
|
2018-07-19 20:40:37 +03:00
|
|
|
desc: {
|
2018-08-29 00:59:43 +03:00
|
|
|
'ja-JP': 'フォルダID'
|
2018-07-19 20:40:37 +03:00
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-19 20:40:37 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
name: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.str.pipe(validateFileName),
|
2018-11-01 20:32:24 +02:00
|
|
|
default: undefined as any,
|
2018-07-19 20:40:37 +03:00
|
|
|
desc: {
|
2018-08-29 00:59:43 +03:00
|
|
|
'ja-JP': 'ファイル名',
|
|
|
|
'en-US': 'Name of the file'
|
2018-07-19 20:40:37 +03:00
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
},
|
2018-07-19 20:40:37 +03:00
|
|
|
|
2018-11-01 20:32:24 +02:00
|
|
|
isSensitive: {
|
2019-02-13 09:33:07 +02:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 20:32:24 +02:00
|
|
|
default: undefined as any,
|
2018-07-19 20:40:37 +03:00
|
|
|
desc: {
|
2018-08-29 00:59:43 +03:00
|
|
|
'ja-JP': 'このメディアが「閲覧注意」(NSFW)かどうか',
|
|
|
|
'en-US': 'Whether this media is NSFW'
|
2018-07-19 20:40:37 +03:00
|
|
|
}
|
2018-11-01 20:32:24 +02:00
|
|
|
}
|
2019-02-22 04:46:58 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
|
|
|
id: 'e7778c7e-3af9-49cd-9690-6dbc3e6c972d'
|
|
|
|
},
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
message: 'Access denied.',
|
|
|
|
code: 'ACCESS_DENIED',
|
|
|
|
id: '01a53b27-82fc-445b-a0c1-b558465a8ed2'
|
|
|
|
},
|
|
|
|
|
|
|
|
noSuchFolder: {
|
|
|
|
message: 'No such folder.',
|
|
|
|
code: 'NO_SUCH_FOLDER',
|
|
|
|
id: 'ea8fb7a5-af77-4a08-b608-c0218176cd73'
|
|
|
|
},
|
2018-07-19 20:40:37 +03:00
|
|
|
}
|
2018-07-16 22:36:44 +03:00
|
|
|
};
|
|
|
|
|
2019-02-22 04:46:58 +02:00
|
|
|
export default define(meta, async (ps, user) => {
|
2017-03-03 12:33:14 +02:00
|
|
|
// Fetch file
|
2016-12-29 00:49:51 +02:00
|
|
|
const file = await DriveFile
|
|
|
|
.findOne({
|
2018-12-18 17:57:28 +02:00
|
|
|
_id: ps.fileId
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (file === null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
|
2018-12-18 17:57:28 +02:00
|
|
|
if (!user.isAdmin && !user.isModerator && !file.metadata.userId.equals(user._id)) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
2018-12-18 17:57:28 +02:00
|
|
|
}
|
|
|
|
|
2018-07-19 20:40:37 +03:00
|
|
|
if (ps.name) file.filename = ps.name;
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-08-18 21:36:51 +03:00
|
|
|
if (ps.isSensitive !== undefined) file.metadata.isSensitive = ps.isSensitive;
|
2017-03-03 12:33:14 +02:00
|
|
|
|
2018-07-19 20:40:37 +03:00
|
|
|
if (ps.folderId !== undefined) {
|
|
|
|
if (ps.folderId === null) {
|
2018-03-29 08:48:47 +03:00
|
|
|
file.metadata.folderId = null;
|
2016-12-29 00:49:51 +02:00
|
|
|
} else {
|
2017-03-03 12:33:14 +02:00
|
|
|
// Fetch folder
|
2017-02-12 18:49:17 +02:00
|
|
|
const folder = await DriveFolder
|
2016-12-29 00:49:51 +02:00
|
|
|
.findOne({
|
2018-07-19 20:40:37 +03:00
|
|
|
_id: ps.folderId,
|
2018-03-29 08:48:47 +03:00
|
|
|
userId: user._id
|
2016-12-29 00:49:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (folder === null) {
|
2019-02-22 04:46:58 +02:00
|
|
|
throw new ApiError(meta.errors.noSuchFolder);
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 08:48:47 +03:00
|
|
|
file.metadata.folderId = folder._id;
|
2016-12-29 00:49:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-06 09:49:07 +02:00
|
|
|
await DriveFile.update(file._id, {
|
|
|
|
$set: {
|
2017-11-07 02:30:51 +02:00
|
|
|
filename: file.filename,
|
2018-07-19 20:40:37 +03:00
|
|
|
'metadata.folderId': file.metadata.folderId,
|
|
|
|
'metadata.isSensitive': file.metadata.isSensitive
|
2017-11-06 09:49:07 +02:00
|
|
|
}
|
2017-02-08 18:37:50 +02:00
|
|
|
});
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2018-09-25 15:09:38 +03:00
|
|
|
// ドライブのファイルが非正規化されているドキュメントも更新
|
|
|
|
Note.find({
|
|
|
|
'_files._id': file._id
|
|
|
|
}).then(notes => {
|
2018-12-11 13:36:55 +02:00
|
|
|
for (const note of notes) {
|
2018-09-25 15:09:38 +03:00
|
|
|
note._files[note._files.findIndex(f => f._id.equals(file._id))] = file;
|
2018-10-14 04:06:10 +03:00
|
|
|
Note.update({ _id: note._id }, {
|
|
|
|
$set: {
|
|
|
|
_files: note._files
|
|
|
|
}
|
2018-09-25 15:09:38 +03:00
|
|
|
});
|
2018-12-11 13:36:55 +02:00
|
|
|
}
|
2018-09-25 15:09:38 +03:00
|
|
|
});
|
|
|
|
|
2018-11-25 21:25:48 +02:00
|
|
|
const fileObj = await pack(file, { self: true });
|
2017-11-06 08:18:45 +02:00
|
|
|
|
2018-10-23 01:01:43 +03:00
|
|
|
// Publish fileUpdated event
|
|
|
|
publishDriveStream(user._id, 'fileUpdated', fileObj);
|
2019-02-22 04:46:58 +02:00
|
|
|
|
|
|
|
return fileObj;
|
|
|
|
});
|