2021-11-12 19:47:04 +09:00
|
|
|
import ms from 'ms';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { addFile } from '@/services/drive/add-file.js';
|
2022-07-02 15:12:11 +09:00
|
|
|
import { DriveFiles } from '@/models/index.js';
|
|
|
|
|
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-07-02 15:12:11 +09:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import define from '../../../define.js';
|
|
|
|
|
import { apiLogger } from '../../../logger.js';
|
|
|
|
|
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-07-02 15:12:11 +09:00
|
|
|
export default define(meta, paramDef, async (ps, user, _, file, cleanup, ip, headers) => {
|
2016-12-29 07:49:51 +09:00
|
|
|
// Get 'name' parameter
|
2019-07-08 13:46:31 +09:00
|
|
|
let name = ps.name || file.originalname;
|
2016-12-29 07:49:51 +09:00
|
|
|
if (name !== undefined && name !== null) {
|
|
|
|
|
name = name.trim();
|
|
|
|
|
if (name.length === 0) {
|
|
|
|
|
name = null;
|
|
|
|
|
} else if (name === 'blob') {
|
|
|
|
|
name = null;
|
2019-04-07 21:50:36 +09:00
|
|
|
} else if (!DriveFiles.validateFileName(name)) {
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError(meta.errors.invalidFileName);
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
name = null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-02 15:12:11 +09:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
|
2017-12-11 13:33:33 +09:00
|
|
|
try {
|
|
|
|
|
// Create file
|
2022-07-02 15:12:11 +09:00
|
|
|
const driveFile = await addFile({
|
|
|
|
|
user,
|
|
|
|
|
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,
|
|
|
|
|
});
|
2019-04-23 22:35:26 +09:00
|
|
|
return await DriveFiles.pack(driveFile, { self: true });
|
2017-12-11 13:33:33 +09:00
|
|
|
} catch (e) {
|
2022-02-04 02:06:24 +09:00
|
|
|
if (e instanceof Error || typeof e === 'string') {
|
|
|
|
|
apiLogger.error(e);
|
|
|
|
|
}
|
2022-07-07 21:06:37 +09:00
|
|
|
if (e instanceof IdentifiableError) {
|
|
|
|
|
if (e.id === '282f77bf-5816-4f72-9264-aa14d8261a21') throw new ApiError(meta.errors.inappropriate);
|
|
|
|
|
if (e.id === 'c6244ed2-a39a-4e1c-bf93-f0fbd7764fa6') throw new ApiError(meta.errors.noFreeSpace);
|
|
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
throw new ApiError();
|
|
|
|
|
} finally {
|
2019-04-13 01:43:22 +09:00
|
|
|
cleanup!();
|
2017-12-11 13:33:33 +09:00
|
|
|
}
|
2019-02-22 11:46:58 +09:00
|
|
|
});
|