2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-24 15:40:38 +09:00
|
|
|
import type { DriveFilesRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-12 21:02:26 +09:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2023-09-24 15:40:38 +09:00
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
2024-10-07 21:03:31 -04:00
|
|
|
import type { Config } from '@/config.js';
|
2022-07-07 21:06:37 +09:00
|
|
|
import { ApiError } from '../../../error.js';
|
2024-11-22 13:43:06 -05:00
|
|
|
import ms from 'ms';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-07-17 04:36:44 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['drive'],
|
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2018-07-17 04:36:44 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
kind: 'write:drive',
|
2018-07-20 02:40:37 +09:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Update the properties of a drive file.',
|
|
|
|
|
|
2022-02-19 14:05:32 +09:00
|
|
|
errors: {
|
|
|
|
|
invalidFileName: {
|
|
|
|
|
message: 'Invalid file name.',
|
|
|
|
|
code: 'INVALID_FILE_NAME',
|
|
|
|
|
id: '395e7156-f9f0-475e-af89-53c3c23080c2',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-22 11:46:58 +09:00
|
|
|
|
|
|
|
|
noSuchFile: {
|
|
|
|
|
message: 'No such file.',
|
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'e7778c7e-3af9-49cd-9690-6dbc3e6c972d',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
|
message: 'Access denied.',
|
|
|
|
|
code: 'ACCESS_DENIED',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '01a53b27-82fc-445b-a0c1-b558465a8ed2',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
noSuchFolder: {
|
|
|
|
|
message: 'No such folder.',
|
|
|
|
|
code: 'NO_SUCH_FOLDER',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'ea8fb7a5-af77-4a08-b608-c0218176cd73',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
2023-07-08 07:08:16 +09:00
|
|
|
|
2023-05-05 14:18:06 +09:00
|
|
|
restrictedByRole: {
|
|
|
|
|
message: 'This feature is restricted by your role.',
|
|
|
|
|
code: 'RESTRICTED_BY_ROLE',
|
|
|
|
|
id: '7f59dccb-f465-75ab-5cf4-3ce44e3282f7',
|
|
|
|
|
},
|
2024-10-07 21:03:31 -04:00
|
|
|
|
|
|
|
|
commentTooLong: {
|
|
|
|
|
message: 'Cannot upload the file because the comment exceeds the instance limit.',
|
|
|
|
|
code: 'COMMENT_TOO_LONG',
|
2024-10-09 16:17:52 -04:00
|
|
|
id: '333652d9-0826-40f5-a2c3-e2bedcbb9fe5',
|
2024-10-07 21:03:31 -04:00
|
|
|
},
|
2021-03-06 22:34:11 +09:00
|
|
|
},
|
|
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 23:58:30 +09:00
|
|
|
ref: 'DriveFile',
|
|
|
|
|
},
|
2024-11-22 13:43:06 -05:00
|
|
|
|
|
|
|
|
// 100 calls per minute
|
|
|
|
|
limit: {
|
|
|
|
|
duration: 1000 * 60,
|
|
|
|
|
max: 100,
|
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2018-07-17 04:36:44 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
|
folderId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
|
name: { type: 'string' },
|
|
|
|
|
isSensitive: { type: 'boolean' },
|
2024-10-07 21:03:31 -04:00
|
|
|
comment: { type: 'string', nullable: true },
|
2022-02-19 14:05:32 +09:00
|
|
|
},
|
|
|
|
|
required: ['fileId'],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
2024-10-07 21:03:31 -04:00
|
|
|
@Inject(DI.config)
|
|
|
|
|
private config: Config,
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-09-24 15:40:38 +09:00
|
|
|
private driveService: DriveService,
|
2023-01-12 21:02:26 +09:00
|
|
|
private roleService: RoleService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
|
const file = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
|
|
|
|
|
if (file == null) {
|
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2023-01-12 21:02:26 +09:00
|
|
|
if (!await this.roleService.isModerator(me) && (file.userId !== me.id)) {
|
2022-09-18 03:27:08 +09:00
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2024-10-07 21:03:31 -04:00
|
|
|
if (ps.comment && ps.comment.length > this.config.maxAltTextLength) {
|
|
|
|
|
throw new ApiError(meta.errors.commentTooLong);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-24 16:24:13 +09:00
|
|
|
let packedFile;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
packedFile = await this.driveService.updateFile(file, {
|
|
|
|
|
folderId: ps.folderId,
|
|
|
|
|
name: ps.name,
|
|
|
|
|
isSensitive: ps.isSensitive,
|
|
|
|
|
comment: ps.comment,
|
|
|
|
|
}, me);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e instanceof DriveService.InvalidFileNameError) {
|
|
|
|
|
throw new ApiError(meta.errors.invalidFileName);
|
|
|
|
|
} else if (e instanceof DriveService.NoSuchFolderError) {
|
|
|
|
|
throw new ApiError(meta.errors.noSuchFolder);
|
|
|
|
|
} else if (e instanceof DriveService.CannotUnmarkSensitiveError) {
|
|
|
|
|
throw new ApiError(meta.errors.restrictedByRole);
|
|
|
|
|
} else {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return packedFile;
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|