diff --git a/lib/customembed.js b/lib/customembed.js index e5e7ef90..c2923f16 100644 --- a/lib/customembed.js +++ b/lib/customembed.js @@ -1,22 +1,96 @@ -const allowed = ["iframe", "object", "param", "embed"]; -const tag_re = /<\s*\/?\s*([a-z]+)(\s*([a-z]+)\s*=\s*('[^']*'|"[^"]*"|[^"'>]*))*\s*>/ig; +var cheerio = require("cheerio"); +var crypto = require("crypto"); +var Media = require("./media"); -function filter(str) { - if (typeof str !== "string") { - return ""; +function sha256(input) { + var hash = crypto.createHash("sha256"); + hash.update(input); + return hash.digest("base64"); +} + +function filter(input) { + var $ = cheerio.load(input, { xmlMode: true }); + var meta = getMeta($); + var id = "cu:" + sha256(input); + + return new Media(id, "Custom Media", "--:--", "cu", meta); +} + +function getMeta($) { + var tag = $("embed"); + if (tag.length !== 0) { + return filterEmbed(tag[0]); + } + tag = $("object"); + if (tag.length !== 0) { + return filterObject(tag[0]); + } + tag = $("iframe"); + if (tag.length !== 0) { + return filterIframe(tag[0]); } - str = str.replace(tag_re, function (match, tag) { - if(!~allowed.indexOf(tag.toLowerCase())) { - return match.replace("<", "<").replace(">", ">"); + throw new Error("Invalid embed. Input must be an