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

23 lines
693 B
TypeScript
Raw Normal View History

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