2019-02-05 03:48:08 +01:00
|
|
|
import $ from 'cafy';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { ID } from '../../../../../misc/cafy-id';
|
2019-02-05 06:14:23 +01:00
|
|
|
import { publishDriveStream } from '../../../../../services/stream';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../../define';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { ApiError } from '../../../error';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { DriveFiles, DriveFolders } from '../../../../../models';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': '指定したドライブのファイルの情報を更新します。',
|
|
|
|
'en-US': 'Update specified file of drive.'
|
2018-07-16 21:36:44 +02:00
|
|
|
},
|
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['drive'],
|
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
requireCredential: true,
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
kind: 'write:drive',
|
2018-07-19 19:40:37 +02:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 19:32:24 +01:00
|
|
|
fileId: {
|
|
|
|
validator: $.type(ID),
|
2018-07-19 19:40:37 +02:00
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': '対象のファイルID'
|
2018-07-19 19:40:37 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-07-19 19:40:37 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
folderId: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 19:32:24 +01:00
|
|
|
default: undefined as any,
|
2018-07-19 19:40:37 +02:00
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'フォルダID'
|
2018-07-19 19:40:37 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-07-19 19:40:37 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
name: {
|
2019-04-07 14:50:36 +02:00
|
|
|
validator: $.optional.str.pipe(DriveFiles.validateFileName),
|
2018-11-01 19:32:24 +01:00
|
|
|
default: undefined as any,
|
2018-07-19 19:40:37 +02:00
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'ファイル名',
|
|
|
|
'en-US': 'Name of the file'
|
2018-07-19 19:40:37 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-07-19 19:40:37 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
isSensitive: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 19:32:24 +01:00
|
|
|
default: undefined as any,
|
2018-07-19 19:40:37 +02:00
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'このメディアが「閲覧注意」(NSFW)かどうか',
|
|
|
|
'en-US': 'Whether this media is NSFW'
|
2018-07-19 19:40:37 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
}
|
2019-02-22 03:46:58 +01: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 19:40:37 +02:00
|
|
|
}
|
2018-07-16 21:36:44 +02:00
|
|
|
};
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, user) => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const file = await DriveFiles.findOne(ps.fileId);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (file == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (!user.isAdmin && !user.isModerator && (file.userId !== user.id)) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
2018-12-18 16:57:28 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (ps.name) file.name = ps.name;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (ps.isSensitive !== undefined) file.isSensitive = ps.isSensitive;
|
2017-03-03 11:33:14 +01:00
|
|
|
|
2018-07-19 19:40:37 +02:00
|
|
|
if (ps.folderId !== undefined) {
|
|
|
|
if (ps.folderId === null) {
|
2019-04-07 14:50:36 +02:00
|
|
|
file.folderId = null;
|
2016-12-28 23:49:51 +01:00
|
|
|
} else {
|
2019-04-07 14:50:36 +02:00
|
|
|
const folder = await DriveFolders.findOne({
|
|
|
|
id: ps.folderId,
|
|
|
|
userId: user.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (folder == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchFolder);
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
file.folderId = folder.id;
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
await DriveFiles.update(file.id, {
|
|
|
|
name: file.name,
|
|
|
|
folderId: file.folderId,
|
|
|
|
isSensitive: file.isSensitive
|
2018-09-25 14:09:38 +02:00
|
|
|
});
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const fileObj = await DriveFiles.pack(file, { self: true });
|
2017-11-06 07:18:45 +01:00
|
|
|
|
2018-10-23 00:01:43 +02:00
|
|
|
// Publish fileUpdated event
|
2019-04-07 14:50:36 +02:00
|
|
|
publishDriveStream(user.id, 'fileUpdated', fileObj);
|
2019-02-22 03:46:58 +01:00
|
|
|
|
|
|
|
return fileObj;
|
|
|
|
});
|