From 01fbd3c54eaa36f57af2fc5b2c25e3438d2e8482 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Thu, 18 Jun 2015 18:46:33 -0400 Subject: [PATCH] Work on custom embeds --- lib/customembed.js | 100 ++++++++++++++++++++++++++++++----- lib/get-info.js | 13 ++++- lib/media.js | 3 +- player/custom-embed.coffee | 46 ++++++++++++++++ player/generic-iframe.coffee | 28 ++++++++++ player/update.coffee | 1 + www/js/player-new.js | 66 ++++++++++++++++++++++- 7 files changed, 239 insertions(+), 18 deletions(-) create mode 100644 player/custom-embed.coffee create mode 100644 player/generic-iframe.coffee 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