2021-11-12 19:47:04 +09:00
|
|
|
import ms from 'ms';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { DriveFilesRepository } from '@/models/index.js';
|
2022-07-02 15:12:11 +09:00
|
|
|
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits.js';
|
2022-07-07 21:06:37 +09:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.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 { MetaService } from '@/core/MetaService.js';
|
|
|
|
|
import { DriveService } from '@/core/DriveService.js';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { ApiError } from '../../../error.js';
|
2018-07-16 03:43:36 +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-16 03:43:36 +09:00
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 23:58:30 +09:00
|
|
|
max: 120,
|
2018-07-16 03:43:36 +09:00
|
|
|
},
|
|
|
|
|
|
2018-07-25 07:18:50 +09:00
|
|
|
requireFile: true,
|
2018-07-16 03:43:36 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
kind: 'write:drive',
|
2018-07-16 03:43:36 +09:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Upload a new 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: {
|
|
|
|
|
invalidFileName: {
|
|
|
|
|
message: 'Invalid file name.',
|
|
|
|
|
code: 'INVALID_FILE_NAME',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: 'f449b209-0c60-4e51-84d5-29486263bfd4',
|
|
|
|
|
},
|
2022-07-07 21:06:37 +09:00
|
|
|
|
|
|
|
|
inappropriate: {
|
|
|
|
|
message: 'Cannot upload the file because it has been determined that it possibly contains inappropriate content.',
|
|
|
|
|
code: 'INAPPROPRIATE',
|
|
|
|
|
id: 'bec5bd69-fba3-43c9-b4fb-2894b66ad5d2',
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
noFreeSpace: {
|
|
|
|
|
message: 'Cannot upload the file because you have no free space of drive.',
|
|
|
|
|
code: 'NO_FREE_SPACE',
|
|
|
|
|
id: 'd08dbc37-a6a9-463a-8c47-96c32ab5f064',
|
|
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
|
properties: {
|
|
|
|
|
folderId: { type: 'string', format: 'misskey:id', nullable: true, default: null },
|
|
|
|
|
name: { type: 'string', nullable: true, default: null },
|
|
|
|
|
comment: { type: 'string', nullable: true, maxLength: DB_MAX_IMAGE_COMMENT_LENGTH, default: null },
|
|
|
|
|
isSensitive: { type: 'boolean', default: false },
|
|
|
|
|
force: { type: 'boolean', default: false },
|
|
|
|
|
},
|
|
|
|
|
required: [],
|
|
|
|
|
} as const;
|
|
|
|
|
|
2022-01-03 02:12:50 +09:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
|
private metaService: MetaService,
|
|
|
|
|
private driveService: DriveService,
|
|
|
|
|
) {
|
|
|
|
|
super(meta, paramDef, async (ps, me, _, file, cleanup, ip, headers) => {
|
|
|
|
|
// Get 'name' parameter
|
|
|
|
|
let name = ps.name ?? file.originalname;
|
|
|
|
|
if (name !== undefined && name !== null) {
|
|
|
|
|
name = name.trim();
|
|
|
|
|
if (name.length === 0) {
|
|
|
|
|
name = null;
|
|
|
|
|
} else if (name === 'blob') {
|
|
|
|
|
name = null;
|
|
|
|
|
} else if (!this.driveFileEntityService.validateFileName(name)) {
|
|
|
|
|
throw new ApiError(meta.errors.invalidFileName);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
name = null;
|
|
|
|
|
}
|
2022-07-02 15:12:11 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Create file
|
|
|
|
|
const driveFile = await this.driveService.addFile({
|
|
|
|
|
user: me,
|
|
|
|
|
path: file.path,
|
|
|
|
|
name,
|
|
|
|
|
comment: ps.comment,
|
|
|
|
|
folderId: ps.folderId,
|
|
|
|
|
force: ps.force,
|
|
|
|
|
sensitive: ps.isSensitive,
|
|
|
|
|
requestIp: meta.enableIpLogging ? ip : null,
|
|
|
|
|
requestHeaders: meta.enableIpLogging ? headers : null,
|
|
|
|
|
});
|
|
|
|
|
return await this.driveFileEntityService.pack(driveFile, { self: true });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof Error || typeof err === 'string') {
|
|
|
|
|
console.error(err);
|
|
|
|
|
}
|
|
|
|
|
if (err instanceof IdentifiableError) {
|
|
|
|
|
if (err.id === '282f77bf-5816-4f72-9264-aa14d8261a21') throw new ApiError(meta.errors.inappropriate);
|
|
|
|
|
if (err.id === 'c6244ed2-a39a-4e1c-bf93-f0fbd7764fa6') throw new ApiError(meta.errors.noFreeSpace);
|
|
|
|
|
}
|
|
|
|
|
throw new ApiError();
|
|
|
|
|
} finally {
|
2019-04-13 01:43:22 +09:00
|
|
|
cleanup!();
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
});
|
2017-12-11 13:33:33 +09:00
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|