Sharkey/src/server/api/endpoints/drive/files/upload_from_url.ts

23 lines
693 B
TypeScript
Raw Normal View History

2017-02-16 22:46:14 +02:00
/**
* Module dependencies
*/
2018-04-24 12:13:06 +03:00
import $ from 'cafy'; import ID from '../../../../../cafy-id';
2018-03-29 14:32:18 +03:00
import { pack } from '../../../../../models/drive-file';
2018-04-07 22:02:12 +03:00
import uploadFromUrl from '../../../../../services/drive/upload-from-url';
2017-02-16 22:46:14 +02:00
/**
* Create a file from a URL
*/
2017-11-13 21:28:51 +02:00
module.exports = async (params, user): Promise<any> => {
2017-02-16 22:46:14 +02:00
// Get 'url' parameter
2017-02-21 02:28:11 +02:00
// TODO: Validate this url
2018-05-02 12:06:16 +03:00
const [url, urlErr] = $.str.get(params.url);
2017-11-13 21:28:51 +02:00
if (urlErr) throw 'invalid url param';
2017-02-16 22:46:14 +02:00
2018-03-29 08:48:47 +03:00
// Get 'folderId' parameter
2018-05-02 12:06:16 +03:00
const [folderId = null, folderIdErr] = $.type(ID).optional().nullable().get(params.folderId);
2018-03-29 08:48:47 +03:00
if (folderIdErr) throw 'invalid folderId param';
2017-02-16 22:46:14 +02:00
2018-03-27 10:51:12 +03:00
return pack(await uploadFromUrl(url, user, folderId));
2017-11-13 21:28:51 +02:00
};