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

27 lines
716 B
TypeScript
Raw Normal View History

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