@uppy/companion
Version:
OAuth helper and remote fetcher for Uppy's (https://uppy.io) extensible file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Dropbox and Google Drive, S3 and more :dog:
69 lines (68 loc) • 2.52 kB
JavaScript
;
const express = require('express');
const { startDownUpload } = require('../helpers/upload');
const { downloadURL } = require('../download');
const { validateURL } = require('../helpers/request');
const { getURLMeta } = require('../helpers/request');
const logger = require('../logger');
const { respondWithError } = require('../provider/error');
/**
* @callback downloadCallback
* @param {Error} err
* @param {string | Buffer | Buffer[]} chunk
*/
/**
* Fetches the size and content type of a URL
*
* @param {object} req expressJS request object
* @param {object} res expressJS response object
*/
const meta = async (req, res) => {
try {
logger.debug('URL file import handler running', null, req.id);
const { allowLocalUrls } = req.companion.options;
if (!validateURL(req.body.url, allowLocalUrls)) {
logger.debug('Invalid request body detected. Exiting url meta handler.', null, req.id);
res.status(400).json({ error: 'Invalid request body' });
return;
}
const urlMeta = await getURLMeta(req.body.url, allowLocalUrls);
res.json(urlMeta);
}
catch (err) {
logger.error(err, 'controller.url.meta.error', req.id);
if (respondWithError(err, res))
return;
res.status(500).json({ message: 'failed to fetch URL metadata' });
}
};
/**
* Handles the reques of import a file from a remote URL, and then
* subsequently uploading it to the specified destination.
*
* @param {object} req expressJS request object
* @param {object} res expressJS response object
*/
const get = async (req, res) => {
logger.debug('URL file import handler running', null, req.id);
const { allowLocalUrls } = req.companion.options;
if (!validateURL(req.body.url, allowLocalUrls)) {
logger.debug('Invalid request body detected. Exiting url import handler.', null, req.id);
res.status(400).json({ error: 'Invalid request body' });
return;
}
const download = () => downloadURL(req.body.url, allowLocalUrls, req.id);
try {
await startDownUpload({ req, res, download, getSize: undefined });
}
catch (err) {
logger.error(err, 'controller.url.error', req.id);
if (respondWithError(err, res))
return;
res.status(500).json({ message: 'failed to fetch URL' });
}
};
module.exports = () => express
.Router()
.post('/meta', express.json(), meta)
.post('/get', express.json(), get);