Use superagent for image download

This commit is contained in:
Mattias Erming 2014-08-17 14:40:08 -07:00
parent 6b0575f191
commit 357578e20c
7 changed files with 64 additions and 71 deletions

View File

@ -492,7 +492,7 @@ button {
#chat .action .user:before { #chat .action .user:before {
content: '* '; content: '* ';
} }
#chat .thumb { #chat .image {
max-height: 120px; max-height: 120px;
max-width: 240px; max-width: 240px;
} }

View File

@ -223,8 +223,8 @@
</span> </span>
<span class="text"> <span class="text">
<em class="type">{{type}}</em> <em class="type">{{type}}</em>
{{#equal type "thumb"}} {{#equal type "image"}}
<img src="{{text}}" class="thumb"> <img src="{{text}}" class="image">
{{else}} {{else}}
{{{uri text}}} {{{uri text}}}
{{/equal}} {{/equal}}

View File

@ -367,7 +367,6 @@ $(function() {
}); });
chat.on("msg", ".messages", function(e, target, msg) { chat.on("msg", ".messages", function(e, target, msg) {
console.log(msg);
var btn = sidebar.find(".chan[data-target=" + target + "]:not(.active)"); var btn = sidebar.find(".chan[data-target=" + target + "]:not(.active)");
var query = btn.hasClass("query"); var query = btn.hasClass("query");
var type = msg.type; var type = msg.type;

View File

@ -1,7 +1,7 @@
{ {
"name": "shout", "name": "shout",
"description": "A web IRC client", "description": "A web IRC client",
"version": "0.10.0", "version": "0.10.1",
"author": "Mattias Erming", "author": "Mattias Erming",
"preferGlobal": true, "preferGlobal": true,
"bin": { "bin": {
@ -30,7 +30,8 @@
"moment": "~2.7.0", "moment": "~2.7.0",
"read": "^1.0.5", "read": "^1.0.5",
"slate-irc": "~0.6.0", "slate-irc": "~0.6.0",
"socket.io": "~1.0.6" "socket.io": "~1.0.6",
"superagent": "^0.18.2"
}, },
"devDependencies": { "devDependencies": {
"grunt": "~0.4.5", "grunt": "~0.4.5",

View File

@ -10,6 +10,7 @@ module.exports = Client;
var id = 0; var id = 0;
var events = [ var events = [
"error", "error",
"image",
"join", "join",
"kick", "kick",
"mode", "mode",
@ -20,7 +21,6 @@ var events = [
"notice", "notice",
"part", "part",
"quit", "quit",
"thumb",
"topic", "topic",
"welcome", "welcome",
"whois" "whois"

View File

@ -4,6 +4,7 @@ var moment = require("moment");
Msg.Type = { Msg.Type = {
ACTION: "action", ACTION: "action",
ERROR: "error", ERROR: "error",
IMAGE: "image",
JOIN: "join", JOIN: "join",
KICK: "kick", KICK: "kick",
MESSAGE: "message", MESSAGE: "message",
@ -13,7 +14,6 @@ Msg.Type = {
NOTICE: "notice", NOTICE: "notice",
PART: "part", PART: "part",
QUIT: "quit", QUIT: "quit",
THUMB: "thumb",
TOPIC: "topic", TOPIC: "topic",
WHOIS: "whois" WHOIS: "whois"
}; };

View File

@ -1,63 +1,56 @@
var _ = require("lodash"); var _ = require("lodash");
var Msg = require("../../models/msg"); var Msg = require("../../models/msg");
var config = require("../../../config.json"); var config = require("../../../config.json");
var fs = require("fs"); var fs = require("fs");
var mkdirp = require("mkdirp"); var mkdirp = require("mkdirp");
var http = require("http"); var request = require("superagent");
module.exports = function(irc, network) { module.exports = function(irc, network) {
var client = this; var client = this;
irc.on("message", function(data) { irc.on("message", function(data) {
var image = ""; var image = "";
var split = data.message.split(" "); var split = data.message.split(" ");
_.each(split, function(w) { _.each(split, function(w) {
var match = w.match(/^(http|https).*\.(gif|png|jpg|jpeg)$/i); var match = w.match(/^(http|https).*\.(gif|png|jpg|jpeg)$/i);
if (match !== null) { if (match !== null) {
image = w; image = w;
} }
}); });
if (image === "") { if (image === "") {
return; return;
} }
var target = data.to; var target = data.to;
var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from}); var chan = _.findWhere(network.channels, {name: target.charAt(0) == "#" ? target : data.from});
if (typeof chan === "undefined") { if (typeof chan === "undefined") {
return; return;
} }
fetchImage(image, function(name) { fetchImage(image, function(name) {
var msg = new Msg({ var msg = new Msg({
type: Msg.Type.THUMB, type: Msg.Type.IMAGE,
from: data.from, from: data.from,
text: "thumbs/" + name text: "thumbs/" + name
}); });
chan.messages.push(msg); chan.messages.push(msg);
client.emit("msg", { client.emit("msg", {
chan: chan.id, chan: chan.id,
msg: msg msg: msg
}); });
}); });
}); });
}; };
function fetchImage(url, callback) { function fetchImage(url, callback) {
var path = process.env.HOME + "/.shout/cache/thumbs"; var path = process.env.HOME + "/.shout/cache/thumbs";
var name = new Date().getTime().toString() var name = new Date().getTime().toString();
mkdirp(path, function(e) { mkdirp(path, function(e) {
if (e) { if (e) {
console.log(e); console.log(e);
} }
var stream = fs.createWriteStream(path + "/" + name); var stream = fs.createWriteStream(path + "/" + name);
stream.on("error", function(e) { var req = request.get(url);
// .. req.pipe(stream);
}); req.on("end", function() {
http.get(url, function(res) { callback(name);
res.on("data", function(chunk) { });
stream.write(chunk); });
}); }
res.on("end", function() {
stream.end();
callback(name);
});
});
});
}