1
0
mirror of https://github.com/hotomoe/hotomoe synced 2025-01-10 20:02:53 +09:00
hotomoe/src/server/api/endpoints/drive/files/create.ts

84 lines
1.8 KiB
TypeScript
Raw Normal View History

2018-07-16 03:43:36 +09:00
const ms = require('ms');
2018-11-02 03:32:24 +09:00
import $ from 'cafy'; import ID, { transform } from '../../../../../misc/cafy-id';
2018-03-29 20:32:18 +09:00
import { validateFileName, pack } from '../../../../../models/drive-file';
2018-04-08 04:02:12 +09:00
import create from '../../../../../services/drive/add-file';
2018-11-02 13:47:44 +09:00
import define from '../../../define';
2018-07-16 03:43:36 +09:00
export const meta = {
desc: {
2018-08-29 06:59:43 +09:00
'ja-JP': 'ドライブにファイルをアップロードします。',
'en-US': 'Upload a file to drive.'
2018-07-16 03:43:36 +09:00
},
requireCredential: true,
limit: {
duration: ms('1hour'),
2018-10-31 23:03:14 +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
kind: 'drive-write',
params: {
2018-11-02 03:32:24 +09:00
folderId: {
validator: $.type(ID).optional.nullable,
transform: transform,
default: null as any,
2018-07-16 03:43:36 +09:00
desc: {
2018-08-29 06:59:43 +09:00
'ja-JP': 'フォルダID'
2018-07-16 03:43:36 +09:00
}
2018-11-02 03:32:24 +09:00
},
2018-07-20 02:40:37 +09:00
2018-11-02 03:32:24 +09:00
isSensitive: {
validator: $.bool.optional,
default: false,
2018-07-20 02:40:37 +09:00
desc: {
2018-08-29 06:59:43 +09:00
'ja-JP': 'このメディアが「閲覧注意」(NSFW)かどうか',
'en-US': 'Whether this media is NSFW'
2018-07-20 02:40:37 +09:00
}
2018-11-02 03:32:24 +09:00
},
2018-11-02 03:32:24 +09:00
force: {
validator: $.bool.optional,
default: false,
desc: {
'ja-JP': 'true にすると、同じハッシュを持つファイルが既にアップロードされていても強制的にファイルを作成します。',
}
2018-11-02 03:32:24 +09:00
}
2018-07-16 03:43:36 +09:00
}
};
2016-12-29 07:49:51 +09:00
2018-11-02 13:47:44 +09:00
export default define(meta, (ps, user, app, file, cleanup) => new Promise(async (res, rej) => {
2016-12-29 07:49:51 +09:00
// Get 'name' parameter
let 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 (!validateFileName(name)) {
2018-11-02 13:47:44 +09:00
return rej('invalid name');
2016-12-29 07:49:51 +09:00
}
} else {
name = null;
}
2017-12-11 13:33:33 +09:00
try {
// Create file
const driveFile = await create(user, file.path, name, null, ps.folderId, ps.force, false, null, null, ps.isSensitive);
2016-12-29 07:49:51 +09:00
2018-05-25 18:41:49 +09:00
cleanup();
2018-11-02 13:47:44 +09:00
res(pack(driveFile));
2017-12-11 13:33:33 +09:00
} catch (e) {
console.error(e);
2018-05-25 18:41:49 +09:00
cleanup();
2018-11-02 13:47:44 +09:00
rej(e);
2017-12-11 13:33:33 +09:00
}
2018-11-02 13:47:44 +09:00
}));