2021-11-12 10:47:04 +00:00
|
|
|
import ms from 'ms';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { addFile } from '@/services/drive/add-file.js';
|
2022-07-02 06:12:11 +00:00
|
|
|
import { DriveFiles } from '@/models/index.js';
|
|
|
|
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits.js';
|
2022-07-07 12:06:37 +00:00
|
|
|
import { IdentifiableError } from '@/misc/identifiable-error.js';
|
2022-07-02 06:12:11 +00:00
|
|
|
import { fetchMeta } from '@/misc/fetch-meta.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import define from '../../../define.js';
|
|
|
|
import { apiLogger } from '../../../logger.js';
|
|
|
|
import { ApiError } from '../../../error.js';
|
2018-07-15 18:43:36 +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-15 18:43:36 +00:00
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 14:58:30 +00:00
|
|
|
max: 120,
|
2018-07-15 18:43:36 +00:00
|
|
|
},
|
|
|
|
|
2018-07-24 22:18:50 +00:00
|
|
|
requireFile: true,
|
2018-07-15 18:43:36 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
kind: 'write:drive',
|
2018-07-15 18:43:36 +00:00
|
|
|
|
2022-06-10 05:25:20 +00:00
|
|
|
description: 'Upload a new drive file.',
|
|
|
|
|
2019-02-23 02:20:58 +00:00
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 13:35:26 +00:00
|
|
|
ref: 'DriveFile',
|
2019-02-23 02:20:58 +00:00
|
|
|
},
|
|
|
|
|
2019-02-22 02:46:58 +00:00
|
|
|
errors: {
|
|
|
|
invalidFileName: {
|
|
|
|
message: 'Invalid file name.',
|
|
|
|
code: 'INVALID_FILE_NAME',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'f449b209-0c60-4e51-84d5-29486263bfd4',
|
|
|
|
},
|
2022-07-07 12:06:37 +00: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 14:58:30 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2016-12-28 22:49:51 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00: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-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-07-02 06:12:11 +00:00
|
|
|
export default define(meta, paramDef, async (ps, user, _, file, cleanup, ip, headers) => {
|
2016-12-28 22:49:51 +00:00
|
|
|
// Get 'name' parameter
|
2019-07-08 04:46:31 +00:00
|
|
|
let name = ps.name || file.originalname;
|
2016-12-28 22:49:51 +00:00
|
|
|
if (name !== undefined && name !== null) {
|
|
|
|
name = name.trim();
|
|
|
|
if (name.length === 0) {
|
|
|
|
name = null;
|
|
|
|
} else if (name === 'blob') {
|
|
|
|
name = null;
|
2019-04-07 12:50:36 +00:00
|
|
|
} else if (!DriveFiles.validateFileName(name)) {
|
2019-02-22 02:46:58 +00:00
|
|
|
throw new ApiError(meta.errors.invalidFileName);
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2022-07-02 06:12:11 +00:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2017-12-11 04:33:33 +00:00
|
|
|
try {
|
|
|
|
// Create file
|
2022-07-02 06:12:11 +00: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 13:35:26 +00:00
|
|
|
return await DriveFiles.pack(driveFile, { self: true });
|
2017-12-11 04:33:33 +00:00
|
|
|
} catch (e) {
|
2022-02-03 17:06:24 +00:00
|
|
|
if (e instanceof Error || typeof e === 'string') {
|
|
|
|
apiLogger.error(e);
|
|
|
|
}
|
2022-07-07 12:06:37 +00: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 02:46:58 +00:00
|
|
|
throw new ApiError();
|
|
|
|
} finally {
|
2019-04-12 16:43:22 +00:00
|
|
|
cleanup!();
|
2017-12-11 04:33:33 +00:00
|
|
|
}
|
2019-02-22 02:46:58 +00:00
|
|
|
});
|