2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import type { DriveFile } from '@/models/entities/DriveFile.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { DriveFilesRepository } from '@/models/index.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
|
import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-04-17 05:59:41 +02:00
|
|
|
import { ApiError } from '../../../error.js';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['drive'],
|
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2018-07-16 21:36:44 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
kind: 'read:drive',
|
2018-10-22 23:59:52 +02:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Show the properties of a drive file.',
|
|
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'DriveFile',
|
2019-02-23 03:20:58 +01:00
|
|
|
},
|
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
|
noSuchFile: {
|
|
|
|
|
message: 'No such file.',
|
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '067bc436-2718-4795-b0fb-ecbe43949e31',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
|
message: 'Access denied.',
|
|
|
|
|
code: 'ACCESS_DENIED',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '25b73c73-68b1-41d0-bad1-381cfdf6579f',
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-07-16 21:36:44 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
2022-04-03 06:57:26 +02:00
|
|
|
anyOf: [
|
|
|
|
|
{
|
|
|
|
|
properties: {
|
|
|
|
|
fileId: { type: 'string', format: 'misskey:id' },
|
|
|
|
|
},
|
|
|
|
|
required: ['fileId'],
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
properties: {
|
|
|
|
|
url: { type: 'string' },
|
|
|
|
|
},
|
|
|
|
|
required: ['url'],
|
|
|
|
|
},
|
|
|
|
|
],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
|
2022-01-02 18:12:50 +01:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
2019-01-19 01:50:38 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
|
let file: DriveFile | null = null;
|
2019-01-19 01:50:38 +01:00
|
|
|
|
2022-09-17 20:27:08 +02: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-28 23:49:51 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (file == null) {
|
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
|
}
|
2019-04-12 18:43:22 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if ((!me.isAdmin && !me.isModerator) && (file.userId !== me.id)) {
|
|
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await this.driveFileEntityService.pack(file, {
|
|
|
|
|
detail: true,
|
|
|
|
|
withUser: true,
|
|
|
|
|
self: true,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|