1
0
mirror of https://github.com/MisskeyIO/misskey synced 2024-12-29 05:58:15 +09:00
MisskeyIO/src/api/endpoints/drive/files/create.ts

53 lines
1.1 KiB
TypeScript
Raw Normal View History

2016-12-29 07:49:51 +09:00
/**
* Module dependencies
*/
2017-03-09 03:50:09 +09:00
import $ from 'cafy';
2016-12-29 07:49:51 +09:00
import { validateFileName } from '../../../models/drive-file';
import serialize from '../../../serializers/drive-file';
import create from '../../../common/add-file-to-drive';
/**
* Create a file
*
2017-03-01 17:37:01 +09:00
* @param {any} file
* @param {any} params
* @param {any} user
* @return {Promise<any>}
2016-12-29 07:49:51 +09:00
*/
module.exports = async (file, params, user): Promise<any> => {
2017-02-12 01:39:27 +09:00
if (file == null) {
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)) {
throw 'invalid name';
2016-12-29 07:49:51 +09:00
}
} else {
name = null;
}
// Get 'folder_id' parameter
2017-03-09 03:50:09 +09:00
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
if (folderIdErr) throw 'invalid folder_id param';
2016-12-29 07:49:51 +09:00
2017-12-11 13:33:33 +09:00
try {
// Create file
const driveFile = await create(user, file.path, name, null, folderId);
2016-12-29 07:49:51 +09:00
2017-12-11 13:33:33 +09:00
// Serialize
return serialize(driveFile);
} catch (e) {
console.error(e);
throw e;
}
};