rudeshark.net/src/server/api/endpoints/drive/files/create.ts
2018-10-31 23:03:14 +09:00

97 lines
2.0 KiB
TypeScript

import * as fs from 'fs';
const ms = require('ms');
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
import { validateFileName, pack } from '../../../../../models/drive-file';
import create from '../../../../../services/drive/add-file';
import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';
export const meta = {
desc: {
'ja-JP': 'ドライブにファイルをアップロードします。',
'en-US': 'Upload a file to drive.'
},
requireCredential: true,
limit: {
duration: ms('1hour'),
max: 120
},
requireFile: true,
kind: 'drive-write',
params: {
folderId: $.type(ID).optional.nullable.note({
default: null,
desc: {
'ja-JP': 'フォルダID'
}
}),
isSensitive: $.bool.optional.note({
default: false,
desc: {
'ja-JP': 'このメディアが「閲覧注意」(NSFW)かどうか',
'en-US': 'Whether this media is NSFW'
}
}),
force: $.bool.optional.note({
default: false,
desc: {
'ja-JP': 'true にすると、同じハッシュを持つファイルが既にアップロードされていても強制的にファイルを作成します。',
}
})
}
};
export default async (file: any, params: any, user: ILocalUser): Promise<any> => {
if (file == null) {
throw 'file is required';
}
// 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';
}
} else {
name = null;
}
function cleanup() {
fs.unlink(file.path, () => {});
}
const [ps, psErr] = getParams(meta, params);
if (psErr) {
cleanup();
throw psErr;
}
try {
// Create file
const driveFile = await create(user, file.path, name, null, ps.folderId, ps.force, false, null, null, ps.isSensitive);
cleanup();
// Serialize
return pack(driveFile);
} catch (e) {
console.error(e);
cleanup();
throw e;
}
};