sync/src/customembed.js

47 lines
1 KiB
JavaScript
Raw Permalink Normal View History

2015-06-18 18:46:33 -04:00
var cheerio = require("cheerio");
var crypto = require("crypto");
var Media = require("./media");
2013-08-03 11:50:41 -04:00
2015-06-18 18:46:33 -04:00
function sha256(input) {
var hash = crypto.createHash("sha256");
hash.update(input);
return hash.digest("base64");
}
function filter(input) {
var $ = cheerio.load(input, {
lowerCaseTags: true,
lowerCaseAttributeNames: true
});
2015-06-18 18:46:33 -04:00
var meta = getMeta($);
var id = "cu:" + sha256(input);
return new Media(id, "Custom Media", "--:--", "cu", meta);
}
function getMeta($) {
2021-03-21 21:20:49 -07:00
let tag = $("iframe");
2015-06-18 18:46:33 -04:00
if (tag.length !== 0) {
return filterIframe(tag[0]);
2014-02-18 21:56:54 -06:00
}
2021-03-21 21:20:49 -07:00
throw new Error("Invalid embed. Input must be an <iframe> tag");
2015-06-18 18:46:33 -04:00
}
function filterIframe(tag) {
if (!/^https:/.test(tag.attribs.src)) {
throw new Error("Invalid embed. Embed source must be HTTPS, plain HTTP is not supported.");
}
2015-06-18 18:46:33 -04:00
var meta = {
embed: {
tag: "iframe",
src: tag.attribs.src
}
};
return meta;
2013-08-03 11:50:41 -04:00
}
exports.filter = filter;