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 {
content: '* ';
}
#chat .thumb {
#chat .image {
max-height: 120px;
max-width: 240px;
}

View file

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

View file

@ -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;

View file

@ -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",

View file

@ -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"

View file

@ -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"
};

View file

@ -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);
});
});
}