2016-12-28 23:49:51 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2016-12-28 23:49:51 +01: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 09:37:01 +01:00
|
|
|
* @param {any} file
|
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 23:49:51 +01:00
|
|
|
*/
|
2017-11-13 20:35:25 +01:00
|
|
|
module.exports = async (file, params, user): Promise<any> => {
|
2017-02-11 17:39:27 +01:00
|
|
|
if (file == null) {
|
2017-11-13 20:35:25 +01:00
|
|
|
throw 'file is required';
|
2017-02-11 17:39:27 +01:00
|
|
|
}
|
|
|
|
|
2016-12-28 23:49:51 +01: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-13 20:35:25 +01:00
|
|
|
throw 'invalid name';
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get 'folder_id' parameter
|
2017-03-08 19:50:09 +01:00
|
|
|
const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$;
|
2017-11-13 20:35:25 +01:00
|
|
|
if (folderIdErr) throw 'invalid folder_id param';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Create file
|
2017-11-13 20:35:25 +01:00
|
|
|
const driveFile = await create(user, file.path, name, null, folderId);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
|
|
|
// Serialize
|
2017-11-13 20:35:25 +01:00
|
|
|
return serialize(driveFile);
|
|
|
|
};
|