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