43 lines
979 B
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const fetch = require('node-fetch');
const isValidDomain = require('is-valid-domain');
module.exports = {
async uploadProxy(ctx) {
try {
const url = new URL(ctx.query.url);
if (!['http:', 'https:'].includes(url.protocol)) {
2020-05-25 10:09:19 +02:00
throw new Error('Unexpected url protocol');
}
if (!isValidDomain(url.hostname)) {
2020-05-25 10:09:19 +02:00
throw new Error('Invalid url hostname');
}
} catch (err) {
ctx.status = 400;
ctx.body = 'Invalid URL';
return;
}
try {
const res = await fetch(ctx.query.url, {
headers: _.omit(ctx.request.headers, ['origin', 'host', 'authorization']),
});
for (const [key, value] of res.headers.entries()) {
ctx.set(key, value);
}
ctx.status = res.status;
ctx.body = res.body;
} catch (err) {
strapi.log.error(err);
ctx.status = 500;
ctx.body = 'Internal Server Error';
}
},
};