2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-20 11:33:36 +09:00
|
|
|
import type { MiDriveFile } from '@/models/DriveFile.js';
|
2023-09-15 14:28:29 +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 { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-12 21:02:26 +09:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2022-04-17 12:59:41 +09:00
|
|
|
import { ApiError } from '../../../error.js';
|
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: 'read:drive',
|
2018-10-23 06:59:52 +09:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Show the properties of a drive file.',
|
|
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'DriveFile',
|
2019-02-23 11:20:58 +09:00
|
|
|
},
|
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
|
|
|
|
noSuchFile: {
|
|
|
|
|
message: 'No such file.',
|
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '067bc436-2718-4795-b0fb-ecbe43949e31',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
|
message: 'Access denied.',
|
|
|
|
|
code: 'ACCESS_DENIED',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '25b73c73-68b1-41d0-bad1-381cfdf6579f',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
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',
|
2023-02-26 11:28:05 +09:00
|
|
|
properties: {
|
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
|
url: { type: 'string' },
|
|
|
|
|
},
|
2022-04-03 06:57:26 +02:00
|
|
|
anyOf: [
|
2023-02-26 11:28:05 +09:00
|
|
|
{ required: ['fileId'] },
|
|
|
|
|
{ required: ['url'] },
|
2022-04-03 06:57:26 +02:00
|
|
|
],
|
2022-02-19 14:05:32 +09:00
|
|
|
} 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,
|
2019-01-19 09:50:38 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
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) => {
|
2023-08-16 17:51:28 +09:00
|
|
|
let file: MiDriveFile | null = null;
|
2019-01-19 09:50:38 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (ps.fileId) {
|
|
|
|
|
file = await this.driveFilesRepository.findOneBy({ id: ps.fileId });
|
|
|
|
|
} else if (ps.url) {
|
|
|
|
|
file = await this.driveFilesRepository.findOne({
|
|
|
|
|
where: [{
|
|
|
|
|
url: ps.url,
|
|
|
|
|
}, {
|
|
|
|
|
webpublicUrl: ps.url,
|
|
|
|
|
}, {
|
|
|
|
|
thumbnailUrl: ps.url,
|
|
|
|
|
}],
|
|
|
|
|
});
|
|
|
|
|
}
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (file == null) {
|
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
|
}
|
2019-04-13 01:43:22 +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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await this.driveFileEntityService.pack(file, {
|
|
|
|
|
detail: true,
|
|
|
|
|
withUser: true,
|
|
|
|
|
self: true,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|