2016-12-28 15:49:51 -07:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 11:50:09 -07:00
|
|
|
import $ from 'cafy';
|
2018-03-29 05:32:18 -06:00
|
|
|
import { validateFileName, pack } from '../../../../../models/drive-file';
|
2018-04-07 13:02:12 -06:00
|
|
|
import create from '../../../../../services/drive/add-file';
|
2016-12-28 15:49:51 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a file
|
|
|
|
*
|
2017-03-01 01:37:01 -07:00
|
|
|
* @param {any} file
|
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2016-12-28 15:49:51 -07:00
|
|
|
*/
|
2017-11-13 12:35:25 -07:00
|
|
|
module.exports = async (file, params, user): Promise<any> => {
|
2017-02-11 09:39:27 -07:00
|
|
|
if (file == null) {
|
2017-11-13 12:35:25 -07:00
|
|
|
throw 'file is required';
|
2017-02-11 09:39:27 -07:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:49:51 -07: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 12:35:25 -07:00
|
|
|
throw 'invalid name';
|
2016-12-28 15:49:51 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name = null;
|
|
|
|
}
|
|
|
|
|
2018-03-28 23:48:47 -06:00
|
|
|
// Get 'folderId' parameter
|
|
|
|
const [folderId = null, folderIdErr] = $(params.folderId).optional.nullable.id().$;
|
|
|
|
if (folderIdErr) throw 'invalid folderId param';
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-12-10 21:33:33 -07:00
|
|
|
try {
|
|
|
|
// Create file
|
|
|
|
const driveFile = await create(user, file.path, name, null, folderId);
|
2016-12-28 15:49:51 -07:00
|
|
|
|
2017-12-10 21:33:33 -07:00
|
|
|
// Serialize
|
2018-02-01 16:21:30 -07:00
|
|
|
return pack(driveFile);
|
2017-12-10 21:33:33 -07:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
|
|
|
|
throw e;
|
|
|
|
}
|
2017-11-13 12:35:25 -07:00
|
|
|
};
|