Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Companion: Use GET instead of HEAD for getURLMeta + Cut off length of file names #3048

Merged
merged 11 commits into from Sep 30, 2021
32 changes: 21 additions & 11 deletions packages/@uppy/companion/src/server/controllers/url.js
Expand Up @@ -43,15 +43,24 @@ const meta = (req, res) => {
* @param {object} res expressJS response object
*/
const get = (req, res) => {
logger.debug('URL file import handler running', null, req.id)
const { debug } = req.companion.options
if (!validateURL(req.body.url, debug)) {
logger.debug('Invalid request body detected. Exiting url import handler.', null, req.id)
return res.status(400).json({ error: 'Invalid request body' })
}
(async () => {
try {
logger.debug('URL file import handler running', null, req.id)
const { debug } = req.companion.options
if (!validateURL(req.body.url, debug)) {
logger.debug('Invalid request body detected. Exiting url import handler.', null, req.id)
res.status(400).json({ error: 'Invalid request body' })
return
}

// If we already have size, no need to get it again.
// See https://github.com/transloadit/uppy/issues/3034
let size = req.body && req.body.size
if (!size) {
const urlMeta = await reqUtil.getURLMeta(req.body.url, !debug)
size = urlMeta.size
}
mifi marked this conversation as resolved.
Show resolved Hide resolved

reqUtil.getURLMeta(req.body.url, !debug)
.then(({ size }) => {
// @ts-ignore
logger.debug('Instantiating uploader.', null, req.id)
const uploader = new Uploader(Uploader.reqToOptions(req, size))
Expand All @@ -70,11 +79,12 @@ const get = (req, res) => {

const response = uploader.getResponse()
res.status(response.status).json(response.body)
}).catch((err) => {
} catch (err) {
logger.error(err, 'controller.url.get.error', req.id)
// @todo send more meaningful error message and status code to client if possible
return res.status(err.status || 500).json({ message: 'failed to fetch URL metadata' })
})
res.status(err.status || 500).json({ message: 'failed to fetch URL metadata' })
}
})()
}

/**
Expand Down