2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DriveFilesRepository, DriveFoldersRepository } from '@/models/index.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-07-07 12:06:37 +00:00
|
|
|
import { ApiError } from '../../../error.js';
|
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'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: true,
|
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
|
|
|
|
2022-06-10 05:25:20 +00:00
|
|
|
description: 'Update the properties of a drive file.',
|
|
|
|
|
2022-02-19 05:05:32 +00:00
|
|
|
errors: {
|
|
|
|
invalidFileName: {
|
|
|
|
message: 'Invalid file name.',
|
|
|
|
code: 'INVALID_FILE_NAME',
|
|
|
|
id: '395e7156-f9f0-475e-af89-53c3c23080c2',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2019-02-22 02:46:58 +00:00
|
|
|
|
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'e7778c7e-3af9-49cd-9690-6dbc3e6c972d',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
message: 'Access denied.',
|
|
|
|
code: 'ACCESS_DENIED',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '01a53b27-82fc-445b-a0c1-b558465a8ed2',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchFolder: {
|
|
|
|
message: 'No such folder.',
|
|
|
|
code: 'NO_SUCH_FOLDER',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'ea8fb7a5-af77-4a08-b608-c0218176cd73',
|
2019-02-22 02:46:58 +00:00
|
|
|
},
|
2021-03-06 13:34:11 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 14:58:30 +00:00
|
|
|
ref: 'DriveFile',
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2018-07-16 19:36:44 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
folderId: { type: 'string', format: 'misskey:id', nullable: true },
|
|
|
|
name: { type: 'string' },
|
|
|
|
isSensitive: { type: 'boolean' },
|
|
|
|
comment: { type: 'string', nullable: true, maxLength: 512 },
|
|
|
|
},
|
|
|
|
required: ['fileId'],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFoldersRepository)
|
|
|
|
private driveFoldersRepository: DriveFoldersRepository,
|
|
|
|
|
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
private globalEventService: GlobalEventService,
|
|
|
|
) {
|
|
|
|
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-28 22:49:51 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if ((!me.isAdmin && !me.isModerator) && (file.userId !== me.id)) {
|
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
}
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.name) file.name = ps.name;
|
|
|
|
if (!this.driveFileEntityService.validateFileName(file.name)) {
|
|
|
|
throw new ApiError(meta.errors.invalidFileName);
|
|
|
|
}
|
2018-12-18 15:57:28 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.comment !== undefined) file.comment = ps.comment;
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.isSensitive !== undefined) file.isSensitive = ps.isSensitive;
|
2021-05-28 00:38:09 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (ps.folderId !== undefined) {
|
|
|
|
if (ps.folderId === null) {
|
|
|
|
file.folderId = null;
|
|
|
|
} else {
|
|
|
|
const folder = await this.driveFoldersRepository.findOneBy({
|
|
|
|
id: ps.folderId,
|
|
|
|
userId: me.id,
|
|
|
|
});
|
2017-03-03 10:33:14 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
if (folder == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchFolder);
|
|
|
|
}
|
2019-04-07 12:50:36 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
file.folderId = folder.id;
|
|
|
|
}
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
await this.driveFilesRepository.update(file.id, {
|
|
|
|
name: file.name,
|
|
|
|
comment: file.comment,
|
|
|
|
folderId: file.folderId,
|
|
|
|
isSensitive: file.isSensitive,
|
|
|
|
});
|
2018-09-25 12:09:38 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
const fileObj = await this.driveFileEntityService.pack(file, { self: true });
|
2017-11-06 06:18:45 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
// Publish fileUpdated event
|
|
|
|
this.globalEventService.publishDriveStream(me.id, 'fileUpdated', fileObj);
|
2019-02-22 02:46:58 +00:00
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
return fileObj;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|