mirror of
https://github.com/transloadit/uppy.git
synced 2026-07-18 00:55:35 +00:00
* improve comments * unify http error responses when proxying requests this in order to make it easier to debug when setting up companion/transloadit integration, and lessen support burden on us. remove outdated `err.status` checks. this was added [7+ years ago](https://github.com/transloadit/uppy/blame/cf18689c1055055fc73a33fb9fe18e1046dfc8e4/packages/%40uppy/companion/src/standalone/index.js#L143) and we now use `got` which doesn't provide err.status Instead, for any other unhandled proxied HTTP request error responses, be nice and forward the JSON response to the client for easier debugging * Update packages/@uppy/companion/src/server/provider/error.js
59 lines
1.9 KiB
JavaScript
59 lines
1.9 KiB
JavaScript
const express = require('express')
|
|
const assert = require('node:assert')
|
|
|
|
const { startDownUpload } = require('../helpers/upload')
|
|
const { validateURL } = require('../helpers/request')
|
|
const { getURLMeta } = require('../helpers/request')
|
|
const logger = require('../logger')
|
|
const { downloadURL } = require('../download')
|
|
const { getGoogleFileSize, streamGoogleFile } = require('../provider/google/drive');
|
|
const { respondWithError } = require('../provider/error')
|
|
|
|
|
|
const getAuthHeader = (token) => ({ authorization: `Bearer ${token}` });
|
|
|
|
/**
|
|
*
|
|
* @param {object} req expressJS request object
|
|
* @param {object} res expressJS response object
|
|
*/
|
|
const get = async (req, res) => {
|
|
try {
|
|
logger.debug('Google Picker file import handler running', null, req.id)
|
|
|
|
const allowLocalUrls = false
|
|
|
|
const { accessToken, platform, fileId } = req.body
|
|
|
|
assert(platform === 'drive' || platform === 'photos');
|
|
|
|
const getSize = async () => {
|
|
if (platform === 'drive') {
|
|
return getGoogleFileSize({ id: fileId, token: accessToken })
|
|
}
|
|
const { size } = await getURLMeta(req.body.url, allowLocalUrls, { headers: getAuthHeader(accessToken) })
|
|
return size
|
|
}
|
|
|
|
if (platform === 'photos' && !validateURL(req.body.url, allowLocalUrls)) {
|
|
res.status(400).json({ error: 'Invalid URL' })
|
|
return
|
|
}
|
|
|
|
const download = () => {
|
|
if (platform === 'drive') {
|
|
return streamGoogleFile({ token: accessToken, id: fileId })
|
|
}
|
|
return downloadURL(req.body.url, allowLocalUrls, req.id, { headers: getAuthHeader(accessToken) })
|
|
}
|
|
|
|
await startDownUpload({ req, res, getSize, download })
|
|
} catch (err) {
|
|
logger.error(err, 'controller.googlePicker.error', req.id)
|
|
if (respondWithError(err, res)) return
|
|
res.status(500).json({ message: 'failed to fetch Google Picker URL' })
|
|
}
|
|
}
|
|
|
|
module.exports = () => express.Router()
|
|
.post('/get', express.json(), get)
|