Merge pull request #1410 from thelounge/xpaw/resolve-relative-uris

Resolve relative URIs in link previewer
This commit is contained in:
Jérémie Astori 2017-08-13 12:15:24 -04:00 committed by GitHub
commit f688e8df6c
2 changed files with 51 additions and 11 deletions

View File

@ -2,6 +2,7 @@
const cheerio = require("cheerio"); const cheerio = require("cheerio");
const request = require("request"); const request = require("request");
const url = require("url");
const Helper = require("../../helper"); const Helper = require("../../helper");
const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks"); const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks");
const es = require("event-stream"); const es = require("event-stream");
@ -62,8 +63,13 @@ function parse(msg, preview, res, client) {
preview.thumb = preview.thumb =
$("meta[property=\"og:image\"]").attr("content") $("meta[property=\"og:image\"]").attr("content")
|| $("meta[name=\"twitter:image:src\"]").attr("content") || $("meta[name=\"twitter:image:src\"]").attr("content")
|| $("link[rel=\"image_src\"]").attr("href")
|| ""; || "";
if (preview.thumb.length) {
preview.thumb = url.resolve(preview.link, preview.thumb);
}
// Make sure thumbnail is a valid url // Make sure thumbnail is a valid url
if (!/^https?:\/\//.test(preview.thumb)) { if (!/^https?:\/\//.test(preview.thumb)) {
preview.thumb = ""; preview.thumb = "";
@ -111,8 +117,8 @@ function handlePreview(client, msg, preview, res) {
return emitPreview(client, msg, preview); return emitPreview(client, msg, preview);
} }
storage.store(res.data, res.type.replace("image/", ""), (url) => { storage.store(res.data, res.type.replace("image/", ""), (uri) => {
preview.thumb = url; preview.thumb = uri;
emitPreview(client, msg, preview); emitPreview(client, msg, preview);
}); });
@ -135,11 +141,11 @@ function emitPreview(client, msg, preview) {
}); });
} }
function fetch(url, cb) { function fetch(uri, cb) {
let req; let req;
try { try {
req = request.get({ req = request.get({
url: url, url: uri,
maxRedirects: 5, maxRedirects: 5,
timeout: 5000, timeout: 5000,
headers: { headers: {

View File

@ -110,21 +110,55 @@ describe("Link plugin", function() {
}); });
}); });
it("should not use thumbnail with invalid url", function(done) { it("should find image_src", function(done) {
const message = this.irc.createMessage({ const message = this.irc.createMessage({
text: "http://localhost:9002/invalid-thumb" text: "http://localhost:9002/thumb-image-src"
}); });
link(this.irc, this.network.channels[0], message); link(this.irc, this.network.channels[0], message);
this.app.get("/invalid-thumb", function(req, res) { this.app.get("/thumb-image-src", function(req, res) {
res.send("<title>test invalid image</title><meta property='og:image' content='/real-test-image.png'>"); res.send("<link rel='image_src' href='http://localhost:9002/real-test-image.png'>");
}); });
this.irc.once("msg:preview", function(data) { this.irc.once("msg:preview", function(data) {
expect(data.preview.thumb).to.be.empty; expect(data.preview.thumb).to.equal("http://localhost:9002/real-test-image.png");
expect(data.preview.head).to.equal("test invalid image"); done();
expect(data.preview.link).to.equal("http://localhost:9002/invalid-thumb"); });
});
it("should correctly resolve relative protocol", function(done) {
const message = this.irc.createMessage({
text: "http://localhost:9002/thumb-image-src"
});
link(this.irc, this.network.channels[0], message);
this.app.get("/thumb-image-src", function(req, res) {
res.send("<link rel='image_src' href='//localhost:9002/real-test-image.png'>");
});
this.irc.once("msg:preview", function(data) {
expect(data.preview.thumb).to.equal("http://localhost:9002/real-test-image.png");
done();
});
});
it("should resolve url correctly for relative url", function(done) {
const message = this.irc.createMessage({
text: "http://localhost:9002/relative-thumb"
});
link(this.irc, this.network.channels[0], message);
this.app.get("/relative-thumb", function(req, res) {
res.send("<title>test relative image</title><meta property='og:image' content='/real-test-image.png'>");
});
this.irc.once("msg:preview", function(data) {
expect(data.preview.thumb).to.equal("http://localhost:9002/real-test-image.png");
expect(data.preview.head).to.equal("test relative image");
expect(data.preview.link).to.equal("http://localhost:9002/relative-thumb");
done(); done();
}); });
}); });