diff --git a/src/plugins/irc-events/link.js b/src/plugins/irc-events/link.js index 67e2db89..49e9f14e 100644 --- a/src/plugins/irc-events/link.js +++ b/src/plugins/irc-events/link.js @@ -2,6 +2,7 @@ const cheerio = require("cheerio"); const request = require("request"); +const url = require("url"); const Helper = require("../../helper"); const findLinks = require("../../../client/js/libs/handlebars/ircmessageparser/findLinks"); const es = require("event-stream"); @@ -62,8 +63,13 @@ function parse(msg, preview, res, client) { preview.thumb = $("meta[property=\"og:image\"]").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 if (!/^https?:\/\//.test(preview.thumb)) { preview.thumb = ""; @@ -111,8 +117,8 @@ function handlePreview(client, msg, preview, res) { return emitPreview(client, msg, preview); } - storage.store(res.data, res.type.replace("image/", ""), (url) => { - preview.thumb = url; + storage.store(res.data, res.type.replace("image/", ""), (uri) => { + preview.thumb = uri; emitPreview(client, msg, preview); }); @@ -135,11 +141,11 @@ function emitPreview(client, msg, preview) { }); } -function fetch(url, cb) { +function fetch(uri, cb) { let req; try { req = request.get({ - url: url, + url: uri, maxRedirects: 5, timeout: 5000, headers: { diff --git a/test/plugins/link.js b/test/plugins/link.js index 912d3de1..dfd73b0f 100644 --- a/test/plugins/link.js +++ b/test/plugins/link.js @@ -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({ - text: "http://localhost:9002/invalid-thumb" + text: "http://localhost:9002/thumb-image-src" }); link(this.irc, this.network.channels[0], message); - this.app.get("/invalid-thumb", function(req, res) { - res.send("test invalid image"); + this.app.get("/thumb-image-src", function(req, res) { + res.send(""); }); this.irc.once("msg:preview", function(data) { - expect(data.preview.thumb).to.be.empty; - expect(data.preview.head).to.equal("test invalid image"); - expect(data.preview.link).to.equal("http://localhost:9002/invalid-thumb"); + expect(data.preview.thumb).to.equal("http://localhost:9002/real-test-image.png"); + done(); + }); + }); + + 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(""); + }); + + 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("test relative image"); + }); + + 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(); }); });