2019-02-01 16:16:27 +01:00
|
|
|
import * as ms from 'ms';
|
2019-02-05 03:48:08 +01:00
|
|
|
import $ from 'cafy';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { ID } from '../../../../../misc/cafy-id';
|
2018-04-07 21:02:12 +02:00
|
|
|
import create from '../../../../../services/drive/add-file';
|
2018-11-02 05:47:44 +01:00
|
|
|
import define from '../../../define';
|
2019-02-03 10:16:57 +01:00
|
|
|
import { apiLogger } from '../../../logger';
|
2019-02-22 03:46:58 +01:00
|
|
|
import { ApiError } from '../../../error';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { DriveFiles } from '../../../../../models';
|
2018-07-15 20:43:36 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'ドライブにファイルをアップロードします。',
|
|
|
|
'en-US': 'Upload a file to drive.'
|
2018-07-15 20:43:36 +02:00
|
|
|
},
|
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['drive'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: true as const,
|
2018-07-15 20:43:36 +02:00
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2018-10-31 15:03:14 +01:00
|
|
|
max: 120
|
2018-07-15 20:43:36 +02:00
|
|
|
},
|
|
|
|
|
2018-07-25 00:18:50 +02:00
|
|
|
requireFile: true,
|
2018-07-15 20:43:36 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
kind: 'write:drive',
|
2018-07-15 20:43:36 +02:00
|
|
|
|
|
|
|
params: {
|
2018-11-01 19:32:24 +01:00
|
|
|
folderId: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.nullable.type(ID),
|
2018-11-01 19:32:24 +01:00
|
|
|
default: null as any,
|
2018-07-15 20:43:36 +02:00
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'フォルダID'
|
2018-07-15 20:43:36 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-07-19 19:40:37 +02:00
|
|
|
|
2019-07-08 06:46:31 +02:00
|
|
|
name: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
default: null as any,
|
|
|
|
desc: {
|
|
|
|
'ja-JP': 'ファイル名(拡張子があるなら含めて)'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
isSensitive: {
|
2019-02-23 03:20:58 +01:00
|
|
|
validator: $.optional.either($.bool, $.str),
|
2018-10-20 08:50:13 +02:00
|
|
|
default: false,
|
2018-11-06 12:53:50 +01:00
|
|
|
transform: (v: any): boolean => v === true || v === 'true',
|
2018-07-19 19:40:37 +02:00
|
|
|
desc: {
|
2018-08-28 23:59:43 +02:00
|
|
|
'ja-JP': 'このメディアが「閲覧注意」(NSFW)かどうか',
|
|
|
|
'en-US': 'Whether this media is NSFW'
|
2018-07-19 19:40:37 +02:00
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
},
|
2018-10-20 08:50:13 +02:00
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
force: {
|
2019-02-23 03:20:58 +01:00
|
|
|
validator: $.optional.either($.bool, $.str),
|
2018-10-20 08:50:13 +02:00
|
|
|
default: false,
|
2018-11-06 12:53:50 +01:00
|
|
|
transform: (v: any): boolean => v === true || v === 'true',
|
2018-10-20 08:50:13 +02:00
|
|
|
desc: {
|
|
|
|
'ja-JP': 'true にすると、同じハッシュを持つファイルが既にアップロードされていても強制的にファイルを作成します。',
|
|
|
|
}
|
2018-11-01 19:32:24 +01:00
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
res: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
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: {
|
|
|
|
invalidFileName: {
|
|
|
|
message: 'Invalid file name.',
|
|
|
|
code: 'INVALID_FILE_NAME',
|
|
|
|
id: 'f449b209-0c60-4e51-84d5-29486263bfd4'
|
|
|
|
}
|
2018-07-15 20:43:36 +02:00
|
|
|
}
|
|
|
|
};
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2020-03-28 03:24:37 +01:00
|
|
|
export default define(meta, async (ps, user, isSecure, file, cleanup) => {
|
2016-12-28 23:49:51 +01:00
|
|
|
// Get 'name' parameter
|
2019-07-08 06:46:31 +02:00
|
|
|
let name = ps.name || file.originalname;
|
2016-12-28 23:49:51 +01:00
|
|
|
if (name !== undefined && name !== null) {
|
|
|
|
name = name.trim();
|
|
|
|
if (name.length === 0) {
|
|
|
|
name = null;
|
|
|
|
} else if (name === 'blob') {
|
|
|
|
name = null;
|
2019-04-07 14:50:36 +02:00
|
|
|
} else if (!DriveFiles.validateFileName(name)) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.invalidFileName);
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2017-12-11 05:33:33 +01:00
|
|
|
try {
|
|
|
|
// Create file
|
2018-10-20 08:50:13 +02:00
|
|
|
const driveFile = await create(user, file.path, name, null, ps.folderId, ps.force, false, null, null, ps.isSensitive);
|
2019-04-23 15:35:26 +02:00
|
|
|
return await DriveFiles.pack(driveFile, { self: true });
|
2017-12-11 05:33:33 +01:00
|
|
|
} catch (e) {
|
2019-02-03 10:16:57 +01:00
|
|
|
apiLogger.error(e);
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError();
|
|
|
|
} finally {
|
2019-04-12 18:43:22 +02:00
|
|
|
cleanup!();
|
2017-12-11 05:33:33 +01:00
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|