|
| 1 | +const common = require('../../lib/common'); |
| 2 | +const {extract, hasProvider} = require('oembed-parser'); |
| 3 | +const Promise = require('bluebird'); |
| 4 | +const request = require('../../lib/request'); |
| 5 | +const cheerio = require('cheerio'); |
| 6 | + |
| 7 | +const findUrlWithProvider = (url) => { |
| 8 | + let provider; |
| 9 | + |
| 10 | + // build up a list of URL variations to test against because the oembed |
| 11 | + // providers list is not always up to date with scheme or www vs non-www |
| 12 | + let baseUrl = url.replace(/^\/\/|^https?:\/\/(?:www\.)?/, ''); |
| 13 | + let testUrls = [ |
| 14 | + `http://${baseUrl}`, |
| 15 | + `https://${baseUrl}`, |
| 16 | + `http://www.${baseUrl}`, |
| 17 | + `https://www.${baseUrl}` |
| 18 | + ]; |
| 19 | + |
| 20 | + for (let testUrl of testUrls) { |
| 21 | + provider = hasProvider(testUrl); |
| 22 | + if (provider) { |
| 23 | + url = testUrl; |
| 24 | + break; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return {url, provider}; |
| 29 | +}; |
| 30 | + |
| 31 | +const getOembedUrlFromHTML = (html) => { |
| 32 | + return cheerio('link[type="application/json+oembed"]', html).attr('href'); |
| 33 | +}; |
| 34 | + |
| 35 | +module.exports = { |
| 36 | + docName: 'oembed', |
| 37 | + |
| 38 | + read: { |
| 39 | + permissions: false, |
| 40 | + data: [ |
| 41 | + 'url' |
| 42 | + ], |
| 43 | + options: [], |
| 44 | + query({data}) { |
| 45 | + let {url} = data; |
| 46 | + |
| 47 | + if (!url || !url.trim()) { |
| 48 | + return Promise.reject(new common.errors.BadRequestError({ |
| 49 | + message: common.i18n.t('errors.api.oembed.noUrlProvided') |
| 50 | + })); |
| 51 | + } |
| 52 | + |
| 53 | + function unknownProvider() { |
| 54 | + return Promise.reject(new common.errors.ValidationError({ |
| 55 | + message: common.i18n.t('errors.api.oembed.unknownProvider') |
| 56 | + })); |
| 57 | + } |
| 58 | + |
| 59 | + function knownProvider(url) { |
| 60 | + return extract(url).catch((err) => { |
| 61 | + return Promise.reject(new common.errors.InternalServerError({ |
| 62 | + message: err.message |
| 63 | + })); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + let provider; |
| 68 | + ({url, provider} = findUrlWithProvider(url)); |
| 69 | + |
| 70 | + if (provider) { |
| 71 | + return knownProvider(url); |
| 72 | + } |
| 73 | + |
| 74 | + // see if the URL is a redirect to cater for shortened urls |
| 75 | + return request(url, { |
| 76 | + method: 'GET', |
| 77 | + timeout: 2 * 1000, |
| 78 | + followRedirect: true |
| 79 | + }).then((response) => { |
| 80 | + if (response.url !== url) { |
| 81 | + ({url, provider} = findUrlWithProvider(response.url)); |
| 82 | + return provider ? knownProvider(url) : unknownProvider(); |
| 83 | + } |
| 84 | + |
| 85 | + const oembedUrl = getOembedUrlFromHTML(response.body); |
| 86 | + |
| 87 | + if (!oembedUrl) { |
| 88 | + return unknownProvider(); |
| 89 | + } |
| 90 | + |
| 91 | + return request(oembedUrl, { |
| 92 | + method: 'GET', |
| 93 | + json: true |
| 94 | + }).then((response) => { |
| 95 | + return response.body; |
| 96 | + }); |
| 97 | + }).catch(() => { |
| 98 | + return unknownProvider(); |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | +}; |
0 commit comments