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

27 lines
704 B
TypeScript
Raw Normal View History

2017-02-17 05:46:14 +09:00
/**
* Module dependencies
*/
2017-03-09 03:50:09 +09:00
import $ from 'cafy';
2018-03-27 16:51:12 +09:00
import { pack } from '../../../models/drive-file';
import uploadFromUrl from '../../../common/drive/upload_from_url';
2017-02-17 05:46:14 +09:00
/**
* Create a file from a URL
*
2017-03-01 17:37:01 +09:00
* @param {any} params
* @param {any} user
* @return {Promise<any>}
2017-02-17 05:46:14 +09:00
*/
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
2017-03-09 03:50:09 +09:00
const [url, urlErr] = $(params.url).string().$;
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
const [folderId = null, folderIdErr] = $(params.folderId).optional.nullable.id().$;
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
};