Resolve relative URIs in link previewer

Also adds support for image_src links
This commit is contained in:
Pavel Djundik 2017-08-13 12:58:27 +03:00
parent d39e8ba9f8
commit 66ea26f4bd
2 changed files with 51 additions and 11 deletions

View file

@ -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: {

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({
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("<title>test invalid image</title><meta property='og:image' content='/real-test-image.png'>");
this.app.get("/thumb-image-src", function(req, res) {
res.send("<link rel='image_src' href='http://localhost:9002/real-test-image.png'>");
});
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("<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();
});
});