2019-02-05 02:48:08 +00:00
|
|
|
import $ from 'cafy';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import { publishDriveStream } from '@/services/stream';
|
|
|
|
import define from '../../../define';
|
|
|
|
import { ApiError } from '../../../error';
|
|
|
|
import { DriveFiles, DriveFolders } from '@/models/index';
|
2021-09-29 16:44:22 +00:00
|
|
|
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits';
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2018-07-16 19:36:44 +00:00
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['drive'],
|
|
|
|
|
2020-02-15 12:33:32 +00:00
|
|
|
requireCredential: true as const,
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'write:drive',
|
2018-07-19 17:40:37 +00:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 18:32:24 +00:00
|
|
|
fileId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
},
|
2018-07-19 17:40:37 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
folderId: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 18:32:24 +00:00
|
|
|
default: undefined as any,
|
|
|
|
},
|
2018-07-19 17:40:37 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
name: {
|
2019-04-07 12:50:36 +00:00
|
|
|
validator: $.optional.str.pipe(DriveFiles.validateFileName),
|
2018-11-01 18:32:24 +00:00
|
|
|
default: undefined as any,
|
|
|
|
},
|
2018-07-19 17:40:37 +00:00
|
|
|
|
2018-11-01 18:32:24 +00:00
|
|
|
isSensitive: {
|
2019-02-13 07:33:07 +00:00
|
|
|
validator: $.optional.bool,
|
2018-11-01 18:32:24 +00:00
|
|
|
default: undefined as any,
|
2021-05-28 00:38:09 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
comment: {
|
2021-09-29 16:44:22 +00:00
|
|
|
validator: $.optional.nullable.str.max(DB_MAX_IMAGE_COMMENT_LENGTH),
|
2021-05-28 00:38:09 +00:00
|
|
|
default: undefined as any,
|
2018-11-01 18:32:24 +00:00
|
|
|
}
|
2019-02-22 02:46:58 +00: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'
|
|
|
|
},
|
2021-03-06 13:34:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
ref: 'DriveFile'
|
2018-07-19 17:40:37 +00:00
|
|
|
}
|
2018-07-16 19:36:44 +00:00
|
|
|
};
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
export default define(meta, async (ps, user) => {
|
2019-04-07 12:50:36 +00:00
|
|
|
const file = await DriveFiles.findOne(ps.fileId);
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (file == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (!user.isAdmin && !user.isModerator && (file.userId !== user.id)) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
2018-12-18 15:57:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (ps.name) file.name = ps.name;
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2021-05-28 00:38:09 +00:00
|
|
|
if (ps.comment !== undefined) file.comment = ps.comment;
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (ps.isSensitive !== undefined) file.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) {
|
2019-04-07 12:50:36 +00:00
|
|
|
file.folderId = null;
|
2016-12-28 22:49:51 +00:00
|
|
|
} else {
|
2019-04-07 12:50:36 +00:00
|
|
|
const folder = await DriveFolders.findOne({
|
|
|
|
id: ps.folderId,
|
|
|
|
userId: user.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (folder == null) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.noSuchFolder);
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
file.folderId = folder.id;
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
await DriveFiles.update(file.id, {
|
|
|
|
name: file.name,
|
2021-05-28 00:38:09 +00:00
|
|
|
comment: file.comment,
|
2019-04-07 12:50:36 +00:00
|
|
|
folderId: file.folderId,
|
|
|
|
isSensitive: file.isSensitive
|
2018-09-25 12:09:38 +00:00
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const fileObj = await DriveFiles.pack(file, { self: true });
|
2017-11-06 06:18:45 +00:00
|
|
|
|
2018-10-22 22:01:43 +00:00
|
|
|
// Publish fileUpdated event
|
2019-04-07 12:50:36 +00:00
|
|
|
publishDriveStream(user.id, 'fileUpdated', fileObj);
|
2019-02-22 02:46:58 +00:00
|
|
|
|
|
|
|
return fileObj;
|
|
|
|
});
|