2017-02-16 21:46:14 +01:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
2017-03-08 19:50:09 +01:00
|
|
|
import $ from 'cafy';
|
2018-03-29 13:32:18 +02:00
|
|
|
import { pack } from '../../../../../models/drive-file';
|
2018-03-31 12:55:00 +02:00
|
|
|
import uploadFromUrl from '../../../../../common/drive/upload_from_url';
|
2017-02-16 21:46:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a file from a URL
|
|
|
|
*
|
2017-03-01 09:37:01 +01:00
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
2017-02-16 21:46:14 +01:00
|
|
|
*/
|
2017-11-13 20:28:51 +01:00
|
|
|
module.exports = async (params, user): Promise<any> => {
|
2017-02-16 21:46:14 +01:00
|
|
|
// Get 'url' parameter
|
2017-02-21 01:28:11 +01:00
|
|
|
// TODO: Validate this url
|
2017-03-08 19:50:09 +01:00
|
|
|
const [url, urlErr] = $(params.url).string().$;
|
2017-11-13 20:28:51 +01:00
|
|
|
if (urlErr) throw 'invalid url param';
|
2017-02-16 21:46:14 +01:00
|
|
|
|
2018-03-29 07:48:47 +02:00
|
|
|
// Get 'folderId' parameter
|
|
|
|
const [folderId = null, folderIdErr] = $(params.folderId).optional.nullable.id().$;
|
|
|
|
if (folderIdErr) throw 'invalid folderId param';
|
2017-02-16 21:46:14 +01:00
|
|
|
|
2018-03-27 09:51:12 +02:00
|
|
|
return pack(await uploadFromUrl(url, user, folderId));
|
2017-11-13 20:28:51 +01:00
|
|
|
};
|