From 357578e20ce548f55fefe6739055424a909d2fed Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 17 Aug 2014 14:40:08 -0700 Subject: [PATCH] Use superagent for image download --- client/css/style.css | 2 +- client/index.html | 4 +- client/js/chat.js | 1 - package.json | 5 +- src/client.js | 2 +- src/models/msg.js | 2 +- src/plugins/irc-events/{thumb.js => image.js} | 119 +++++++++--------- 7 files changed, 64 insertions(+), 71 deletions(-) rename src/plugins/irc-events/{thumb.js => image.js} (74%) diff --git a/client/css/style.css b/client/css/style.css index 2234f9c..42baed2 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -492,7 +492,7 @@ button { #chat .action .user:before { content: '* '; } -#chat .thumb { +#chat .image { max-height: 120px; max-width: 240px; } diff --git a/client/index.html b/client/index.html index f72e4de..fe0fe83 100644 --- a/client/index.html +++ b/client/index.html @@ -223,8 +223,8 @@ {{type}} - {{#equal type "thumb"}} - + {{#equal type "image"}} + {{else}} {{{uri text}}} {{/equal}} diff --git a/client/js/chat.js b/client/js/chat.js index 545a167..58556a1 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -367,7 +367,6 @@ $(function() { }); chat.on("msg", ".messages", function(e, target, msg) { - console.log(msg); var btn = sidebar.find(".chan[data-target=" + target + "]:not(.active)"); var query = btn.hasClass("query"); var type = msg.type; diff --git a/package.json b/package.json index d6dadd2..327d0a0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "shout", "description": "A web IRC client", - "version": "0.10.0", + "version": "0.10.1", "author": "Mattias Erming", "preferGlobal": true, "bin": { @@ -30,7 +30,8 @@ "moment": "~2.7.0", "read": "^1.0.5", "slate-irc": "~0.6.0", - "socket.io": "~1.0.6" + "socket.io": "~1.0.6", + "superagent": "^0.18.2" }, "devDependencies": { "grunt": "~0.4.5", diff --git a/src/client.js b/src/client.js index 3c4bccc..1982396 100644 --- a/src/client.js +++ b/src/client.js @@ -10,6 +10,7 @@ module.exports = Client; var id = 0; var events = [ "error", + "image", "join", "kick", "mode", @@ -20,7 +21,6 @@ var events = [ "notice", "part", "quit", - "thumb", "topic", "welcome", "whois" diff --git a/src/models/msg.js b/src/models/msg.js index 3eb43fe..8b26d76 100644 --- a/src/models/msg.js +++ b/src/models/msg.js @@ -4,6 +4,7 @@ var moment = require("moment"); Msg.Type = { ACTION: "action", ERROR: "error", + IMAGE: "image", JOIN: "join", KICK: "kick", MESSAGE: "message", @@ -13,7 +14,6 @@ Msg.Type = { NOTICE: "notice", PART: "part", QUIT: "quit", - THUMB: "thumb", TOPIC: "topic", WHOIS: "whois" }; diff --git a/src/plugins/irc-events/thumb.js b/src/plugins/irc-events/image.js similarity index 74% rename from src/plugins/irc-events/thumb.js rename to src/plugins/irc-events/image.js index 97a2661..eb4676a 100644 --- a/src/plugins/irc-events/thumb.js +++ b/src/plugins/irc-events/image.js @@ -1,63 +1,56 @@ -var _ = require("lodash"); -var Msg = require("../../models/msg"); -var config = require("../../../config.json"); -var fs = require("fs"); -var mkdirp = require("mkdirp"); -var http = require("http"); - -module.exports = function(irc, network) { - var client = this; - irc.on("message", function(data) { - var image = ""; - var split = data.message.split(" "); - _.each(split, function(w) { - var match = w.match(/^(http|https).*\.(gif|png|jpg|jpeg)$/i); - if (match !== null) { - image = w; - } - }); - if (image === "") { - return; - } - var target = data.to; - var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from}); - if (typeof chan === "undefined") { - return; - } - fetchImage(image, function(name) { - var msg = new Msg({ - type: Msg.Type.THUMB, - from: data.from, - text: "thumbs/" + name - }); - chan.messages.push(msg); - client.emit("msg", { - chan: chan.id, - msg: msg - }); - }); - }); -}; - -function fetchImage(url, callback) { - var path = process.env.HOME + "/.shout/cache/thumbs"; - var name = new Date().getTime().toString() - mkdirp(path, function(e) { - if (e) { - console.log(e); - } - var stream = fs.createWriteStream(path + "/" + name); - stream.on("error", function(e) { - // .. - }); - http.get(url, function(res) { - res.on("data", function(chunk) { - stream.write(chunk); - }); - res.on("end", function() { - stream.end(); - callback(name); - }); - }); - }); -} +var _ = require("lodash"); +var Msg = require("../../models/msg"); +var config = require("../../../config.json"); +var fs = require("fs"); +var mkdirp = require("mkdirp"); +var request = require("superagent"); + +module.exports = function(irc, network) { + var client = this; + irc.on("message", function(data) { + var image = ""; + var split = data.message.split(" "); + _.each(split, function(w) { + var match = w.match(/^(http|https).*\.(gif|png|jpg|jpeg)$/i); + if (match !== null) { + image = w; + } + }); + if (image === "") { + return; + } + var target = data.to; + var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from}); + if (typeof chan === "undefined") { + return; + } + fetchImage(image, function(name) { + var msg = new Msg({ + type: Msg.Type.IMAGE, + from: data.from, + text: "thumbs/" + name + }); + chan.messages.push(msg); + client.emit("msg", { + chan: chan.id, + msg: msg + }); + }); + }); +}; + +function fetchImage(url, callback) { + var path = process.env.HOME + "/.shout/cache/thumbs"; + var name = new Date().getTime().toString(); + mkdirp(path, function(e) { + if (e) { + console.log(e); + } + var stream = fs.createWriteStream(path + "/" + name); + var req = request.get(url); + req.pipe(stream); + req.on("end", function() { + callback(name); + }); + }); +}