2021-11-12 10:47:04 +00:00
|
|
|
import ms from 'ms';
|
2022-01-19 17:43:13 +00:00
|
|
|
import { addFile } from '@/services/drive/add-file';
|
2021-08-19 12:55:45 +00:00
|
|
|
import define from '../../../define';
|
|
|
|
import { apiLogger } from '../../../logger';
|
|
|
|
import { ApiError } from '../../../error';
|
|
|
|
import { DriveFiles } from '@/models/index';
|
2022-01-23 13:52:35 +00:00
|
|
|
import { DB_MAX_IMAGE_COMMENT_LENGTH } from '@/misc/hard-limits';
|
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
|
|
|
|
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-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-02-20 04:15:40 +00:00
|
|
|
// @ts-ignore
|
2022-02-19 05:05:32 +00:00
|
|
|
export default define(meta, paramDef, async (ps, user, _, file, cleanup) => {
|
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;
|
|
|
|
}
|
|
|
|
|
2017-12-11 04:33:33 +00:00
|
|
|
try {
|
|
|
|
// Create file
|
2022-01-23 13:52:35 +00:00
|
|
|
const driveFile = await addFile({ user, path: file.path, name, comment: ps.comment, folderId: ps.folderId, force: ps.force, sensitive: ps.isSensitive });
|
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);
|
|
|
|
}
|
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
|
|
|
});
|