yumechi-no-kuni/src/server/api/endpoints/drive/files/upload_from_url.ts

27 lines
707 B
TypeScript
Raw Normal View History

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