monkeeShark/src/server/api/endpoints/drive/files/create.ts

97 lines
2 KiB
TypeScript
Raw Normal View History

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