diff --git a/api/handlers/oembed.js b/api/handlers/oembed.js new file mode 100644 index 0000000..d955ac9 --- /dev/null +++ b/api/handlers/oembed.js @@ -0,0 +1,42 @@ +/* + * See oEmbed standard here: https://oembed.com/ + */ +const url = require('url') + +const toIFrame = (url, width, height) => + ` +` + +module.exports = (req, res) => { + let embedUrl = req.query.url + + try { + embedUrl = decodeURIComponent(req.query.url) + } catch (e) { + /* URL is already decoded */ + } + + try { + const { query } = url.parse(embedUrl) + + const width = Math.min(Number(req.query.maxwidth) || Infinity, 1024) + const height = Math.min(Number(req.query.maxheight) || Infinity, 473) + + const obj = { + version: '1.0', + type: 'rich', + provider_name: 'Carbon', + width, + height, + html: toIFrame(`?${query}`, width, height) + } + + return res.json(obj) + } catch (e) { + return res.status(500).send() + } +} diff --git a/api/server.js b/api/server.js index 2cd4184..cc12668 100644 --- a/api/server.js +++ b/api/server.js @@ -36,6 +36,7 @@ puppeteer.launch(puppeteerParams).then(browser => { const server = express() const imageHandler = require('./handlers/image')(browser) const unsplashHandler = require('./handlers/unsplash') + const oembedHandler = require('./handlers/oembed') if (dev) { server.use(morgan('tiny')) @@ -54,6 +55,7 @@ puppeteer.launch(puppeteerParams).then(browser => { server.post('/image', bodyParser.json({ limit: '5mb' }), wrap(imageHandler)) server.get('/unsplash/random', wrap(unsplashHandler.randomImages)) server.get('/unsplash/download/:imageId', wrap(unsplashHandler.downloadImage)) + server.get('/oembed', oembedHandler) server.listen(port, '0.0.0.0', err => { if (err) throw err