2018-05-25 18:41:49 +09:00
|
|
|
import * as fs from 'fs';
|
2018-07-16 03:43:36 +09:00
|
|
|
const ms = require('ms');
|
2018-07-07 19:19:00 +09:00
|
|
|
import $ from 'cafy'; import ID 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-06-18 09:54:53 +09:00
|
|
|
import { ILocalUser } from '../../../../../models/user';
|
2018-07-16 03:43:36 +09:00
|
|
|
import getParams from '../../../get-params';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
desc: {
|
2018-07-17 04:36:44 +09:00
|
|
|
ja: 'ドライブにファイルをアップロードします。',
|
|
|
|
en: 'Upload a file to drive.'
|
2018-07-16 03:43:36 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
requireCredential: true,
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
|
|
|
max: 100
|
|
|
|
},
|
|
|
|
|
|
|
|
withFile: true,
|
|
|
|
|
|
|
|
kind: 'drive-write',
|
|
|
|
|
|
|
|
params: {
|
|
|
|
folderId: $.type(ID).optional.nullable.note({
|
|
|
|
default: null,
|
|
|
|
desc: {
|
|
|
|
ja: 'フォルダID'
|
|
|
|
}
|
2018-07-20 02:40:37 +09:00
|
|
|
}),
|
|
|
|
|
|
|
|
isSensitive: $.bool.optional.note({
|
|
|
|
default: false,
|
|
|
|
desc: {
|
|
|
|
ja: 'このメディアが「閲覧注意」(NSFW)かどうか',
|
|
|
|
en: 'Whether this media is NSFW'
|
|
|
|
}
|
2018-07-16 03:43:36 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2016-12-29 07:49:51 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a file
|
|
|
|
*/
|
2018-07-06 02:58:29 +09:00
|
|
|
export default async (file: any, params: any, user: ILocalUser): Promise<any> => {
|
2017-02-12 01:39:27 +09:00
|
|
|
if (file == null) {
|
2017-11-14 04:35:25 +09:00
|
|
|
throw 'file is required';
|
2017-02-12 01:39:27 +09:00
|
|
|
}
|
|
|
|
|
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)) {
|
2017-11-14 04:35:25 +09:00
|
|
|
throw 'invalid name';
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2018-05-25 18:41:49 +09:00
|
|
|
function cleanup() {
|
|
|
|
fs.unlink(file.path, () => {});
|
|
|
|
}
|
|
|
|
|
2018-07-16 03:43:36 +09:00
|
|
|
const [ps, psErr] = getParams(meta, params);
|
|
|
|
if (psErr) {
|
|
|
|
cleanup();
|
|
|
|
throw psErr;
|
|
|
|
}
|
|
|
|
|
2017-12-11 13:33:33 +09:00
|
|
|
try {
|
|
|
|
// Create file
|
2018-07-20 02:40:37 +09:00
|
|
|
const driveFile = await create(user, file.path, name, null, ps.folderId, false, false, null, null, ps.isSensitive);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-05-25 18:41:49 +09:00
|
|
|
cleanup();
|
|
|
|
|
2017-12-11 13:33:33 +09:00
|
|
|
// Serialize
|
2018-02-02 08:21:30 +09:00
|
|
|
return pack(driveFile);
|
2017-12-11 13:33:33 +09:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
|
2018-05-25 18:41:49 +09:00
|
|
|
cleanup();
|
|
|
|
|
2017-12-11 13:33:33 +09:00
|
|
|
throw e;
|
|
|
|
}
|
2017-11-14 04:35:25 +09:00
|
|
|
};
|